I have a virtual property in my mongoose schema, I would like to know if I can query my documents using this property.
var PersonSchema = new Schema({ number: {type: Number, required: true}, name: {type: Date, required: true} }); PersonSchema.virtual('capitalCaseName').get(function () { return this.name.toUpperCase(); }); ... Person.find({"capitalCaseName": "DANIEL"}).exec(); ...
In Mongoose, a virtual is a property that is not stored in MongoDB. Virtuals are typically used for computed properties on documents.
Virtuals are document properties that do not persist or get stored in the MongoDB database, they only exist logically and are not written to the document's collection.
Populating Virtuals: In MongoDB, Population is the process of replacing the specified path in the document of one collection with the actual document from the other collection.
The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ). If you don't want to use this version key you can use the versionKey: false as mongoose.
No, you can't. Mongoose virtual properties only exist in the Mongoose model representation of documents, not in MongoDB itself where the query is performed.
Any field you need to query against must be defined in the schema as a non-virtual field and persisted to the database.
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