Mongoose 在嵌套对象上找到一个 [英] Mongoose Find One on Nested Object

查看:51
本文介绍了Mongoose 在嵌套对象上找到一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从嵌套在 Mongo 对象中的对象获取信息.数据结构如下:

I'm trying to get info from an object that is nested within an object in Mongo. The data structure looks like this:

Card{
    _id;
    contributors: [
        {
            name;
            _id;
        },
        {
            name;
            _id;
        }
    ]
}

这是我尝试访问贡献者"数组中的特定贡献者".

Here is my attempt at accessing a specific 'contributor' in the 'contributors' array.

Card.findOne({_id: cardId, "contributor._id": contributorId},
    (err, contributor) => {
        if (err) {
            console.log(err);
            res.status(500);
            res.send({status: "error", message: "sass overload"});
            return;
        }
    console.log(contributor);
    res.send(contributor);
});

推荐答案

您需要使用 "contributors._id" 而不是 "contributor._id"

You need to use "contributors._id" not "contributor._id"

模型中字段的名称是 contributors 而不是 contributor.很明显,但需要注意.

The name of the field in your Model is contributors not contributor. Quite obvious, yet, to be noticed.

这篇关于Mongoose 在嵌套对象上找到一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆