需要mongo db查询来查找对象类型

时间:2023-01-03 02:57:53

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 [].

我有1000个用户和用户数据如下所示。有些用户将设备设为[{some data}](数组),有些用户设备为{some data},有些用户将设备设为空[]。

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

我需要mongodb查询来查找带有设备{some 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" : []
}

3 个解决方案

#1


2  

Use can use $type operator like this

使用可以像这样使用$ type运算符

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

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

or

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

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

#2


2  

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: 需要mongo db查询来查找对象类型 需要mongo db查询来查找对象类型

检查此截图:

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 wont give proper results.

此外,请注意您的数据集中有重复的键(_id),这意味着这些数据集未插入到您的数据库中。所以查询显然不会给出正确的结果。

Edit: OP removed duplicate keys from data set.

编辑:OP从数据集中删除了重复的键。

#3


1  

You can use $type operator.

您可以使用$ type运算符。

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

有关此链接的更多详细信息,请访问https://docs.mongodb.com/manual/reference/operator/query/type/

#1


2  

Use can use $type operator like this

使用可以像这样使用$ type运算符

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

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

or

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

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

#2


2  

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: 需要mongo db查询来查找对象类型 需要mongo db查询来查找对象类型

检查此截图:

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 wont give proper results.

此外,请注意您的数据集中有重复的键(_id),这意味着这些数据集未插入到您的数据库中。所以查询显然不会给出正确的结果。

Edit: OP removed duplicate keys from data set.

编辑:OP从数据集中删除了重复的键。

#3


1  

You can use $type operator.

您可以使用$ type运算符。

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

有关此链接的更多详细信息,请访问https://docs.mongodb.com/manual/reference/operator/query/type/