所以我在MongoDB集合中有一个文档,如下所示:
{
"_id" : ObjectId("5a9d476d0361662cb469eb92"),
"layout_id" : 123,
"entries" : {
"sim_b" : {
"id" : "",
"resolution" : "",
"description" : "",
"data" : {
"heatmap" : [ ],
"seats" : [ ]
}
},
"sim_c" : {
"id" : "",
"resolution" : "",
"description" : "",
"data" : {
"heatmap" : [ ],
"seats" : [ ]
}
}
}
}
我只想选择heatmap
,以便文档如下所示:
{
"_id" : ObjectId("5a9d476d0361662cb469eb92"),
"entries" : {
"sim_b" : {
"data" : {
"heatmap" : [ ]
}
},
"sim_c" : {
"data" : {
"heatmap" : [ ]
}
}
}
}
有没有一种方法可以显示比db.collection.find({ 'layout_id': 123}, { 'entries.sim_b.data.heatmap': 1, 'entries.sim_c.data.heatmap': 1})
更好的查询?当entries
字典下有更多子文档时,会派上用场。
答案 0 :(得分:0)
如果投影不够,并且您希望将所有热图都放在某种聚合数组中,则可能必须使用聚合框架和$ push运算符(https://docs.mongodb.com/manual/reference/operator/aggregation/push/)或$ addToSet operator(https://docs.mongodb.com/manual/reference/operator/aggregation/addToSet/),取决于您想要达到的结果。