【问题标题】:Check that Field Exists with MongoDB使用 MongoDB 检查字段是否存在
【发布时间】:2013-11-20 23:48:09
【问题描述】:

所以我正在尝试查找具有字段集且不为空的所有记录。

我尝试使用$exists,但是根据MongoDB documentation,,此查询将返回等于null的字段。

$exists 匹配包含存储空值字段的文档。

所以我现在假设我必须做这样的事情:

db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })

然而,每当我尝试这个时,我都会收到错误[invalid use of $not] 任何人都知道如何查询这个?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    使用$ne(表示“不等于”)

    db.collection.find({ "fieldToCheck": { $exists: true, $ne: null } })
    

    【讨论】:

    • 这会返回什么?一个空集合?单品?一个数组?
    • @iLoveUnicorns:find 总是返回:符合条件的记录集合。
    • @SergioTulentsev AFAIK 它返回一个光标
    • @fernandohur:是的,但是如果您的文档少于一页,您甚至不会看到差异。而且,如果您要从外部驱动程序运行此查询,我敢肯定它们中的大多数会保护您免受游标实现细节的影响。
    【解决方案2】:

    假设我们有一个如下的集合:

    { 
      "_id":"1234"
      "open":"Yes"
      "things":{
                 "paper":1234
                 "bottle":"Available"
                 "bottle_count":40
                } 
    }
    

    我们想知道瓶子字段是否存在?

    回答:

    db.products.find({"things.bottle":{"$exists":true}})
    

    【讨论】:

    • When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null. From the docs.
    • 是的,但我不明白为什么数据库会包含 null 值,这很草率
    【解决方案3】:

    我发现这对我有用

    db.getCollection('collectionName').findOne({"fieldName" : {$ne: null}})
    

    【讨论】:

      【解决方案4】:
      db.<COLLECTION NAME>.find({ "<FIELD NAME>": { $exists: true, $ne: null } })
      

      【讨论】:

        【解决方案5】:

        此评论写于2021,适用于MongoDB 5.X 及更早版本。

        如果您重视 查询性能 永远不要使用 $exists(或仅当您对所查询的字段有稀疏索引时才使用它。稀疏索引应该匹配查询条件,意思是,如果搜索 $exists:true,稀疏索引应该超过 field:{$exist:true} ,如果你正在查询 where $exists:true 稀疏索引应该在 field:{$exist:false}

        改为使用:

        db.collection.find({ "fieldToCheck": {  $ne: null } })
        

        db.collection.find({ "fieldToCheck": {  $eq: null } })
        

        这将要求您将 fieldToCheck 包含在集合的每个文档中,但是 - 性能将大大提高。

        【讨论】:

          【解决方案6】:

          我尝试将其转换为布尔条件,如果文档与 表名已经存在,那么它将附加在同一个文档中, 否则它会创建一个。

          table_name 是我用来查找文档的变量

          query = { table_name : {"$exists": "True"}}
              
              result = collection.find(query)
              flag = 0
              for doc in result:
                  collection.update_one({}, { "$push" : { table_name : {'name':'hello'} } } )
                  flag = 1
              if (flag == 0):
                  collection.insert_one({ table_name : {'roll no' : '20'}})
          

          【讨论】:

            【解决方案7】:

            汇总示例

            https://mongoplayground.net/p/edbKil4Zvwc

            db.collection.aggregate([
              {
                "$match": {
                  "finishedAt": {
                    "$exists": true
                  }
                }
              },
              {
                "$unwind": "$tags"
              },
              {
                "$match": {
                  "$or": [
                    {
                      "tags.name": "Singapore"
                    },
                    {
                      "tags.name": "ABC"
                    }
                  ]
                }
              },
              {
                "$group": {
                  "_id": null,
                  "count": {
                    "$sum": 1
                  }
                }
              }
            ])
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-08-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-09-09
              相关资源
              最近更新 更多