Mongodb: Projection of only one element from one nested array to another nested array to one object

Asked

Viewed 492 times

0

I have the following situation: I need to query a document with a projection that returns only the first element of a list. Only this list is inside another list, and that other list is inside an object that is inside another object in a document in Mongodb, more or less like the following example:

    {
      "items": {
        "item": [
          {
            "items": {
              "item": [
                {
                  "cell": [
                    {
                      "value": "a"
                    },
                    {
                      "value": "b"
                    },
                    {
                      "value": "c"
                    }
                  ]
                }
              ]
            }
          }
        ]
      }
    }

My goal is to make a search for value : "the" with a projection that ignores the other values. That is, my projection should return this:

    {
      "items": {
        "item": [
          {
            "items": {
              "item": [
                {
                  "cell": [
                    {
                      "value": "a"
                    }
                  ]
                }
              ]
            }
          }
        ]
      }
    }

The problem is that I’ve tried all the possible alternatives: I used $elemMatch, $ and $Lice to return only the first element, but I can’t mount a query that does this because of the complexity of the document. I’m using Mongodb 3.2.

If you need more information I update here. Anyway thanks to everyone from now on!

1 answer

-1

db.getCollection('items').find({"item": {"items":{"item":{"cell":{"value":"a"}}}}})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.