I have a mongoose schema with a virtual of mixed type. For example:
var mongoose = require('mongoose') // version 3.3.1
var FooSchema = new mongoose.Schema( { x: Number } );
FooSchema.virtual('v').set( function(value){
console.log("SETTING", value);
});
var Foo = mongoose.model('Foo', FooSchema);
new Foo( { v:1 } );
new Foo( { v:[] } );
new Foo( { v:{} } );
When I run this code I get:
SETTING 1
SETTING []
As you'll notice it never shows "SETTING {}", any reason why this doesn't work?
Reference to issue on Github
In Mongoose, a virtual is a property that is not stored in MongoDB. Virtuals are typically used for computed properties on documents.
findById returns the document where the _id field matches the specified id . If the document is not found, the function returns null .
For such cases, mongoose provides the $in operator. In this article, we will discuss how to use $in operator. We will use the $in method on the kennel collection. For performing HTTP endpoint testing, we will use the postman tool.
For populating virtual, we have to specify three necessary options: ref: It contains the name of the model from which we want to populate the document. localField: It is any field of the current collection. foreignField: It is any field of the collection from which we want to populate the document.
This was an open issue in Mongoose that's been fixed in a recent commit by Aaron Heckmann.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With