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:
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.
You can use $type
operator.
Find more detail on this link https://docs.mongodb.com/manual/reference/operator/query/type/