MONGO:艰难的find()查询,提供了很好的解释

时间:2018-09-09 06:25:25

标签: node.js mongodb

enter image description here

我所拥有的:

  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. 和另一个没有价格范围条件的商品。

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

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

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

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"
                }
        ]
}

1 个答案:

答案 0 :(得分:0)

对于您的数据和所需的查询,我建议如下所示:

// I've tried to annotate without being too verbose
db.companies.aggregate([
  { $match: { company_name: { $in: [...] } } }, // match company names
  { $unwind: "$cars" },                         // separate cars array into distinct documents
  { $replaceRoot: { newRoot: "$cars" } },       // remove company information so it just has car information
  {
    $match: {                                   // match car information
      transmission_type: "",                    // any one of these can be omitted
      color: "",
      year: 0,
      PriceInINR: { $gt: 0, $lt: 0 }
    }
  }
])

另一方面,我发现您的数据结构非常奇怪。通常,您希望避免嵌套的对象数组,特别是如果要对它们执行许多查询时。

相关问题