1

I need your help to get data from collection with and condition. Suppose I have a collection having below document and I need search on sub document field named brandId having value of 14 and 38.

{
    "_id" : ObjectId("5b3b206c4a25da19d05f41a2"),
    "models" : [ 
        {
            "brandId" : "14",
            "modelId" : "100",
            "brandSlug" : "honda",
            "modelSlug" : "hrv"
        }, 
        {
            "brandId" : "38",
            "modelId" : "894",
            "brandSlug" : "toyota",
            "modelSlug" : "fortuner"
        }, 
        {
            "brandId" : "38",
            "modelId" : "894",
            "brandSlug" : "toyota",
            "modelSlug" : "fortuner"
        }, 
        {
            "brandId" : "37",
            "modelId" : "773",
            "brandSlug" : "suzuki",
            "modelSlug" : "ertiga"
        } 
    ]
}

I want that sub documents which brandId is 14 and 38.

Desired Output =>

{
    "_id" : ObjectId("5b3b206c4a25da19d05f41a2"),
    "models" : [ 
        {
            "brandId" : "14",
            "modelId" : "100",
            "brandSlug" : "honda",
            "modelSlug" : "hrv"
        }, 
        {
            "brandId" : "38",
            "modelId" : "1240",
            "brandSlug" : "toyota",
            "modelSlug" : "kijang-innova"
        }, 
        {
            "brandId" : "38",
            "modelId" : "894",
            "brandSlug" : "toyota",
            "modelSlug" : "fortuner"
        } 
    ]
}
4
  • 1
    Try docs.mongodb.com/manual/reference/operator/projection/elemMatch/… and show your attempts Commented Jul 4, 2018 at 13:37
  • it's return 37 brandId data also Commented Jul 4, 2018 at 13:47
  • Apparently you do something wrong in the query. You need to find a way to tell us how exactly you are fetching documents. Commented Jul 4, 2018 at 13:52
  • 1
    hi @AlexBlex $elemMatch will not work here... because there are two matching condition inside the array... Commented Jul 4, 2018 at 13:55

2 Answers 2

1

Why be so complicated? Unwind,match and group will do the job perfectly.

db.test1.aggregate([
            {
                $unwind: {
                    path : "$models"
                }
            },
            {
                $match: {
                $or:[{"models.brandId":"14"},{"models.brandId":"38"}]
                }
            },
            {
                $group: {
                _id:"$_id",
                models:{$push:"$models"}
                }
            },
        ]
    );
Sign up to request clarification or add additional context in comments.

1 Comment

0

You can try $filter aggregation with $or operator

db.collection.aggregate([
  {
    "$addFields": {
      "models": {
        "$filter": {
          "input": "$models",
          "as": "model",
          "cond": {
            "$or": [
              {
                "$eq": [
                  "$$model.brandId",
                  "14"
                ]
              },
              {
                "$eq": [
                  "$$model.brandId",
                  "38"
                ]
              }
            ]
          }
        }
      }
    }
  }
])

1 Comment

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.