0

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!

2 Answers 2

2

You could do that through aggregation

User.aggregate([{$unwind: '$deal_details'}, 
                {$match: {'deal_details.deals_tag': 'B'}}, 
                {$project: {deal_details: 1, _id: 0}}])
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @zangw thanks for your reply, may I know is it possible to use $in in $match in order to match more document? like ['B','H'] and then return match document
@JohnLim, yes, you can do that as {$match: {'deal_details.deals_tag': {$in: ['B', 'H']}}}
2

To find elements with specific value in array in sub Document (it's your case) and to filter the result, you shoul use the aggregation framework. It's impossible with a simple find document.

You can see an example with your problem here : How to filter array in subdocument with MongoDB

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.