【问题标题】:Mongodb query to check if a particular field is not null ( aggregation)Mongodb查询以检查特定字段是否不为空(聚合)
【发布时间】:2021-03-17 16:28:23
【问题描述】:

我有类似的疑问

{
"title": "sjncx",
"desciption": "cknxk jckd",
"price": "29.99",
"stock": "3",
...
}

如果标题is not empty.(标题存在且不为空),我需要过滤数据。还有is empty 喜欢~title: ""~(所以标题存在但为空)

我试过is not empty

{'title': {'$ne': 'null'}}

我试过is empty

{'title': {'$type': 10}}

这行不通。会是什么原因?

【问题讨论】:

    标签: python django mongodb mongodb-query


    【解决方案1】:

    您可以使用$in 覆盖空和空两种情况:

    {
      "title": {
        "$nin": [null, ""]
      }
    }
    

    试试here

    【讨论】:

      【解决方案2】:

      使用 $and 很有表现力

      db.collection.find({
        "$and": [
          {
            "title": {
              "$ne": null
            }
          },
          {
            "title": {
              "$ne": ""
            }
          }
        ]
      })
      

      您将在那里设置您的条件。 doc

      见:https://mongoplayground.net/p/V13mnmf6xGE

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-02
        • 2020-03-09
        相关资源
        最近更新 更多