本文介绍了非理想嵌套文档的 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
游乐场
更动态的方法,
游乐场
更多推荐
非理想嵌套文档的 mongodb $unwind
发布评论