Heim  >  Fragen und Antworten  >  Hauptteil

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
},
怪我咯怪我咯2759 Tage vor544

Antworte allen(1)Ich werde antworten

  • 伊谢尔伦

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

    使用aggregate的match属性可以实现

    Antwort
    0
  • StornierenAntwort