Node.js 猫鼬没有带来任何结果
我试图在mongoose中构建一种复杂的查询,我的问题是这个查询可以接收的所有参数都是非强制性的,这给我带来了问题。现在我做到了:Node.js 猫鼬没有带来任何结果,node.js,express,mongoose,Node.js,Express,Mongoose,我试图在mongoose中构建一种复杂的查询,我的问题是这个查询可以接收的所有参数都是非强制性的,这给我带来了问题。现在我做到了: let query = { publicationType: req.body.searchParams.publicationType }; if (req.body.searchParams.address) { query.address = {}; if
let query = {
publicationType: req.body.searchParams.publicationType
};
if (req.body.searchParams.address) {
query.address = {};
if (req.body.searchParams.address.city) {
query.address.city = req.body.searchParams.address.city;
}
}
try {
const dwellings = await model.Dwelling.find({
$and: [{$or: [{publicationType: req.body.searchParams.publicationType}]},
{
$or: [{
address: [query.address]
}]
}]
}).lean().exec();
console.log(dwellings);
res.send({dwellings});
} catch (err) {
next(err);
}
});
这是为了测试city参数是否有效,如果我只按发布类型搜索,我会得到所需的结果,如果我添加city,我会得到0个结果,即使我应该得到更多结果,我调试了mongoose以查看查询是如何生成的,如下所示:
Mongoose: dwelling.find({ '$and': [ { '$or': [ { publicationType: 'Venta' } ] }, { '$or': [ { address: { '$in': [ { city: 'La Plata' } ] } } ] } ] }, { fields: {} })
[]
db.dwellings.find({
'$or': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
]
});
在您的代码中,您正在构建一个由两个组成的表达式,但每个$或表达式都只有一个参数,因此您正在有效地构建这个$和
表达式(注意,我使用的是下面的mongo
shell语法):
。。。换句话说:“找到带有publicationTypeVenta
和
address.city
时获得更多结果,我怀疑您确实希望顶级表达式为$或而不是$和
,如下所示:
Mongoose: dwelling.find({ '$and': [ { '$or': [ { publicationType: 'Venta' } ] }, { '$or': [ { address: { '$in': [ { city: 'La Plata' } ] } } ] } ] }, { fields: {} })
[]
db.dwellings.find({
'$or': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
]
});
在将查询表示为代码之前,您可能希望在mongo
shell中对查询进行更多的实验。如果您的工作站上碰巧有Docker,我发现在容器中运行mongo
是在开发时对事物进行原型化的一种有用方法,例如:
docker run --name mongo -d mongo:3.7
# after mongo is up and running...
docker exec -it mongo mongo
在交互式mongo shell中,您可以插入用于测试和试验find
表达式的文档,例如:
use stackoverflow;
dwell1 = { publicationType: 'Venta', address: { city: 'Somewhere' } };
dwell2 = { publicationType: 'Something', address: { city: 'La Plata' } };
db.dwellings.insert(dwell1);
db.dwellings.insert(dwell2);
# Try results of $and
db.dwellings.find({ '$and': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
] });
# ... compare to results of $or
db.dwellings.find({ '$or': [
{ publicationType: 'Venta' },
{ address: { '$in': [ { city: 'La Plata' } ] } }
] });
…插入一些文档进行测试后,请确认在find
中是否需要$和
或$或
表达式,因此借助drew Maclnnis的指导,我至少找到了如何使其工作的方法
const query = {};
if (req.body.searchParams.publicationType !== '') {
query['publicationType'] = req.body.searchParams.publicationType;
}
if (req.body.searchParams.address) {
if (req.body.searchParams.address.streetNumber !== undefined) {
query['address.streetNumber'] = req.body.searchParams.address.streetNumber;
}
if (req.body.searchParams.address.streetName !== undefined) {
query['address.streetName'] = req.body.searchParams.address.streetName;
}
if (req.body.searchParams.address.city !== undefined) {
query['address.city'] = req.body.searchParams.address.city;
}
if (req.body.searchParams.address.state !== undefined) {
query['address.state'] = req.body.searchParams.address.state;
}
if (req.body.searchParams.address.country !== undefined) {
query['address.country'] = req.body.searchParams.address.country;
}
if (req.body.searchParams.address.zip !== undefined) {
query['address.zip'] = req.body.searchParams.address.zip;
}
}
if (req.body.searchParams.price) {
if (req.body.searchParams.price.min !== undefined) {
query['price'] = {$gt: req.body.searchParams.price.min};
}
if (req.body.searchParams.price.max !== undefined) {
query['price'] = {$lt: req.body.searchParams.price.max};
}
}
try {
const dwellings = await model.Dwelling.find({
'$and': [
query
]
}).lean().exec();
console.log(dwellings);
res.send({dwellings});
} catch (err) {
next(err);
}
这可以优化吗?这很有帮助,但仍然不起作用,我的第一个问题是我想找到具有publicationType:Venta和address.city la plata的住宅,但我可能无法获得地址,只能获得出版物类型。其次,我尝试了第一个示例,您向我展示了第一个示例,但仍然以看起来唯一的方式得到0结果如果我这样问{“address.city”:“La Plata}和这个“address.city”:req.body.searchParams.address.city?(req.body.searchParams.address.city?req.body.searchParams.address.city:'':''问题是如果我没有地址,它会用city=''搜索文档