Reputation: 4667
ASchema={item:[BSchema]};
ASchema.findOne({item._id=xx})
It gets a array of BSchema, document.item is a array. how to get only one item which _id is xx?
Upvotes: 1
Views: 2185
Reputation: 151112
You want the positional $
operator using query projection to just return your matched array element. For Mongoose you can do this:
ASchema.findOne({"item._id": itemId},"item.$",function(err,doc) {
console.log( doc );
});
Or paired in an object:
ASchema.findOne({"item._id": itemId},{ "item.$": 1 },function(err,doc) {
console.log( doc );
});
Mongoose supports the shorthand syntax with options like "-fieldname" for field removal which is the same as { "fieldname": 0 }
. But you cannot mix inclusion and exclusion with the exception of the root _id
field.
Therefore you must specify all of the fields you want to appear when using projection.
See also .select()
in the mongoose documentation.
Upvotes: 6
Reputation: 276
I think your syntax for the query is wrong. Try:
ASchema.findOne({'item._id': xx})
This link is helpful for more examples: http://mongoosejs.com/docs/queries.html
Upvotes: 1