在我的一个集合中进行了几个聚合步骤后,最终结果如下:
{
"_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
?
$project
可以排除字段:https://docs.mongodb.com/manual/reference/operator/aggregation/project/#exclude-fields - SteveB