Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in mongoose, how to select the fields in a array property

Tags:

mongoose

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 ?

like image 945
user824624 Avatar asked Oct 27 '25 18:10

user824624


1 Answers

I actually found it out, just by

Model.findOne({ 'name': name }, 'name age likes.type', callback);
like image 122
user824624 Avatar answered Oct 30 '25 14:10

user824624



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!