mongodb $unwind 用于非理想嵌套文档

编程入门 行业动态 更新时间:2024-10-24 18:23:13
本文介绍了mongodb $unwind 用于非理想嵌套文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道下面的 mongodb 文档可能不是一个理想的结构,但是有什么方法可以解开 $rounds.round_values?

I'm aware that the below mongodb document may not be in an ideal structure, but is there any way to unwind that $rounds.round_values?

我试过 aggregate([{"$unwind": "$rounds"}]) 或 "$rounds.round_values" 但是这似乎不起作用.非常感谢任何建议.

I've tried with aggregate([{"$unwind": "$rounds"}]), or "$rounds.round_values" but that doesn't seem to work. Any suggestions are greatly appreciated.

{ "_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 解构rounds数组
  • $project 显示必填字段
    • $unwind deconstruct the rounds array
    • $project to show required fields
    • 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" } } ])

      游乐场

      更动态的方法,

      • $mergeObjects 合并根和 round_values 对象中的必填字段
      • $replaceRoot 将上面的合并对象替换为根
      • $mergeObjects to merge required fields from root and round_values object
      • $replaceRoot to replace above merged object to root
      db.collection.aggregate([ { $unwind: "$rounds" }, { $replaceRoot: { newRoot: { $mergeObjects: [ { GameID: "$GameID", ComputerName: "$ComputerName" }, "$rounds.round_values" ] } } } ])

      游乐场

更多推荐

mongodb $unwind 用于非理想嵌套文档

本文发布于:2023-11-22 18:41:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   理想   文档   mongodb   unwind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!