Mongodb组嵌入对象
我有这个收藏:Mongodb组嵌入对象,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有这个收藏: { "_id" : "1", "productGroup" : "G1", "product" : { "oldPrice" : 1.22, "newPrice" : 1.33 } }, { "_id" : "2", "productGroup" : "G1", "product" : { "oldPrice" : 1.12, "newPrice" : 1.23 } } 我想按productGroup分组,只保留具有minne
{
"_id" : "1",
"productGroup" : "G1",
"product" : {
"oldPrice" : 1.22,
"newPrice" : 1.33
}
},
{
"_id" : "2",
"productGroup" : "G1",
"product" : {
"oldPrice" : 1.12,
"newPrice" : 1.23
}
}
我想按productGroup分组,只保留具有minnewPrice
在我的情况下是这样的
{
"_id" : "G1",
"product" : {
"oldPrice" : 1.12,
"newPrice" : 1.23
}
}
如果我这样做,我只会得到newPrice属性,而我需要整个产品嵌入对象
db.myColl.aggregate([{
"$group" : {
_id : "$productGroup",
product : { "$min" : "$product.newPrice"}
}
}])
有没有关于如何做到这一点的想法?在聚合管道中使用,并获得在每个组中获得最低新价格的预期结果
db.myColl.aggregate([
{$sort: {"product.newPrice":1}},
{$group: {_id:"$productGroup", product:{$first:"$product"}}}
]);