将文本字段上的匹配与聚合相结合 [英] Combining match on text field with aggregate
问题描述
运行以下语句:
db.users.aggregate(
[
{ $match : { "confirmed" : {$eq: "true" } } },
{ $group : { _id : "$email", email: { $push: "$email" } } }
]
)
给我零结果,虽然以下一个工作得非常好,给出~12k:
Gives me zero results, although the following one works perfectly fine giving ~12k:
db.getCollection('users').find({confirmed:"true"})
问题:如何正确组合匹配和分组?我基本上是在寻找 SELECT email FROM users GROUP BY email
Question: how do I combined match and group properly? I am basically looking for a SELECT email FROM users GROUP BY email
附加: SELECT email, count(*) FROM users GROUP BY email
将是我的长期目标,但我似乎无法理解mongodb :(
Add on: SELECT email, count(*) FROM users GROUP BY email
would be my long term goal, but I can't seem to get my head around mongodb :(
推荐答案
db.users.aggregate(
[
{ $match : { "confirmed" : "true" } },
{ $group : { _id : "$email",count:{$sum:1} } }
]
)
`请检查 true 是否为布尔值,如果布尔值不需要将其检查为字符串,它将为您提供 0 文档.只是确认:真实
` please check if true is boolean if boolean than no need to check it as a string it will give you 0 document.just Confirmed:true
这篇关于将文本字段上的匹配与聚合相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!