猫鼬-查找匹配的数组值

时间:2019-03-26 19:19:01

标签: node.js mongodb rest api mongoose

我正在使用Mongoose使用Node.js创建一个REST API,以访问我的后端MongoDB数据库。我的一个收藏夹(也包含变体)中有一个数组(变体),需要检索数组中与提供的值匹配的对象。

到目前为止,我已经获得了返回空值的值...

//Get variation by ID
app.get('/v1/:id', async (request, response) => {
    try {
        var result = await variationsModel.find({ 'variationID': request.params.id }).exec();
        response.send(result);
    } catch (error) {
        response.status(500).send(error);
    }
})

这是我在API中定义的模型...

const variationsModel = mongoose.model("variations", {
    "variations": [
        {
        "variationID": String,
        "custID": String,
        "projID": String,
        "variationTitle": String,
        "variationDesc": String,
        "variationStatus": String,
        "variationChargeable": String,
        "variationCost": String,
        "requireMaterial": String,
        "variationRequestor": String,
        "variationCreationDate": String,
        "variationImages": [
            {
            "imageId": String
            }
        ],
        "variationCategory": String
        }
    ]
});

有人指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

查询应为

variationsModel.find({ 'variations.variationID': request.params.id }).exec();