如何在填充函数中使用$ elemMatch

问题描述

嗨,我该如何在填充中使用elemMatch?

picture.findById(pictureId).populate(
{
        path: 'comments',$elemMatch: { "likes": user.id },populate: {
            path: "user",select: ["username","thumbnail"]
        }
    }
)

我尝试了这个但没用

解决方法

使用match代替$ elemMatch

https://mongoosejs.com/docs/api.html#query_Query-populate

picture.findById(pictureId).populate(
{
        path: 'comments',match: { "likes": user.id },populate: {
            path: "user",select: ["username","thumbnail"]
        }
    }
)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...