# References

### Searching with Schema references

The reference documents resemble the following document:

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

> <mark style="color:yellow;">**$ref**</mark>
>
> &#x20;  The `$ref` field holds the name of the **Schema** where the referenced document resides.
>
> <mark style="color:yellow;">**$id**</mark>
>
> &#x20;   The `$id` field contains the value of the `_id` field in the referenced document.

#### ***Example***

```javascript
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:

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

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

#### Using the property $data

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