假设我有一些包含一些文档的集合.这样的事情.
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":1, "name" : "foo"}
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":2, "name" : "bar"}
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":3, "name" : "baz"}
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":4, "name" : "foo"}
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":5, "name" : "bar"}
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":6, "name" : "bar"}
Run Code Online (Sandbox Code Playgroud)
我想通过"name"字段找到此集合中的所有重复条目.例如,"foo"出现两次,"bar"出现3次.
我创建了一个集合并添加了一个这样的唯一键
db.user_services.createIndex({"uid":1 , "sid": 1},{unique:true,dropDups: true})
Run Code Online (Sandbox Code Playgroud)
该集合看起来像这样的"user_services"
{
"_id" : ObjectId("55068b35f791c7f81000002d"),
"uid" : 15,
"sid" : 1,
"rate" : 5
},
{
"_id" : ObjectId("55068b35f791c7f81000002f"),
"uid" : 15,
"sid" : 1,
"rate" : 4
}
Run Code Online (Sandbox Code Playgroud)
问题:
我使用php驱动程序插入具有相同的uid和sid的文档,它正在插入.
我想要的是
我有一个包含如下文档的集合:
{
"_id" : ObjectId("55b377cb66b393427367c3e2"),
"comment" : "This is a comment",
"url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection
}
Run Code Online (Sandbox Code Playgroud)
我需要在此集合中找到包含注释和url_key重复值的记录。
我可以轻松地生成(使用汇总)相同,单个键(例如:注释)的重复记录,但是我不知道如何对多个关键字进行分组/汇总。
这是我当前的聚合管道:
db.comments.aggregate([ { $group: { _id: { comment: "$comment" }, uniqueIds: { $addToSet: "$_id" }, count: { $sum: 1 } } }, { $match: { count: { $gte: 2 } } }, { $sort: { count : -1} }, {$limit 10 } ]);
Run Code Online (Sandbox Code Playgroud) mongodb mongodb-query aggregation-framework mongodb-aggregation