gpt4 book ai didi

python - 构建where子句的mongodb查询问题

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

我是 mongoDB 的新手,我有以下查询:

jds=jd.aggregate( [
{
"$group": {
"_id": {"house_NAME":"$house_NAME"},
"count": { "$sum": 1 }
}
},
{ "$match": { "count": { "$gt": 0 } } }
] )

返回集合中每个房屋名称的计数。

我的收藏有点像下面:

record_id  house_NAME  status

1 Thomas Open
2 Panther Close
3 Thomas Close

我想要的是只返回状态为“打开”的值,我想在上面的查询中添加“和”子句,以便它只返回状态为“打开”的那些文档的计数。我不知道该怎么做。

我被卡住了。任何帮助将不胜感激!

提前致谢!

最佳答案

您可以添加 $match管道开始阶段

jds=jd.aggregate([
{ "$match": { "status": "Open" }},
{ "$group": {
"_id": { "house_NAME": "$house_NAME" },
"count": { "$sum": 1 }
}},
{ "$match": { "count": { "$gt": 0 }}}
])

关于python - 构建where子句的mongodb查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54710281/

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