Mongo / Node JS如何查询对象数组

时间:2019-12-03 08:37:42

标签: javascript node.js mongodb

Mongo / node js如何查询对象数组。我们如何查询下面的ff数据。我想从与查询匹配的数据库中查询数据。我提供了以下所有数据,包括我的代码。谢谢。有任何想法吗 ?将不胜感激。谢谢

查询参数

query { Year: { '$in': 2007 },
  Make: { '$in': 'Honda' },
  Model: { '$in': 'Piloto' },
  Body: { '$in': 'Sport Utility' } }

查询

PriceRule.find({ data : query })
            .then(rules => {
                console.log("Data Result :" , rules)
                res.send(rules);
            }).catch(err => {
                res.status(500).send({
                    message: err.message || "Some error occurred while retrieving price rules."
                });
            });

数据库中的数据

[
    {
        "_id": "5de60cdbe5e5a52760d6aaf8",
        "name": "ada",
        "summer_sale_value": "1000",
        "description": "dasdsa",
        "__v": 0,
        "data": [
            {
                "_id": "5de60cdbe5e5a52760d6aaf9",
                "body": null,
                "model": null,
                "year": [
                    "2007"
                ],
                "make": null
            }
        ]
    },
    {
        "_id": "5de611c30f0c2548c4d60f32",
        "name": "23",
        "summer_sale_value": null,
        "description": "1212321",
        "__v": 0,
        "data": [
            {
                "_id": "5de611c30f0c2548c4d60f33",
                "body": null,
                "model": null,
                "year": [
                    "2007"
                ],
                "make": null
            }
        ]
    },
    {
        "_id": "5de61be7af76ca3efc82beec",
        "name": "dasd",
        "summer_sale_value": null,
        "description": "sadsad",
        "__v": 0,
        "data": [
            {
                "_id": "5de61be7af76ca3efc82beed",
                "body": null,
                "model": null,
                "year": [
                    "2009"
                ],
                "make": null
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

我相信查询应如下所示:

PriceRule.find({
  "data.year" : "2017",
  "data.body" : "Sport Utility",
  "data.model" : "Piloto",
  "data.make" : "Honda",
});

记住,这只是演示如何实现它。