References

Reference system between schemas, through a find you can merge data from other schemas.

Searching with Schema references

The reference documents resemble the following document:

{ "$ref" : <value>, "$id" : <value> }

$ref

The $ref field holds the name of the Schema where the referenced document resides.

$id

The $id field contains the value of the _id field in the referenced document.

Example

User.find({
  _id: 1,
  creator: { $ref: "Creators", $id: 1 },
}); // { _id: 1, username: 'Customer', bag: { weapons: [ 'bow', 'katana' ] }, creator: { _id: 2, name: 'Lennart' } }

In this example we use the "Creators" schema as $ref, looking for _id 2 in it.

To use the find base data, use $data, it returns the base value. The $data can only be used in the $id field, for that, the $id can be an object with $data inside or just a string, starting with $data

We use an object reference system, so if you want to access an object, pass the path inside the string, for example:

$data = {
  a: {
    b: {
      c: 22,
      d: [1, 2, 3],
    },
  }
}

"$data.a.b.d.1" // 2
"$data.a.b.c" // 22

Using the property $data

User.find({
  _id: 1, // find base
  creator: { $ref: "Creators", $id: { $data: "creatorId" } }
  // or
  creator: { $ref: "Creators", $id: "$data.creatorId" }
})

Last updated