db.TruckUsers.group({
key:{PhoneNumber:true},
initial:{Count:0},
reduce:function(doc,prev)
{
if (!prev.PhoneNumber)
{
prev.PhoneNumber = doc.PhoneNumber;
prev.Count = 1;
}
else if(doc.PhoneNumber == prev.PhoneNumber)
{
prev.Count++;
}
},
finalize:function(doc)
{
for(i in doc.Count)
{
if (doc.Count > 1)
{
doc.NewPhoneNumber = doc.PhoneNumber;
doc.NewCount = doc.Count;
}
}
delete doc.PhoneNumber;
delete doc.Count;
}
})
我想只显示Count>1的记录,但不知道如何下手,现在的这个结果会把所有的记录都会显示出来,如下:
"28" : {},
"29" : {},
"30" : {},
"31" : {},
"32" : {},
"33" : {},
"34" : {},
"35" : {},
"36" : {},
"37" : {},
"38" : {},
"39" : {},
"40" : {},
"41" : {
"NewPhoneNumber" : "18217039***",
"NewCount" : 3
},
This can be achieved using aggregate’s match attribute