dan*_*rvt 14 mongoose mongodb node.js
我的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();
...
Run Code Online (Sandbox Code Playgroud)
Joh*_*yHK 20
不,你不能.Mongoose虚拟属性仅存在于文档的Mongoose模型表示中,而不存在于执行查询的MongoDB本身中.
您需要查询的任何字段必须在模式中定义为非虚拟字段并持久保存到数据库.