【发布时间】:2017-02-19 11:53:57
【问题描述】:
在我的一个集合中执行了几个聚合步骤(管道步骤)后,我得到了以下结果:
{
"_id" : ObjectId("574e7722bffe901713d383bb"),
"eventname" : "Ball Passed",
"command" : {
"_id" : ObjectId("57ec6b6f6c61e919b578fe7c"),
"name" : "Run",
"strike" : 15,
"score" : true,
"duration" : 123
}
}
{
"_id" : ObjectId("57ec6b6f6c61e919b578ff8a"),
"eventname" : "Ball Passed",
"command" : {
"_id" : ObjectId("573d688d080cc2cbe8aecbbc"),
"name" : "Run",
"strike" : 12,
"score" : false,
"duration" : 597
}
}
这很好!
但是,在聚合的下一步中,我希望得到以下结果:
{
"_id" : ObjectId("57ec6b6f6c61e919b578fe7c"),
"name" : "Run",
"strike" : 15,
"duration" : 123
}
{
"_id" : ObjectId("573d688d080cc2cbe8aecbbc"),
"name" : "Run",
"strike" : 12,
"duration" : 597
}
如果你注意到了,command 字段应该成为顶级文档,command.score 应该被跳过。
如何一步完成?如果这在一个步骤中是不可能的,那么在多个步骤中呢?我想我必须使用$project?
【问题讨论】:
标签: mongodb mongodb-query