What I have :
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
What i can do now : if i had only one object (ie. one company in companies) then i would do something like :
query = {{transmission_type : transmission}, {color : car_color}, {year : model_year}, { $range: [ 0, "$PriceInINR", 25 ] } }
db.companies.cars.find(query)
But here first there are a lot of companies, and then each company has a list of cars.
How can I do such a query?
Early thoughts: I thought that I could do query each company separately as I know company names. so finding the result individually and then push them to an array.
How should I do this, any other suggestions?
The actual structure of companies collection in 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"
}
]
}
You can use aggregation. $unwind will output a document for each car. Then $match is used in place of find() in your example.
const transmission = 'Manual'
const car_color = 'Yellow'
const model_year = 2013
const min_price = 3500000
const max_price = 4000000
const query = {
'cars.transmission_type': transmission,
'cars.color': car_color,
'cars.year': model_year,
'cars.PriceInINR': { '$gte': min_price, '$lte': max_price }
}
db.companies.aggregate([
{ '$unwind': '$cars' },
{ '$match': query }
])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.