MongoDB多个逐字输入单个字段以进行注释

时间:2016-09-26 13:31:14

标签: mongodb

我有以下数据:

{"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"}

1 个答案:

答案 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}}   
}
]
)