mongodb:集合中的find()查询 [英] mongodb : find() query in a collection
本文介绍了mongodb:集合中的find()查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有什么:
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)
但是这里首先有很多公司,然后每个公司都有汽车列表.
如何进行此类查询?
- 我想要一个考虑价格范围的查询.
- 和另一个没有价格范围条件的商品.
早期想法:我以为知道公司名称就可以分别查询每个公司.因此单独查找结果,然后将其推入数组.
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.
还有其他建议,我该怎么做?
Mongo DB中公司集合的实际结构
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"
}
]
}
推荐答案
您可以使用聚合. $ unwind 将为每辆汽车输出文档.然后 $ match 代替 find( ).
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 }
])
这篇关于mongodb:集合中的find()查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文