mongodb:集合中的find()查询

时间:2018-09-09 11:12:03

标签: node.js mongodb mongodb-query

**enter image description here**

我有什么:

 list of company names : [bmw, tata, ferrari,...]
 transmission: Manual or Automatic3.
 car color: Red or White etc
 model year: 2010 to 2014
 Min Price & Max Price. --> Price range

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

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

db.companies.cars.find(query)

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

如何进行此类查询?

  1. 我想要一个考虑价格范围的查询。
  2. 和另一个没有价格范围条件的商品。

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

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

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)

您可以使用聚合。 $unwind将为每辆汽车输出一个文档。然后在您的示例中使用$match代替 find()

filenames <- list.files(dataPath)
names <- substr(filenames,13,17)

CCLF1_width <- c(13,6,11,2,10,10,1,1,7,7,2,17,1,2,2,4,1,10,10,10,10,10,2,10,10,10,11,2,2,1,1,1)
CCLF2_width <- c(13,10,11,2,10,10,4,10,5,11,6,10,10,24,17,2,2,2,2,2)
CCLF3_width <- c(13,11,2,2,7,10,11,6,10,10,1)
CCLF4_width <- c(13,11,2,1,2,7,11,6,10,10,7,1)
CCLF5_width <- c(13,10,11,2,10,10,3,2,2,1,2,10,10,5,15,1,7,10,10,2,2,2,10,10,40,11,17,24,2,2,2,2,2,2,7,7,7,7,7,7,7,7,1)
CCLF6_width <- c(13,10,11,2,10,10,1,2,10,10,5,15,1,10,10,2,2,2,10,10,40,11,17,2)
CCLF7_width <- c(13,11,11,2,10,2,20,1,1,24,9,2,20,13,2,10,10,12,9)
CCLF8_width <- c(11,2,3,5,10,1,1,3,2,2,10,10,10,30,15,40,1,1)
CCLF9_width <- c(11,11,10,10,12)
CCLF0_width <- c(11,11)

for (i in length(filenames)){
  assign(paste0(substr(filenames,13,17)), read_fwf(grepl("CCLF1",filenames),paste0(i,"_width")))
  }