gpt4 book ai didi

javascript - Mongodb查询: search regex in nested array

转载 作者:行者123 更新时间:2023-11-28 11:04:42 26 4
gpt4 key购买 nike

鉴于录制的游戏的以下文档结构:

{
...
"rightkingpercenthp":[1,1,1,1,1,1,1,1,1,1,1,0.94,0.95,0],
"gameDetails":[
{"playername":"Calvino",
...
"unitsPerWave":[
["grarl_unit_id:0.5|9.5"],
["hydra_unit_id:3.5|6.5","veteran_unit_id:1.5|6","grarl_unit_id:3.5|5.5","peewee_unit_id:8.5|7"], ["hydra_unit_id:3.5|6.5","veteran_unit_id:1.5|6","veteran_unit_id:8.5|7","peewee_unit_id:8.5|6",
"fatalizer_unit_id:2.5|5"],
...
]
}
我如何计算至少有一个游戏的数量,例如gameDetails.unitsPerWave 中的“veteran_unit_id”(忽略名称后的坐标)?

最佳答案

您可以使用嵌套 $elemMatch用于在嵌套 gamesDetails.unitsPerWave 数组中查询的运算符以及与字符串开头匹配的正则表达式:

db.col.count({
'gameDetails.unitsPerWave': {$elemMatch: {$elemMatch: {$regex: /^veteran_unit_id/}}}
})

关于javascript - Mongodb查询: search regex in nested array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54041822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com