我试图做一种复杂的猫鼬查询。我已经四处搜索并找到了解决单个变量(使用if)和构建查询对象的解决方案,但我想知道是否有更简单的方法因为我将构建一个包含大约40个非必需参数的查询,例如现在在我的req.body
我有:
{ searchParams:
{ publicationType: 'Venta',
subtype: [ 'Duplex', 'Cabaña' ],
address:
{
streetNumber: '1312',
streetName: 'José María Bosch',
city: 'Villa Bosch',
state: 'Buenos Aires',
country: 'Argentina' }
}
}
数据库中的所有名称都是相同的,我必须做什么我搜索publicationType
是否匹配,如果地址匹配,最后是子类型,如果任何这些数组项匹配,因为subtype是一个字符串。如果没有每个字段的if,有没有办法做到这一点?
现在我做了这个简单的查询试用
router.post('/search', (req, res, next) => {
model.Dwelling.find(req.body.searchParams).lean().exec().then(
dwellings => res.send({dwellings})
).catch(next);
});
适用于publicationType
和subtype
不在address
购买。
这是我数据库中的一个对象:
{
"_id": {
"$oid": "5af06167ea174f00142bab91"
},
"publicationType": "Alquiler",
"address": {
"latitude": -34.59250669999999,
"altitude": -58.571258599999965,
"streetNumber": "1312",
"streetName": "José María Bosch",
"city": "Villa Bosch",
"state": "Buenos Aires",
"country": "Argentina"
},
"type": "Residencial",
"subtype": "Casa",
"currency": "",
"price": 50000,
"occupationStatus": "Disponible",
"spaces": {
"rooms": 3,
"floors": 0,
"bedrooms": 0,
"closets": 0,
"bathRoom": 0,
"toilette": 0,
"living": false,
"livingDining": false,
"diningRoom": false,
"kitchen": false,
"kitchenDining": false,
"terrace": false,
"balcony": false,
"backYard": false,
"swimmingPool": false,
"barbecue": false,
"garage": "No",
"laundryRoom": "No"
},
"features": {
"status": "Desconocido",
"orientation": "Desconocida",
"luminosity": "Desconocida",
"heating": [
{
"value": "No posee",
"label": "No posee"
}
],
"refurbished": false,
"repair": "No"
},
"services": {
"gas": true,
"water": true,
"sewer": true,
"phone": true,
"pavement": true,
"electricity": true,
"cableTv": true
},
"legal": {
"bank": false,
"prof": false
},
"generalDescription": "Desc 1",
"privateDescription": "Desc 2",
"siocId": 713675,
"createdAt": {
"$date": "2018-05-07T14:23:35.230Z"
},
"updatedAt": {
"$date": "2018-05-07T14:23:35.230Z"
},
"__v": 0
}