1

i m new in mongodb, i want only single object data which id is 5689746, by which way can i get this?

[
  {
    "_id": 8965651651,
    "orditems": [
      {
        "_id": 65748413141,
        "itms": [
          {
            "item1": "aa",
            "item2": "bb",
            "item3": "cc",
            "_id": 7894567
          },
          {
            "item1": "dd",
            "item2": "ee",
            "item3": "ff",
            "_id": 5689746
          },
          {
            "item1": "gg",
            "item2": "hh",
            "item3": "ii",
            "_id": 1644824
          }
        ]
      },
      {
        "_id": 87448413141,
        "itms": [
          {
            "item1": "jj",
            "item2": "kk",
            "item3": "ll",
            "_id": 9874567
          },
          {
            "item1": "mm",
            "item2": "nn",
            "item3": "oo",
            "_id": 8659746
          },
          {
            "item1": "pp",
            "item2": "qq",
            "item3": "rr",
            "_id": 4614824
          }
        ]
      }
    ]
  }
]

i m using $elemMatch but i didn't get result as expected

db.orders.findOne({
  _id: 8965651651
},
{
  orditems: {
    $elemMatch: {
      _id: 87448413141,
      itms: {
        $elemMatch: {
          _id: 5689746
        }
      }
    }
  }
})

i want result like

{
    "_id": 8965651651,
    "orditems": [
      {
        "_id": 65748413141,
        "itms": [
          {
            "item1": "dd",
            "item2": "ee",
            "item3": "ff",
            "_id": 5689746
          }
        ]
      }
    ]
  }

1 Answer 1

2

You need to chain up $filter and $mergeObjects as you are doubly nesting your array fields.

db.collection.aggregate([
  {
    $match: {
      "_id": 8965651651
    }
  },
  {
    $set: {
      orditems: {
        "$filter": {
          "input": "$orditems",
          "as": "oi",
          "cond": {
            $eq: [
              "$$oi._id",
              65748413141
            ]
          }
        }
      }
    }
  },
  {
    $set: {
      orditems: {
        "$map": {
          "input": "$orditems",
          "as": "oi",
          "in": {
            "$mergeObjects": [
              "$$oi",
              {
                itms: {
                  "$filter": {
                    "input": "$$oi.itms",
                    "as": "i",
                    "cond": {
                      $eq: [
                        "$$i._id",
                        5689746
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Mr. ray it is working, is there any other simple way to achieve this
@manojsahni I doubt so. I would suggest refactoring your schema if possible to avoid doubly nesting the arrays. Highly nested arrays are considered an anti-pattern and introduce unnecessary complexity to queries.

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.