Pavan kumar Dasireddy
Pavan kumar Dasireddy

Reputation: 366

Mongo db query for finding object type

I've 1000's of users and user data looks like the below. Some users have the devices as [{some data}] (array), some users have devices as {some data}, and some users have devices as empty [].

I need mongodb query to find the userslist with devices {some data}. Here is the sample of my users data.

{
    "_id" : ObjectId("56fe07bab95708fa18d45ac4"), 
    "username" : "abcd",    
    "devices" : []
},
{
    "_id" : ObjectId("56fe07bab95708fa18d45df7"), 
    "username" : "efgh",    
    "devices" : [ 
        {
            "_id" : ObjectId("5827804ef659a60400e12fcb"),
            "devicetype" : "web"
        }
    ],
},
{
    "_id" : ObjectId("56fe07bab95708fa18d45ae8"), 
    "username" : "efgh",    
    "devices" : {
         "_id" : ObjectId("5951ea8b47abe300046ea26e"),
         "devicetype" : "web"
     }
},
{
    "_id" : ObjectId("56fe07bab95708fa18d45b5b"), 
    "username" : "ijkl",    
    "devices" : [ 
        {
            "_id" : ObjectId("59bd2317eeff3200049a2ba6"),
            "devicetype" : "ios"
            "devicetoken" : "1abffaa4419d498b48d0bf982"
        }
    ],
},
{
    "_id" : ObjectId("56fe07bab95708fa18d46102"), 
    "username" : "efgh",    
    "devices" : {
         "_id" : ObjectId("58c433da28841d00040d3cdb"),
         "devicetype" : "web"
     }
},
{
    "_id" : ObjectId("56fe07bab95708fa18d46177"), 
    "username" : "efgh",    
    "devices" : {
         "_id" : ObjectId("59d073d96974d20004a4bb9f"),
         "devicetype" : "web"
     }
},
{
    "_id" : ObjectId("56fe07bab95708fa18d456c9"), 
    "username" : "ijkl",    
    "devices" : [ 
        {
            "_id" : ObjectId("59b93dd2e6673c00044cca49"),
            "devicetype" : "ios"
            "devicetoken" : "1abffaa4419d498b48d0bf982"
        }
    ],
},
{
    "_id" : ObjectId("56fe07bab95708fa18d456f4"), 
    "username" : "abcd",    
    "devices" : []
}

Upvotes: 4

Views: 3478

Answers (3)

Rahul Raj
Rahul Raj

Reputation: 3459

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.

Upvotes: 3

Efe
Efe

Reputation: 5796

You can use $type operator like this

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

or

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

Upvotes: 10

pathik devani
pathik devani

Reputation: 530

You can use $type operator.

Find more detail on this link https://docs.mongodb.com/manual/reference/operator/query/type/

Upvotes: 2

Related Questions