4

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" : []
}

2
  • You probably want to check if device array is empty rather than checking its type. Aren't you? Check my answer below. Commented Mar 27, 2018 at 8:59
  • Updated my answer below. Commented Mar 27, 2018 at 9:21

3 Answers 3

10

You can use $type operator like this

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

or

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

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

2 Comments

But it is strange it is working for me with type object
db.collection.find({ "devices": { $not: { $type: "array" } } }) is works well for me, i tried this with date you provided.
3

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.

8 Comments

He wants this {} so just object types neither array nor empty array.
I only need the users list who have devices: { } no need of devices:[ { ----}], and devices :[]
@PavankumarDasireddy Got it now.
@PavankumarDasireddy Updated my answer, please check it.Let me know if that works!
devices: {} means, devices in object like: "devices" : { "_id" : ObjectId("5a5325442f001203c39193bd"), "devicetype" : "web" }
|
2

You can use $type operator.

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

Comments

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.