我讨厌这种问题,但也许你可以指出我很明显。我正在使用 Mongo 2.2.2。
我有一个包含 6M 文档的集合(在副本集中),其中包含名为username的字符串字段,我有索引。该索引不是唯一的,但最近我使它成为唯一的。突然以下查询给了我错误的警报,我有重复。
db.users.aggregate(
{ $group : {_id : "$username", total : { $sum : 1 } } },
{ $match : { total : { $gte : 2 } } },
{ $sort : {total : -1} } );
返回
{
"result" : [
{
"_id" : "davidbeges",
"total" : 2
},
{
"_id" : "jesusantonio",
"total" : 2
},
{
"_id" : "elesitasweet",
"total" : 2
},
{
"_id" : "theschoolofbmx",
"total" : 2
},
{
"_id" : "longflight",
"total" : 2
},
{
"_id" : "thenotoriouscma",
"total" : 2
}
],
"ok" : 1
}
我用很少的文档在样本集合上测试了这个查询,它按预期工作。