Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
非理想嵌套文档的mongodb$展开_Mongodb_Aggregation Framework_Pymongo - Fatal编程技术网

非理想嵌套文档的mongodb$展开

非理想嵌套文档的mongodb$展开,mongodb,aggregation-framework,pymongo,Mongodb,Aggregation Framework,Pymongo,我知道下面的mongodb文档可能不是一个理想的结构,但是有没有办法解开$rounds.round_值 我试过使用聚合([{“$unwind”:“$rounds”}]),或者“$rounds.round\u值”,但这似乎不起作用。如有任何建议,我们将不胜感激 { "_id" : ObjectId("60bea750c26a1c7095387d00"), "rounds" : [ {

我知道下面的mongodb文档可能不是一个理想的结构,但是有没有办法解开
$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
    合并根目录中的必填字段和
    舍入值
    对象
  • $replaceRoot
    将上述合并对象替换为root

确切的问题是什么?它工作得很好,我明白了。是的,这是正确的,它将展开它,但我如何将整个结构展平,以便直接在顶层显示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"
        ]
      }
    }
  }
])