在mongoose中查询虚拟财产

我有一个在我的mongoose模式的虚拟财产,我想知道我是否可以使用此属性查询我的文档。

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(); ... 

不,你不能。 Mongoose虚拟属性仅存在于文档的Mongoose模型表示中,而不存在于执行查询的MongoDB本身中。

您需要查询的任何字段都必须在架构中定义为非虚拟字段并保存到数据库。