我有以下数据:
{"ID" : "334", "Q1": "aaaaaaa", "Q2": "bbbbbbbbb"},
{"ID" : "335", "Q1": "cccccc", "Q2": "ddddddd"},
并希望将字段组合成结果:
{"ID" : "334", "Q": "Q1", "Comment": "aaaaaaaa"},
{"ID" : "334", "Q": "Q2", "Comment": "bbbbbbbb"},
{"ID" : "335", "Q": "Q1", "Comment": "cccccccc"},
{"ID" : "334", "Q": "Q2", "Comment": "dddddddd"}
答案 0 :(得分:0)
db.getCollection('test1').aggregate(
[
{
$project: {
_id: false,
id: "$ID",
comment : [{"id": {"$literal":"Q1"} , "text": "$Q1"}, {"id": {"$literal":"Q2"}, "text": "$Q2"}] } },
{
$unwind : "$comment"
},
{$match : {comment.text : {$ne: null}}
}
]
)