0

I have a problem creating a query in MongoDB. I have the following JSON. How to ask mongodb question to get _id from level5 object?

{
  "Id": "1",
  "level1": {
    "level2": {
      "level3": {
        "level4": {
          "level5": {
            "_id": 2
          }
        }
      }
    }
  }
}

1 Answer 1

1

You can use the dot notation to get value from nested object:

db.collection.aggregate([
    {
        $project: {
            value: "$level1.level2.level3.level4.level5._id"
        }
    }
])

Mongo Playground

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

2 Comments

Need by aggregations not by find?
Just showing the way you can access it, same dot notation can be used for filtering etc. Nothing prevents you from fetching entire document and accessing that property in your code, what is your use-case?

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.