首页 文章

你如何在Mongo中查询“is not null”?

提问于
浏览
266

我想执行以下查询:

db.mycollection.find(HAS IMAGE URL)

什么应该是正确的语法?

8 回答

  • 2

    这将使用名为“IMAGE URL”的键返回所有文档,但它们仍可能具有空值 .

    db.mycollection.find({"IMAGE URL":{$exists:true}});
    

    这将返回包含名为"IMAGE URL"的键和非空值的所有文档 .

    db.mycollection.find({"IMAGE URL":{$ne:null}});
    

    另外,根据文档,$ exists目前不能使用索引,但$ ne可以 .

    Edit: Adding some examples due to interest in this answer

    鉴于这些插入:

    db.test.insert({"num":1, "check":"check value"});
    db.test.insert({"num":2, "check":null});
    db.test.insert({"num":3});
    

    这将返回所有三个文件:

    db.test.find();
    

    这将仅返回第一个和第二个文档:

    db.test.find({"check":{$exists:true}});
    

    这将仅返回第一个文档:

    db.test.find({"check":{$ne:null}});
    

    这将仅返回第二个和第三个文档:

    db.test.find({"check":null})
    
  • 622

    在pymongo你可以使用:

    db.mycollection.find({"IMAGE URL":{"$ne":None}});
    

    因为pymongo将mongo“null”表示为python“None” .

  • 37

    db.collection_name.find({"filed_name":{$exists:true}});

    获取包含此filed_name的文档,即使它为null .

    My proposition:

    db.collection_name.find({"field_name":{$type:2}}) //type:2 == String
    

    您可以检查所需属性的类型,它将返回其field_name查询包含值的所有文档,因为您正在检查字段的类型否则如果它为null,则类型条件不匹配,因此不会返回任何内容 .

    N.b :如果field_name有一个空字符串,意思是“”,它将被返回 . 这与 db.collection_name.find({"filed_name":{$ne:null}}); 的行为相同

    Extra validation:

    好的,所以我们还没有完成,我们需要一个额外的条件 .

    db.collection_name. find({ "field_name":{$type:2},$where:"this.field_name.length >0"})

    要么

    db.collection_name. find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})

    所有类型的参考:https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type

  • 9

    一个班轮是最好的:

    db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });
    

    这里,

    mycollection :放置您想要的收藏品名称

    fieldname :放置您想要的字段名称

    解释:

    $exists :如果为true,则$ exists匹配包含该字段的文档,包括字段值为null的文档 . 如果为false,则查询仅返回不包含该字段的文档 .

    $ne 选择字段值不等于指定值的文档 . 这包括不包含该字段的文档 .

    因此,在您提供的情况下,查询将返回所有具有imageurl字段的文档并且不具有null值:

    db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });
    
  • 13

    分享未来的读者 .

    此查询适用于我们(查询从MongoDB compass执行):

    {
      "fieldName": {
        "$nin": [
          "",
          null
        ]
      }
    }
    
  • -4

    一个未提及的替代方案,但对于某些人来说可能是更有效的选项(不适用于NULL条目)是使用sparse index(索引中的条目仅在字段中存在某些内容时才存在) . 这是一个示例数据集:

    db.foo.find()
    { "_id" : ObjectId("544540b31b5cf91c4893eb94"), "imageUrl" : "http://example.com/foo.jpg" }
    { "_id" : ObjectId("544540ba1b5cf91c4893eb95"), "imageUrl" : "http://example.com/bar.jpg" }
    { "_id" : ObjectId("544540c51b5cf91c4893eb96"), "imageUrl" : "http://example.com/foo.png" }
    { "_id" : ObjectId("544540c91b5cf91c4893eb97"), "imageUrl" : "http://example.com/bar.png" }
    { "_id" : ObjectId("544540ed1b5cf91c4893eb98"), "otherField" : 1 }
    { "_id" : ObjectId("544540f11b5cf91c4893eb99"), "otherField" : 2 }
    

    现在,在imageUrl字段上创建稀疏索引:

    db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
    {
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
    }
    

    现在,总是有机会(特别是像我的样本那样的小数据集)而不是使用索引,MongoDB将使用表扫描,即使对于潜在的覆盖索引查询 . 事实证明,这给了我一个简单的方法来说明这里的区别:

    db.foo.find({}, {_id : 0, imageUrl : 1})
    { "imageUrl" : "http://example.com/foo.jpg" }
    { "imageUrl" : "http://example.com/bar.jpg" }
    { "imageUrl" : "http://example.com/foo.png" }
    { "imageUrl" : "http://example.com/bar.png" }
    {  }
    {  }
    

    好的,所以没有 imageUrl 的额外文件被退回,只是空的,不是我们想要的 . 只是为了确认原因,做一个解释:

    db.foo.find({}, {_id : 0, imageUrl : 1}).explain()
    {
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 6,
        "nscannedObjects" : 6,
        "nscanned" : 6,
        "nscannedObjectsAllPlans" : 6,
        "nscannedAllPlans" : 6,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "server" : "localhost:31100",
        "filterSet" : false
    }
    

    所以,是的, BasicCursor 等于表扫描,它没有使用索引 . 让我们强制查询使用带有 hint() 的稀疏索引:

    db.foo.find({}, {_id : 0, imageUrl : 1}).hint({imageUrl : 1})
    { "imageUrl" : "http://example.com/bar.jpg" }
    { "imageUrl" : "http://example.com/bar.png" }
    { "imageUrl" : "http://example.com/foo.jpg" }
    { "imageUrl" : "http://example.com/foo.png" }
    

    并且我们正在寻找结果 - 只返回填充了字段的文档 . 这也只使用索引(即它是一个覆盖的索引查询),因此只有索引需要在内存中才能返回结果 .

    这是一个专门的用例,一般不能使用(请参阅其他答案) . 特别应该注意的是,就目前而言,你不能以这种方式使用 count() (对于我的例子,它将返回6而不是4),所以请仅在适当时使用 .

  • 14
    db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})
    
  • 2

    查询将是

    db.mycollection.find({"IMAGE URL":{"$exists":"true"}})
    

    它将返回所有“IMAGE URL”作为键的文件...........

相关问题