我有这样的查询结果;
{
"content" : [
"X",
"1"
]
}
我的查询就是这样;
db.getCollection("x").aggregate([
{$group : {_id : null, tN: {$addToSet : { content: [ "$tN", "$pC" ] }}}},
{$unwind: "$tN"},
{$replaceRoot: {newRoot: '$tN'}}
])
如何将 content 数组转换为
{
"tN" : "X",
"pC" : "1"
}
答案 0 :(得分:0)
您可以在$arrayElemAt阶段使用$project来获得它
db.getCollection("x").aggregate([
{
$group: {
_id: null,
tN: { $addToSet: { content: [ "$tN", "$pC" ] } }
}
},
{
$unwind: "$tN"
},
{
$replaceRoot: { newRoot: "$tN" }
},
{
$project: {
_id: 0,
"tN": {
$arrayElemAt: [ "$content", 0 ]
},
"pC": {
$arrayElemAt: [ "$content", 1 ]
}
}
}
])