ref
is not nullish, this becomes a populated virtual justOne
, the populated virtual will be a single doc or null
. true
, Mongoose will call any custom getters you defined on this virtual true
, populate()
will set this virtual to the number of populated documents, as opposed to the documents themselves, using Query#countDocuments()
VirtualType constructor
This is what mongoose uses to define virtual attributes via Schema.prototype.virtual
.
const fullname = schema.virtual('fullname');
fullname instanceof mongoose.VirtualType // true
Applies getters to value
.
Applies setters to value
.
Adds a custom getter to this virtual.
value
: the value returned by the previous getter. If there is only one getter, value
will be undefined
.virtual
: the virtual object you called .get()
ondoc
: the document this virtual is attached to. Equivalent to this
.var virtual = schema.virtual('fullname');
virtual.get(function(value, virtual, doc) {
return this.name.first + ' ' + this.name.last;
});
Adds a custom setter to this virtual.
value
: the value being setvirtual
: the virtual object you're calling .set()
ondoc
: the document this virtual is attached to. Equivalent to this
.const virtual = schema.virtual('fullname');
virtual.set(function(value, virtual, doc) {
var parts = value.split(' ');
this.name.first = parts[0];
this.name.last = parts[1];
});
const Model = mongoose.model('Test', schema);
const doc = new Model();
// Calls the setter with `value = 'Jean-Luc Picard'`
doc.fullname = 'Jean-Luc Picard';
doc.name.first; // 'Jean-Luc'
doc.name.last; // 'Picard'
© 2010 LearnBoost
Licensed under the MIT License.
https://mongoosejs.com/docs/api/virtualtype.html