【问题标题】:how to retrieve object value from mongoose如何从猫鼬中检索对象值
【发布时间】:2017-12-20 07:25:23
【问题描述】:

对象看起来像这样:

 {
            "_id" : ObjectId("5969b38a9ab0c60370a6dea3"),
            "name" : "789",
            "dec" : "789",
            "regular" : "789",
            "sale" : "78",
            "stock" : "8",
            "cat" : {
                    "wallets" : [{
                                    "selected" : "true",
                                    "subcategory" : "leather"
                            }]
            },
            "imgs" : [
                    "file-1500099465305.png",
                    "file-1500099465310.png"
            ],
            "__v" : 0
    }

我的问题是

如何通过查询在 mongodb 中检索特定值?

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

导入您的模型并通过查询找到您的实例:

    export function myFunc(req, res) {
          return Model.find({ 'name.last': 'Ghost' }}  //your condition
            })
            .then(entity=> {
              if (entity) {
                console.log(entity)                   // to see object
                res.status(200).json({'entity': entity}).end();
              } else {
                return res.status(404).end();  
              }
            }).catch(handleError(res));             // error handling
        }

现在,当您调用此函数时,您将获得实体作为对象,您可以从中获取实体对象键。

【讨论】:

    猜你喜欢
    • 2021-05-13
    • 2016-07-15
    • 2016-01-20
    • 2021-10-28
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多