search

Home  >  Q&A  >  body text

mongodb group的finalize方法里怎么针对查询结果进一步过滤

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
},
怪我咯怪我咯2807 days ago591

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-24 09:15:14

    This can be achieved using aggregate’s match attribute

    reply
    0
  • Cancelreply