python - MongoDB:如何确定特定值不存在

标签 python mongodb

如果特定的Key不存在,我只能找例子。

我的 MongoDB 实例中的特定文档具有如下字段:

        "actions" : [
            {
                    "date" : "2015-03-09T15:28:03Z",
                    "reason" : "",
                    "begin_date" : "2015-03-09T15:28:03Z",
                    "end_date" : "2015-03-09T15:28:03Z",
                    "action_type" : "Block",
                    "performed_date" : "2015-03-09T15:28:03Z",
                    "active" : "on",
            },
            {
                    "date" : "2015-03-09T15:28:03Z",
                    "reason" : "",
                    "begin_date" : "2015-03-09T15:28:03Z",
                    "end_date" : "2015-03-09T15:28:03Z",
                    "action_type" : "Alert",
                    "performed_date" : "2015-03-09T15:28:03Z",
                    "active" : "on",
            },
            {
                    "date" : "2015-03-09T15:28:03Z",
                    "reason" : "None",
                    "begin_date" : "2015-03-09T15:28:03Z",
                    "end_date" : "2015-03-09T15:28:03Z",
                    "action_type" : "History",
                    "performed_date" : "2015-03-09T15:28:03Z",
                    "active" : "on",
            }
    ],

现在,我正在尝试生成一个查询以返回“action_type”:“History”不存在的所有记录。

我知道 $exists 运算符可以告诉我“action_type”键是否不存在,但我找不到任何示例来告诉我在哪里可以指定不存在的值。

在我的脑海里我想说的是这样的(但显然这是行不通的):

db.collection.find({ "actions":{"$elemMatch":{"action_type": "History"}} : { '$exists': false} } )

希望这比我想象的要容易得多。

最佳答案

这应该有效:

db.collection.find({"actions.action_type": {"$ne": "History"}})

关于python - MongoDB:如何确定特定值不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942516/

相关文章:

python - .Bat 到 .Py 重写

python - SQLAlchemy 和连接,我们没有外键

java - Mongo JSON 到 Java POJO 映射

文档不在数组中的mongodb默认值

c++ - 如何在 Visual Studio 中使用 mongodb-cxx-driver 设置项目

python - 连接两个列表中相同索引处的字符串

Python numpy statsmodels OLS回归具体值

python - 如何在 django 表单的查询集上使用切片?

node.js - 如何使用 MongoDB Node 驱动程序通过 id 查找?

javascript - 基于变量的动态 $in 运算符