gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:28:56 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com