我将聚合框架用于多个字段的分组依据
{
_id:{_id:"$_id",feature_type:"$feature_type",feature_name:"$feature_name"},
features: { $push: "$features" }
}
给出类似
的结果{_id:
{_id:1,feature_type:"Test",feature_name:"Tests"},
features:["23423","32423","2342342"]
}
但是我想要类似
的结果{_id:1,feature_type:"Test",feature_name:"Tests",
features:["23423","32423","2342342"]
}
我如何使用聚集框架来实现这一目标。
答案 0 :(得分:3)
您需要使用$replaceRoot
更改您的根文档
db.collection.aggregate([
{
"$addFields": {
"_id.features": "$features"
}
},
{
"$replaceRoot": {
"newRoot": "$_id"
}
}
])
答案 1 :(得分:1)
db.collection.aggregate([
{
$project: {
_id: "$_id._id",
feature_type:"$_id.feature_type",
feature_name:"$_id.feature_name",
features:1
}
}
])