Mongo Db,如何查询参数中哪里是对象
Mongo Db , how to query where in the paramer is an object
Mongo Db,如何查询参数中哪里是带数组的对象。我想 return 基于下面示例查询参数的结果。 array 中的 year 还包括 model , mileage 等。我们将如何在 mongo 中做到这一点?我想 return 基于下面过滤器的数据。
代码
exports.list = function (req, res) {
let request = req.query.filters || req.query
let pageNo = parseInt(req.query.page || 1)
let size = parseInt(req.query.pageSize || 10)
let query_pagination = {}
let query = {}
query_pagination.skip = size * (pageNo - 1)
query_pagination.limit = size
query.Year = { $in: request.year }
query.Model = { $in: request.model }
query.Mileage = { $in: request.mileage }
query.PriceRange = { $in: request.priceRange }
query.BodyStyle = { $in: request.bodyStyle }
console.log("query", query)
Vehicle.model.find().exec(function(err, vehicle) {
if(err || !vehicle) {
res.json({
message: err
})
}
return res.status(200).send({
message: "success",
data: vehicle
})
})
}
}
查询参数
{
Year: ['2017'],
Model: ['Edge', 'Eco Sports', 'Escape'],
Mileage: ['1-1000'],
PriceRange: [',000-,000'],
BodyStyle: ['Convertible', '2dr Car', '4dr Car']
}
示例数据
[
{
"_id": "5dafe73296876045c01ca797",
"DriveType": "RWD",
"FuelType": "Gasoline Fuel",
"ImageList": "",
"Options": "Rear Wheel Drive,Locking/Limited Slip Differential,Tow Hitch,Tires - Front All-Season,Tires - Rear All-Season,Conventional Spare Tire,Aluminum Wheels,Power Steering,4-Wheel Disc Brakes,ABS,Luggage Rack,Running Boards/Side Steps,Fog Lamps,Automatic Headlights,Heated Mirrors,Power Mirror(s),Privacy Glass,Intermittent Wipers,Variable Speed Intermittent Wipers,Power Driver Seat,Cloth Seats,Split Bench Seat,Driver Adjustable Lumbar,Passenger Adjustable Lumbar,Pass-Through Rear Seat,Rear Bench Seat,3rd Row Seat,Floor Mats,Adjustable Steering Wheel,Adjustable Pedals,Power Windows,Power Door Locks,Keyless Entry,Cruise Control,Engine Immobilizer,A/C,Rear A/C,Rear Defrost,AM/FM Stereo,Cassette,CD Player,Power Outlet,Auto-Dimming Rearview Mirror,Front Reading Lamps,Driver Vanity Mirror,Passenger Vanity Mirror,Driver Illuminated Vanity Mirror,Passenger Illuminated Visor Mirror,Driver Air Bag,Passenger Air Bag,Child Safety Locks",
"Description": "",
"DateInStock": "8/13/2019",
"Invoice": 3500,
"BookValue": "4566",
"MSRP": null,
"SellingPrice": 6991,
"Miles": 135779,
"Transmission": "Automatic",
"EngineDisplacement": "4.6L",
"EngineCylinders": "8",
"InteriorColor": "",
"ExteriorColor": "Gray",
"Doors": 4,
"ModelNumber": "U15",
"Trim": "Xlt Popular",
"Body": "Sport Utility",
"Model": "Edge",
"Make": "Ford",
"Year": 2003,
"VIN": "1FMRU15W83LA54322",
"Stock": "P10100",
"Type": "Used",
"__v": 0,
"CategorizedOptions": "",
"Comment": "",
"FileData": "",
"name": "",
"publishedDate": "2019-10-23T16:00:00.000Z",
"content": {
"brief": "",
"extended": ""
},
"state": "published",
"Certified": true
},
{
"_id": "5dafe73296876045c01ca796",
"DriveType": "RWD",
"FuelType": "Gasoline Fuel",
"ImageList": "",
"Options": "SOUND SYSTEM FEATURE XM SATELLITE RADIO. features 67 channels of 100% commercial-free music included in its over 150 channels of the best in music news sports talk comedy XM Instant Traffic and Weather and more. Digital quality sound with coast-to-coast signal coverage. Three trial months-no obligation. (Includes body-color external antenna. Subscription fees apply. Available only in the 48 contiguous U.S.),HIGHWEAR NUANCE LEATHER SEAT TRIM (STD),ENGINE 6.0L LS2 V8 SFI (400 HP [298.3 kW] @ 6000 rpm 400 lb.-ft. [542.8 N-m] @ 4400 rpm) (STD),TRANSMISSION 4-SPEED AUTOMATIC ELECTRONICALLY CONTROLLED WITH OVERDRIVE (Includes (GU2) Axle 2.73 ratio),SEATS FRONT SPORT BUCKET WITH PERFORATED LEATHER SEATING SURFACES includes back angle adjustment adjustable lumbar and side bolsters (Includes (AJ7) Air bags frontal and side-impact driver and front passenger.) (STD),HOMELINK TRANSMITTER includes garage door opener 3-channel programmable located on driver visor,SOUND SYSTEM ETR AM/FM STEREO WITH 6-DISC IN DASH CD CHANGER MP3 PLAYBACK AND (U65) SOUND SYSTEM FEATURE BOSE PREMIUM 7-SPEAKER SYSTEM includes Radio Data System seek-and-scan digital clock auto-tone control automatic volume and TheftLock,LICENSE PLATE BRACKET FRONT,WHEELS POLISHED 5-SPOKE ALUMINUM 18 x 8.5 (45.7 CM x 21.6 CM) FRONT AND 19 x 10.0 (48.3 CM x 25.4 CM) REAR,HEAD-UP DISPLAY includes dot-matrix readouts for street mode track mode with g-meter vehicle speed engine rpm and readings from key gauges including water temperature and oil pressure,PREFERRED EQUIPMENT GROUP includes (UV6) Head-Up Display (UG1) Homelink transmitter (AAB) Memory Package (DD0) Mirrors inside rearview with compass and driver outside rearview auto-dimming (KA1) Seats heated driver and passenger (US9) Sound system ETR AM/FM stereo with 6-disc in dash CD changer MP3 playback (N37) Steering column power telescopic includes manual tilt,AXLE 3.15 PERFORMANCE RATIO,MEMORY PACKAGE 2 driver settings remembers presets for 6-way power seat outside mirrors climate control radio presets and telescoping steering column,Locking/Limited Slip Differential,Rear Wheel Drive,Traction Control,Stability Control,Tires - Front Performance,Tires - Rear Performance,Aluminum Wheels,Power Steering,ABS,4-Wheel Disc Brakes,Rear Defrost,Convertible Soft Top,HID headlights,Automatic Headlights,Fog Lamps,Daytime Running Lights,Heated Mirrors,Power Mirror(s),Intermittent Wipers,Driver Air Bag,Passenger Air Bag,Front Side Air Bag,Leather Seats,Bucket Seats,Driver Adjustable Lumbar,Passenger Adjustable Lumbar,Power Driver Seat,Power Passenger Seat,Power Outlet,Floor Mats,Adjustable Steering Wheel,Leather Steering Wheel,Tire Pressure Monitor,Power Door Locks,Power Windows,Cruise Control,Security System,Engine Immobilizer,Keyless Start,Keyless Entry,Remote Trunk Release,Climate Control,Multi-Zone A/C,AM/FM Stereo,CD Player,MP3 Player,Front Reading Lamps,Driver Vanity Mirror,Passenger Vanity Mirror,Driver Illuminated Vanity Mirror,Passenger Illuminated Visor Mirror,Passenger Air Bag On/Off Switch",
"Description": "",
"DateInStock": "08/08/2019",
"Invoice": 18000,
"BookValue": "19556",
"MSRP": 0,
"SellingPrice": 22997,
"Miles": 22116,
"Transmission": "Automatic",
"EngineDisplacement": "6.0L",
"EngineCylinders": "8",
"InteriorColor": "Cashmere",
"ExteriorColor": "Victory Red",
"Doors": 2,
"ModelNumber": "1YY67",
"Trim": "Base",
"Body": "Convertible",
"Model": "Corvette",
"Make": "Chevrolet",
"Year": 2005,
"VIN": "1G1YY34U455128500",
"Stock": "P10092",
"Type": "New",
"__v": 0,
"state": "published",
"Certified": true
}]
Vehicle.model.find(query, null, options)
try like this...
您可以通过以下方式实现:
Vehicle.model.find( { Year: { $all: ['2017', '2018'] } } )
您可以相应地用查询变量替换所有输入。
Mongo Db,如何查询参数中哪里是带数组的对象。我想 return 基于下面示例查询参数的结果。 array 中的 year 还包括 model , mileage 等。我们将如何在 mongo 中做到这一点?我想 return 基于下面过滤器的数据。
代码
exports.list = function (req, res) {
let request = req.query.filters || req.query
let pageNo = parseInt(req.query.page || 1)
let size = parseInt(req.query.pageSize || 10)
let query_pagination = {}
let query = {}
query_pagination.skip = size * (pageNo - 1)
query_pagination.limit = size
query.Year = { $in: request.year }
query.Model = { $in: request.model }
query.Mileage = { $in: request.mileage }
query.PriceRange = { $in: request.priceRange }
query.BodyStyle = { $in: request.bodyStyle }
console.log("query", query)
Vehicle.model.find().exec(function(err, vehicle) {
if(err || !vehicle) {
res.json({
message: err
})
}
return res.status(200).send({
message: "success",
data: vehicle
})
})
}
}
查询参数
{
Year: ['2017'],
Model: ['Edge', 'Eco Sports', 'Escape'],
Mileage: ['1-1000'],
PriceRange: [',000-,000'],
BodyStyle: ['Convertible', '2dr Car', '4dr Car']
}
示例数据
[
{
"_id": "5dafe73296876045c01ca797",
"DriveType": "RWD",
"FuelType": "Gasoline Fuel",
"ImageList": "",
"Options": "Rear Wheel Drive,Locking/Limited Slip Differential,Tow Hitch,Tires - Front All-Season,Tires - Rear All-Season,Conventional Spare Tire,Aluminum Wheels,Power Steering,4-Wheel Disc Brakes,ABS,Luggage Rack,Running Boards/Side Steps,Fog Lamps,Automatic Headlights,Heated Mirrors,Power Mirror(s),Privacy Glass,Intermittent Wipers,Variable Speed Intermittent Wipers,Power Driver Seat,Cloth Seats,Split Bench Seat,Driver Adjustable Lumbar,Passenger Adjustable Lumbar,Pass-Through Rear Seat,Rear Bench Seat,3rd Row Seat,Floor Mats,Adjustable Steering Wheel,Adjustable Pedals,Power Windows,Power Door Locks,Keyless Entry,Cruise Control,Engine Immobilizer,A/C,Rear A/C,Rear Defrost,AM/FM Stereo,Cassette,CD Player,Power Outlet,Auto-Dimming Rearview Mirror,Front Reading Lamps,Driver Vanity Mirror,Passenger Vanity Mirror,Driver Illuminated Vanity Mirror,Passenger Illuminated Visor Mirror,Driver Air Bag,Passenger Air Bag,Child Safety Locks",
"Description": "",
"DateInStock": "8/13/2019",
"Invoice": 3500,
"BookValue": "4566",
"MSRP": null,
"SellingPrice": 6991,
"Miles": 135779,
"Transmission": "Automatic",
"EngineDisplacement": "4.6L",
"EngineCylinders": "8",
"InteriorColor": "",
"ExteriorColor": "Gray",
"Doors": 4,
"ModelNumber": "U15",
"Trim": "Xlt Popular",
"Body": "Sport Utility",
"Model": "Edge",
"Make": "Ford",
"Year": 2003,
"VIN": "1FMRU15W83LA54322",
"Stock": "P10100",
"Type": "Used",
"__v": 0,
"CategorizedOptions": "",
"Comment": "",
"FileData": "",
"name": "",
"publishedDate": "2019-10-23T16:00:00.000Z",
"content": {
"brief": "",
"extended": ""
},
"state": "published",
"Certified": true
},
{
"_id": "5dafe73296876045c01ca796",
"DriveType": "RWD",
"FuelType": "Gasoline Fuel",
"ImageList": "",
"Options": "SOUND SYSTEM FEATURE XM SATELLITE RADIO. features 67 channels of 100% commercial-free music included in its over 150 channels of the best in music news sports talk comedy XM Instant Traffic and Weather and more. Digital quality sound with coast-to-coast signal coverage. Three trial months-no obligation. (Includes body-color external antenna. Subscription fees apply. Available only in the 48 contiguous U.S.),HIGHWEAR NUANCE LEATHER SEAT TRIM (STD),ENGINE 6.0L LS2 V8 SFI (400 HP [298.3 kW] @ 6000 rpm 400 lb.-ft. [542.8 N-m] @ 4400 rpm) (STD),TRANSMISSION 4-SPEED AUTOMATIC ELECTRONICALLY CONTROLLED WITH OVERDRIVE (Includes (GU2) Axle 2.73 ratio),SEATS FRONT SPORT BUCKET WITH PERFORATED LEATHER SEATING SURFACES includes back angle adjustment adjustable lumbar and side bolsters (Includes (AJ7) Air bags frontal and side-impact driver and front passenger.) (STD),HOMELINK TRANSMITTER includes garage door opener 3-channel programmable located on driver visor,SOUND SYSTEM ETR AM/FM STEREO WITH 6-DISC IN DASH CD CHANGER MP3 PLAYBACK AND (U65) SOUND SYSTEM FEATURE BOSE PREMIUM 7-SPEAKER SYSTEM includes Radio Data System seek-and-scan digital clock auto-tone control automatic volume and TheftLock,LICENSE PLATE BRACKET FRONT,WHEELS POLISHED 5-SPOKE ALUMINUM 18 x 8.5 (45.7 CM x 21.6 CM) FRONT AND 19 x 10.0 (48.3 CM x 25.4 CM) REAR,HEAD-UP DISPLAY includes dot-matrix readouts for street mode track mode with g-meter vehicle speed engine rpm and readings from key gauges including water temperature and oil pressure,PREFERRED EQUIPMENT GROUP includes (UV6) Head-Up Display (UG1) Homelink transmitter (AAB) Memory Package (DD0) Mirrors inside rearview with compass and driver outside rearview auto-dimming (KA1) Seats heated driver and passenger (US9) Sound system ETR AM/FM stereo with 6-disc in dash CD changer MP3 playback (N37) Steering column power telescopic includes manual tilt,AXLE 3.15 PERFORMANCE RATIO,MEMORY PACKAGE 2 driver settings remembers presets for 6-way power seat outside mirrors climate control radio presets and telescoping steering column,Locking/Limited Slip Differential,Rear Wheel Drive,Traction Control,Stability Control,Tires - Front Performance,Tires - Rear Performance,Aluminum Wheels,Power Steering,ABS,4-Wheel Disc Brakes,Rear Defrost,Convertible Soft Top,HID headlights,Automatic Headlights,Fog Lamps,Daytime Running Lights,Heated Mirrors,Power Mirror(s),Intermittent Wipers,Driver Air Bag,Passenger Air Bag,Front Side Air Bag,Leather Seats,Bucket Seats,Driver Adjustable Lumbar,Passenger Adjustable Lumbar,Power Driver Seat,Power Passenger Seat,Power Outlet,Floor Mats,Adjustable Steering Wheel,Leather Steering Wheel,Tire Pressure Monitor,Power Door Locks,Power Windows,Cruise Control,Security System,Engine Immobilizer,Keyless Start,Keyless Entry,Remote Trunk Release,Climate Control,Multi-Zone A/C,AM/FM Stereo,CD Player,MP3 Player,Front Reading Lamps,Driver Vanity Mirror,Passenger Vanity Mirror,Driver Illuminated Vanity Mirror,Passenger Illuminated Visor Mirror,Passenger Air Bag On/Off Switch",
"Description": "",
"DateInStock": "08/08/2019",
"Invoice": 18000,
"BookValue": "19556",
"MSRP": 0,
"SellingPrice": 22997,
"Miles": 22116,
"Transmission": "Automatic",
"EngineDisplacement": "6.0L",
"EngineCylinders": "8",
"InteriorColor": "Cashmere",
"ExteriorColor": "Victory Red",
"Doors": 2,
"ModelNumber": "1YY67",
"Trim": "Base",
"Body": "Convertible",
"Model": "Corvette",
"Make": "Chevrolet",
"Year": 2005,
"VIN": "1G1YY34U455128500",
"Stock": "P10092",
"Type": "New",
"__v": 0,
"state": "published",
"Certified": true
}]
Vehicle.model.find(query, null, options)
try like this...
您可以通过以下方式实现:
Vehicle.model.find( { Year: { $all: ['2017', '2018'] } } )
您可以相应地用查询变量替换所有输入。