Mongo db query for finding object type Mongo db query for finding object type json json

Mongo db query for finding object type


You can use $type operator like this

db.collection.find( { "devices" : { $type : "object" } } );

or

db.collection.find({ "devices": { $not: { $type: "array" } }})


Update:

Try one of below query as per your requirement (remove empty objects or keep only empty objects):

db.device.find( {$and:[ {devices:{ $type : "object" }},{devices:{$not: { $type: "array" }}}], devices:{$ne:{}}} );db.device.find( {$and:[ {devices:{ $type : "object" }},{devices:{$not: { $type: "array" }}}], devices:{$eq:{}}} );

Check this screenshot:

enter image description here

enter image description here

Moreover, please note that you have duplicate keys (_id) in your data set which means those data sets are not inserted into your DB. So query will obviously won't give proper results.

Edit:

OP removed duplicate keys from data set.