gpt4 book ai didi

MongoDB 聚合 - $sum 参数

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

我有以下聚合查询:

{"$match": {"expired":{"$exists":False}}},
{"$group":{
"_id":"$retailer",
"average_price": {"$avg":"$price"},
"highest_price": {"$max":"$price"},
"lowest_price": {"$min":"$price"},
"online": {"$sum":1}
}}

我想通过计算有多少产品在促销来对此进行扩展。我试过了(这显然无效):

{"$match": {"expired":{"$exists":False}}},
{"$group":{
"_id":"$retailer",
"average_price": {"$avg":"$price"},
"highest_price": {"$max":"$price"},
"lowest_price": {"$min":"$price"},
"online": {"$sum":1},
"promomtion": {"$sum":{"promotion":True}},
}}

有没有办法从这个查询中计算这个促销计数?

最佳答案

解决了这个问题:

{"$match": {"expired":{"$exists":False}}},
{"$group":{
"_id":"$retailer",
"average_price": {"$avg":"$price"},
"highest_price": {"$max":"$price"},
"lowest_price": {"$min":"$price"},
"online": {"$sum":1},
"promotion_count": { "$sum": { "$cond": [ { "$eq": [ "$promotion", True ] }, 1,0 ] }}
}}

关于MongoDB 聚合 - $sum 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20498071/

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