如果特定的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/