非理想嵌套文档的mongodb$展开
我知道下面的mongodb文档可能不是一个理想的结构,但是有没有办法解开非理想嵌套文档的mongodb$展开,mongodb,aggregation-framework,pymongo,Mongodb,Aggregation Framework,Pymongo,我知道下面的mongodb文档可能不是一个理想的结构,但是有没有办法解开$rounds.round_值 我试过使用聚合([{“$unwind”:“$rounds”}]),或者“$rounds.round\u值”,但这似乎不起作用。如有任何建议,我们将不胜感激 { "_id" : ObjectId("60bea750c26a1c7095387d00"), "rounds" : [ {
$rounds.round_值
我试过使用聚合([{“$unwind”:“$rounds”}])
,或者“$rounds.round\u值”
,但这似乎不起作用。如有任何建议,我们将不胜感激
{
"_id" : ObjectId("60bea750c26a1c7095387d00"),
"rounds" : [
{
"round_number" : "0",
"round_values" : {
"max_player_pot" : "0.25",
"pot_multiple" : "0.625",
},
{
"round_number" : "1",
"round_values" : {
"max_player_pot" : "0.0",
"pot_multiple" : "0.0",
}
],
"GameID" : "392124717",
"ComputerName" : "awdfadf",
}
预期产出:
{
"max_player_pot" : "0.25",
"pot_multiple" : "0.625",
"GameID" : "392124717",
"ComputerName" : "awdfadf",
},
{
"max_player_pot" : "0.0",
"pot_multiple" : "0.0",
"GameID" : "392124717",
"ComputerName" : "awdfadf",
}
解构轮阵列$unwind
显示必填字段$project
更具活力的方法
合并根目录中的必填字段和$mergeObjects
对象舍入值
将上述合并对象替换为root$replaceRoot
确切的问题是什么?它工作得很好,我明白了。是的,这是正确的,它将展开它,但我如何将整个结构展平,以便直接在顶层显示round_值?能否显示您的预期结果以获得更清晰的图片?编辑问题以澄清这一点很好,但我有大约300个条目。我能不能避免把它们都打出来?谢谢,我已经用第二个解决方案更新了答案。
db.collection.aggregate([
{ $unwind: "$rounds" },
{
$project: {
GameID: 1,
ComputerName: 1,
max_player_pot: "$rounds.round_values.max_player_pot",
pot_multiple: "$rounds.round_values.pot_multiple"
}
}
])
db.collection.aggregate([
{ $unwind: "$rounds" },
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
{
GameID: "$GameID",
ComputerName: "$ComputerName"
},
"$rounds.round_values"
]
}
}
}
])