gpt4 book ai didi

mongodb - 在 mongodb 中查找字段的所有非不同值

转载 作者:IT老高 更新时间:2023-10-28 13:09:09 25 4
gpt4 key购买 nike

如何在 mongodb 的集合中列出一个字段的所有非不同值?我找到了 distinct 命令来查找该字段的所有不同值,但我想要相反。

最佳答案

您可以使用 .aggregate() 来执行此操作

db.collection.aggregate([
{ "$group": {
"_id": "$field",
"count": { "$sum": 1 }
}},
{ "$match": {
"count": { "$gt": 1 }
}}
])

另见 SQL to Aggregate Mapping例子。

关于mongodb - 在 mongodb 中查找字段的所有非不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740509/

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