0

This is my collection structure.

{
 "_id" : ObjectId("5685ea32ba5298688d27cceb"),
 "name" : "t1",
 "array" : [ 
     {
         "name" : "n1",
         "innerArray1" : [ 
             {
                 "id" : 1,
                 "name" : "aaa"
             }, 
             {
                 "id" : 2,
                 "name" : "bbb"
             }
         ],
         "innerArray2" : [ 
             {
                 "id" : 1,
                 "name" : "cccc",
                 "active" : true
             }, 
             {
                 "id" : 2,
                 "name" : "dddd",
                 "active" : false
             }
         ]
     }
 ]}

after find().i want to get only innerArray2. I tried like this

db.getCollection('Test').find({"name":"t1","array.name":"n1"},{"array.$":1})

{
"_id" : ObjectId("5685ea32ba5298688d27cceb"),
"array" : [ 
    {
        "name" : "n1",
        "innerArray1" : [ 
            {
                "id" : 1,
                "name" : "aaa"
            }, 
            {
                "id" : 2,
                "name" : "bbb"
            }
        ],
        "innerArray2" : [ 
            {
                "id" : 1,
                "name" : "cccc",
                "active" : true
            }, 
            {
                "id" : 2,
                "name" : "dddd",
                "active" : false
            }
        ]
    }
]

}

but actually i want to remove innerArray1 element from the result.this is my expectation.

{
"_id" : ObjectId("5685ea32ba5298688d27cceb"),
"array" : [ 
    {
        "name" : "n1",
        "innerArray2" : [ 
            {
                "id" : 1,
                "name" : "cccc",
                "active" : true
            }, 
            {
                "id" : 2,
                "name" : "dddd",
                "active" : false
            }
        ]
    }
]}

how can I do this ?

styvane
  • 59,869
  • 19
  • 150
  • 156
Tharanga
  • 377
  • 3
  • 8
  • 18
  • 1
    Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – styvane Jan 01 '16 at 11:55
  • thanks.this reference give the solution.needs to doing filtering part also inside the projection – Tharanga Jan 01 '16 at 14:25

1 Answers1

1

I think Instead of using db.getCollection('Test').find() you can use db.getCollection('Test').findOne('innerArray2').

Bijal
  • 132
  • 1
  • 8