【发布时间】:2021-04-18 04:12:49
【问题描述】:
在我的用户集合中,我有以下内容
{
_id: ObjectId('whatever user id'),
movies: [
{
_id: ObjectId('whatever id of this movie'),
name: 'name of this movie',
actors: [
{
_id: ObjectId('whatever id of this actor'),
name: 'name of this actor'
}
]
}
]
}
所以在我的用户集合中,我希望能够通过user.id、pet.id 和actor.id 查询演员
我想像这样返回演员......
actor: {
fields...
}
我尝试了以下...
const actor = await User.findById(req.user.id, {
movies: {
$elemMatch: {
_id: req.params.movie_id,
actors: {
$elemMatch: {
_id: req.params.actor_id,
},
},
},
},
});
我尝试了其他方法,但似乎无法正常工作。我看到您可以使用聚合,但我不知道如何在使用我可以使用的 ids 进行查询。
【问题讨论】:
标签: arrays mongodb mongoose nested nodes