javascript - Mongodb查询: search regex in nested array

标签 javascript mongodb mongodb-query

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

{
...
"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/

相关文章:

javascript - 连接没有索引 0 的 JSON 数组对象未定义

mongodb - 如何规范化/减少 mongoDB 中的时间数据?

mongodb - 使用 Spring 和 MongoRepository 使用注解更新查询

python - 在 MongoDB 中重用 Cursor 对象

database - MongoDB 如何从$lookup 远程集合中$project(限制字段)?

javascript - 使用替换 JavaScript

javascript - 谷歌翻译参数表

javascript - 有没有办法放大 D3 力布局图?

python - pymongo : delete records elegantly

node.js - NodeJS 和 MongoDB - 根据 id 数组获取对象