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" : []
}
You can use $type
operator like this
db.collection.find( { "devices" : { $type : "object" } } );
or
db.collection.find({ "devices": { $not: { $type: "array" } }})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With