2
我的MongoDB集合這樣MongoDB中的GroupBy
{
"_id" : "EkKTRrpH4FY9AuRLj",
"stage" : 10,
},
{
"_id" : "EkKTRrpH4FY9AuRLj",
"stage" : 10,
},
{
"_id" : "EkKTRrpH4FY9AuRLj",
"stage" : 20,
}
而且我希望將它
stage: 10 in paid
stage: 20 in unpaid
結果應該是這樣的
{
"paid": [
{
"_id" : "EkKTRrpH4FY9AuRLj",
"stage" : 10,
},
{
"_id" : "EkKTRrpH4FY9AuRLj",
"stage" : 10,
}
],
"unpaid": [
{
"_id" : "EkKTRrpH4FY9AuRLj",
"stage" : 20,
}
]
}
我怎麼能去這?
由於它的工作。如果我想要整個對象,我應該通過$ cond中的每個鍵或是否有任何選項 – sumair
您可以選擇['「$$ ROOT」'](https://docs.mongodb.com/v3.0 /reference/aggregation-variables/#variable.ROOT)系統變量''$ cond':[ {「$ eq」:[「$ stage」,20]}, $$ ROOT, false ]'但會不要個人推薦,因爲必須在結果文檔不超過[BSON文檔大小限制](https://docs.mongodb.com/v3.0/reference/limits/#BSON-Document-Size)時使用它。 – chridam