may I know how can I use $in operator for array of object? and only return match document as result?
This is what I have tried so far but I still not able to get what I want.
Let's say I have set of documents look like this
{
"_id" : ObjectId("56824417fa32883c0335ce61"),
"company_name" : "Pentagon",
"deal_details" : [
{
"deals_name" : "hello",
"deals_tag" : ["A","B","C"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
{
"deals_name" : "Hola",
"deals_tag" : ["E","F","G"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
],
"_id" : ObjectId("54824417fa32983c0335ce61"),
"company_name" : "Oreo",
"deal_details" : [
{
"deals_name" : "Bye",
"deals_tag" : ["B"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
{
"deals_name" : "Ciao",
"deals_tag" : ["H","I","J"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
]
}
and I do this
User.find({
'deal_details.deals_tags': {
$in: ["B"]
}
}
}, {'deal_details':1}).exec(function(err, deal) {
if (err) {
return res.status(500).send(err);
}
res.status(200).send(deal);
});
what I expect from deal is
{
"deals_name" : "hello",
"deals_tag" : ["A","B","C"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
{
"deals_name" : "Bye",
"deals_tag" : ["B"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
}
however I get this instead
{
"deals_name" : "hello",
"deals_tag" : ["A","B","C"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
{
"deals_name" : "Hola",
"deals_tag" : ["E","F","G"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
{
"deals_name" : "Bye",
"deals_tag" : ["B"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
},
{
"deals_name" : "Ciao",
"deals_tag" : ["H","I","J"],
"_id" : ObjectId("56c69da8a1f60804064abf0f")
}
So can I know is there a way to only return only matched document? your help is appreciated thanks!