0
{
"_id" : NumberLong(107),
"cnic" : NumberLong(098765),
"recordsOFGt" : [ 
    {
        "records" : [ 
            {
                "_id" : NumberLong(1),
                "contactIds" : [ 
                    NumberLong(303)
                ],
            },
            {
                "_id" : NumberLong(2),
                "contactIds" : [ 
                    NumberLong(303),
                    NumberLong(304)
                ],
            },
            {
                "_id" : NumberLong(3),
                "contactIds" : [ 
                    NumberLong(309),
                    NumberLong(304)
                ],
            },
            {
                "_id" : NumberLong(4),
                "contactIds" : [ 
                    NumberLong(303),
                    NumberLong(304)
                ],
            },
            {
                "_id" : NumberLong(5),
                "contactIds" : [ 
                    NumberLong(303),
                    NumberLong(304)
                ],
            },
        ]
    },
    "records2" : {
     ...
     ...
    }
]
}

I want only record element that will be matched via _id I have tried this

db.getCollection('tempCollection').findOne(    
  { 'recordsOFGt': {$elemMatch: {'records._id': 1} }},
  {'recordsOFGt.$': 1}
)

this returns all elements of records array need just matching element.

this is what I want as output :

{

     "records" : 
          {
               "_id" : NumberLong(1),
               "contactIds" : [ 
                     NumberLong(303)
               ],
          }

}
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35

0 Answers0