mongodb 查找值不为空的文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24079660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:44:43  来源:igfitidea点击:

Find documents where value is not null

mongodb

提问by Jase Whatson

I have the following doc structure

我有以下文档结构

{
    "model" : {

    },
    "_id" : ObjectId("538df963d1c3f82329000257"),
    "email" : "[email protected]",
    "name" : "sam",
    "endpointarn" : "arn:aws:sns:us-east-1:284585229836:endpoint/GCM/Positivethinking/59eb7c66-c13c-3e44-af80-0b1f4f7ea9fd",
    "devregidtoken" : "APA91bFMOBo6ZWemMAG5clKEd5VYVvbeDOM5zNXjbbl3faSE0FZt3gtJYv0hpjdWuOY-mvr-k3sCcq0dEveCM6jx5mOh1u6JEubuUmKb2zU64dn_A4gJ4pCBG7AGQJ8DnkO83Ca4wgzsoJcio9T-TtA",
    "topicsubs" : [
        {
            "topicsubid" : "arn:aws:sns:us-east-1:284585229836:Wealth-ProsperityQuotes:5f3e8060-48fa-4a8e-bdc3-e9596747da1a",
            "topicname" : "Wealth-Prosperity",
            "topicno" : 0
        }
    ],
    "timecreated" : ISODate("2014-06-03T16:35:47.442Z"),
    "purchasedata" : {
        "orderid" : "111",
        "packagename" : "",
        "productid" : "",
        "purchasetime" : "",
        "purchasestate" : "",
        "developerpayload" : "",
        "purchasetoken" : ""
    },
    "ccode" : "",
    "ccodestat" : ""
}

I want to get all documents where purchasedata.orderid IS NOT NULL. I have tried

我想获取purchasedata.orderid 不是NULL 的所有文档。我试过了

db.User.find({"purchasedata.orderid" : {$ne : ""}});

回答by Christian P

If you're checking for nullyou can use $neoperator:

如果您正在检查,null您可以使用$ne运算符:

db.User.find({
   "purchasedata.orderid" : { $ne : null }
});

Note:

笔记:

OP originally wanted to check that the value is not an empty string "" and not null. nulland empty string are two different BSON types. You can use $typeand $notoperators to check where the value is nulland that the key exists:

OP 最初想检查该值是否为空字符串 "" 且不为 null。null和空字符串是两种不同的BSON 类型。您可以使用$type$not运算符来检查值在哪里null以及键是否存在:

db.User.find({
   "purchasedata.orderid" : { 
      $not : { $type : 10 }, 
      $exists : true
   }
});

The $typeoperator selects documents where the value is a specific BSON type (10corresponds to Null). $existswill check that the key exists in the subdocument.

$type操作者选择文件,其中的值是一个特定类型的BSON(10相当于Null)。$exists将检查该键是否存在于子文档中。

回答by vintastic

You were very close. If the value is actually null, you can just use "null" instead of blank.

你非常接近。如果该值实际上为 null,则可以使用“null”而不是空白。

db.User.find({"purchasedata.orderid" : {$ne : null}});

回答by Jase Whatson

So this is what finally worked for me

所以这就是最终对我有用的东西

db.User.find({
   "purchasedata.orderid" : {
      $exists : true,
      $ne : ""
   }
});

@Christian p was close so I up voted, but I think my problem was the value wasn't null but actually a empty string, also checking if the value exists is important

@Christian p 很接近所以我投了票,但我认为我的问题是该值不是 null 而实际上是一个空字符串,检查该值是否存在也很重要