【发布时间】:2021-04-07 05:36:02
【问题描述】:
我有一个包含数组子文档的文档
我需要通过单个查询搜索文档和子文档
我只需要子文档数组数据的结果,通过条件
样本数据 [2 个文档]
{
_id : ObjectId("512e28984815cbfcb21646a7"),
name: David,
list: [
{
sport: basketball,
score: 100
},
{
sport: cricket,
score: 30
}
{
sport: rugby,
score: 100
}
]
},
{
_id : ObjectId("879e28664815cbfcb21622g9"),
name: Shawn,
list: [
{
sport: basketball,
score: 100
},
{
sport: cricket,
score: 50
}
{
sport: rugby,
score: 20
}
]
}
预期结果
大卫得分为 100 的游戏列表
- 文档查询name = David
- 子文档查询score = 100
响应:[篮球、橄榄球]
查询已尝试,但结果为 NULL
findOne({name:'David', "list.score": 100})
【问题讨论】:
-
试试
findOne({name:'David', list: { $elemMatch: {score: 100} }) -
@WernfriedDomscheit 这给了我整个文档.. 列表也包含 !=100 分
标签: database mongodb mongoose subdocument