MongoDB: Projection of only one element of an array nested to another array nested to an object

0

I have the following situation: I need to query a document with a projection that returns only the first element of a list. But 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 search for value: "a" 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 have tried all possible alternatives: I used $ elemMatch, $ and $ slice to return only the first element, but I can not 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 thank you all right away!

    
asked by anonymous 15.12.2016 / 22:02

1 answer

-1
db.getCollection('items').find({"item": {"items":{"item":{"cell":{"value":"a"}}}}})
    
04.10.2017 / 15:00