Virtualtype
Parameters
options «Object»
[options.ref] «string|function» if is not nullish, this becomes a
[options.localField] «string|function» the local field to populate on if this is a populated virtual.
[options.foreignField] «string|function» the foreign field to populate on if this is a populated virtual.
[options.justOne=false] «boolean» by default, a populated virtual is an array. If you set
justOne
, the populated virtual will be a single doc ornull
.[options.getters=false] «boolean» if you set this to
true
, Mongoose will call any custom getters you defined on this virtual[options.count=false] «boolean» if you set this to
true
,populate()
will set this virtual to the number of populated documents, as opposed to the documents themselves, usingQuery#countDocuments()
[options.skip=null] «Number» add a default
skip
to thepopulate()
query[options.perDocumentLimit=null] «Number» For legacy reasons, with
populate()
may give incorrect results because it only executes a single query for every document being populated. If you setperDocumentLimit
, Mongoose will ensure correctlimit
per document by executing a separate query for each document topopulate()
. For example,.find().populate({ path: 'test', perDocumentLimit: 2 })
will execute 2 additional queries if.find()
returns 2 documents.[options.options=null] «Object» Additional options like
limit
andlean
.
VirtualType constructor
This is what mongoose uses to define virtual attributes via Schema.prototype.virtual
.
Example:
Parameters
- value «Object»
- doc «Document» The document this virtual is attached to
Returns:
- «any» the value after applying all getters
Applies getters to value
.
Parameters
- value «Object»
- doc «Document»
Returns:
- «any» the value after applying all setters
Applies setters to value
.
Parameters
- VirtualType, «Function(Any|» Document)} fn
Returns:
- «VirtualType» this
Adds a custom getter to this virtual.
Mongoose calls the getter function with the below 3 parameters.
- : the value returned by the previous getter. If there is only one getter,
value
will beundefined
. virtual
: the virtual object you called.get()
ondoc
: the document this virtual is attached to. Equivalent tothis
.
Example:
Parameters
- VirtualType, «Function(Any|» Document)} fn
Returns:
- «VirtualType» this
Mongoose calls the setter function with the below 3 parameters.
value
: the value being setdoc
: the document this virtual is attached to. Equivalent tothis
.