in mongoose, how to select the fields in a array property
in Mongoose, I have such a schema
var schema= mongoose.Schema({
name:String,
age:String
likes: [{ type:String, price:String}]
});
As in the mongoose, the option in the Model.find(conditions, [fields], [options], [callback]) gives us a choice to select the fields we need. However in my schema, there is a array property 'likes', and in the query, I only want to select only one property type rather than all of them, how can I make this query ?
我真的发现了它,只是通过
Model.findOne({ 'name': name }, 'name age likes.type', callback);
For find()
you need to use select()
chain call, something like this:
Model.find({ name: name }).select('name age likes.type').exec(function() {
});
Or
Model.find({ name: name }).select({ 'name': 1, 'age': 1, 'likes.type': 1 }).exec(function() {
});
More examples here in Mongoose docs
Don't forget to use cursor for traversing over large amounts of data (it's described in the Streaming section of the docs).
链接地址: http://www.djcxy.com/p/60684.html上一篇: 如何在Mongoose中使用Document#更新?
下一篇: 在猫鼬中,如何选择数组属性中的字段