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 ?

2

1 Answer 1

1

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

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

1 Comment

thanks.but it return all the elements in the array.but i only want "array.name":"n1" this element

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.