Mongo Query:很多要搜索的参数。无法弄清

时间:2018-09-09 13:47:16

标签: node.js mongodb

我所拥有的:

  1. 公司名称列表:[宝马,塔塔,法拉利...]
  2. 传输:手动或自动3。
  3. 汽车颜色:红色或白色等
  4. 车型年份:2010年至2014年
  5. 最低价格和最高价格。 ->价格范围

============================================= ==============================

我现在可以做什么:如果我只有一个对象(即companies中的一家公司),那么我会做类似的事情:

query = {{transmission_type : transmission}, {color : car_color}, {year : model_year}, { $range: [ 0, "$PriceInINR", 25 ] }  }

db.companies.cars.find(query)

但是这里首先有很多公司,然后每个公司都有汽车列表。

我该如何进行查询

  1. 我想要一个考虑价格范围的查询。

  2. 和另一个没有价格范围条件的商品。

早期想法:我认为我可以分别查询每个公司,因为我知道公司名称。因此单独查找结果,然后将其推入数组。

还有其他建议,我该怎么做?

也尝试过此操作,但似乎不起作用,请帮助

db.companies.aggregate([ 

  { $match: { company_name: { $in: ["gmc"] } } },

  { $unwind: "$cars" },

  { $replaceRoot: { newRoot: "$cars" } },

  {

    $match: {

      transmission_type: "Automatic",

      color: "Metallic White",

      year: 2015,

      price: { $gt: 0, $lt: 99999999 }

    }

  }

])

============================================= ==============================

companies集合的伪结构

Companies {
    bmw: [
            car1 {
                Model,  Year, PriceInINR,  Trim,
                Engine, Body,color ,transmittion_type;
                dealer - id_2: <<
            },
            car2 {
                Model,  Year, PriceInINR,  Trim,
                Engine, Body,color ,transmittion_type;
                dealer - id_2: <<

            },car3 {
                Model,  Year, PriceInINR,  Trim,
                Engine, Body,color ,transmittion_type;
                dealer - id_2: <<

            }
        ],

    jaguar: [
            car1 {
                Model,  Year, PriceInINR,  Trim,
                Engine, Body,color ,transmittion_type;
                dealer - id_2: <<

            },
            car2 {
                Model,  Year, PriceInINR,  Trim,
                Engine, Body,color ,transmittion_type;
                dealer - id_2: <<

            }
          ],
    ferrari [
            {-- -- --}
            {-- -- --}
            {-- -- --} 
        ]   
}

============================================= ============================== companies集合在mongo db中的实际结构

{
        "_id" : ObjectId("5b8ef8b78cc390cca71aa0e5"),
        "company_location" : "USA",
        "company_name" : "buick",
        "__v" : 0,
        "cars" : [
                {
                        "_id" : ObjectId("5b8ef8b6d1a7c2156417de56"),
                        "model" : "ENCLAVE",
                        "year" : 2014,
                        "PriceInINR" : 2537993,
                        "trim" : "Leather FWD",
                        "engine" : "SPORT UTILITY 4-DR",
                        "body" : "3.6L V6 DOHC 24V",
                        "color" : "Silver",
                        "transmission_type" : "Manual",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef8b6d1a7c2156417de5d"),
                        "model" : "LaCrosse",
                        "year" : 2011,
                        "PriceInINR" : 4677427,
                        "trim" : "CXL FWD",
                        "engine" : "SEDAN 4-DR",
                        "body" : "3.6L V6 DOHC 24V",
                        "color" : "Grey",
                        "transmission_type" : "Automatic",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef8b7d1a7c2156417de8e"),
                        "model" : "ENCORE",
                        "year" : 2013,
                        "PriceInINR" : 4808616,
                        "trim" : "Leather FWD",
                        "engine" : "SPORT UTILITY 4-DR",
                        "body" : "1.4L L4 DOHC 16V TURBO",
                        "color" : "Yellow",
                        "transmission_type" : "Manual",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef8b7d1a7c2156417dece"),
                        "model" : "LaCrosse",
                        "year" : 2011,
                        "PriceInINR" : 868875,
                        "trim" : "CXL FWD",
                        "engine" : "SEDAN 4-DR",
                        "body" : "2.4L L4 DOHC 16V",
                        "color" : "Grey",
                        "transmission_type" : "Automatic",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef9c1f0412315aa07b65a"),
                        "model" : "VERANO",
                        "year" : 2013,
                        "PriceInINR" : 4380113,
                        "trim" : "Base",
                        "engine" : "SEDAN 4-DR",
                        "body" : "2.4L L4 DOHC 16V FFV",
                        "color" : "Metallic White",
                        "transmission_type" : "Automatic",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                }
        ]
}
{
        "_id" : ObjectId("5b8ef8b78cc390cca71aa0e7"),
        "company_location" : "USA",
        "company_name" : "gmc",
        "__v" : 0,
        "cars" : [
                {
                        "_id" : ObjectId("5b8ef8b6d1a7c2156417de57"),
                        "model" : "TERRAIN",
                        "year" : 2013,
                        "PriceInINR" : 3851710,
                        "trim" : "SLE2 FWD",
                        "engine" : "SPORT UTILITY 4-DR",
                        "body" : "2.4L L4 DOHC 16V FFV",
                        "color" : "Yellow",
                        "transmission_type" : "Manual",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef8b6d1a7c2156417de5b"),
                        "model" : "YUKON",
                        "year" : 2015,
                        "PriceInINR" : 3129397,
                        "trim" : "SLE 2WD",
                        "engine" : "SPORT UTILITY 4-DR",
                        "body" : "5.3L V8 OHV 16V",
                        "color" : "Silver",
                        "transmission_type" : "Manual",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },            
                {
                        "_id" : ObjectId("5b8ef9c1f0412315aa07b659"),
                        "model" : "SIERRA 1500",
                        "year" : 2014,
                        "PriceInINR" : 3649025,
                        "trim" : "SLE Crew Cab 2WD",
                        "engine" : "CREW CAB PICKUP 4-DR",
                        "body" : "5.3L V8 OHV 16V",
                        "color" : "Metallic White",
                        "transmission_type" : "Automatic",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef9c1f0412315aa07b666"),
                        "model" : "TERRAIN",
                        "year" : 2012,
                        "PriceInINR" : 1896832,
                        "trim" : "SLT1 FWD",
                        "engine" : "SPORT UTILITY 4-DR",
                        "body" : "3.0L V6 DOHC 24V",
                        "color" : "Metallic White",
                        "transmission_type" : "Automatic",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                },
                {
                        "_id" : ObjectId("5b8ef9c1f0412315aa07b650"),
                        "model" : "ACADIA",
                        "year" : 2012,
                        "PriceInINR" : 2541355,
                        "trim" : "Denali AWD",
                        "engine" : "SPORT UTILITY 4-DR",
                        "body" : "3.6L V6 DOHC 24V",
                        "color" : "Metallic White",
                        "transmission_type" : "Automatic",
                        "dealer_id" : "5b8ee03ffe42df0d94de785d"
                }
        ]
}

0 个答案:

没有答案