我在Mongodb中有一个用户列表需要根据一些过滤器进行搜索,如下图所示:Only gender is mandatory and users may or may not have other details
User.find({
"gender": userEntry.gender,
"dob": { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit), $exist: false },
"details.chest": { $gte: userEntry.chestLowerLimit, $lte: userEntry.chestHigherLimit, $exist: false },
"details.waist": { $gte: userEntry.waistLowerLimit, $lte: userEntry.waistHigherLimit, $exist: false },
"details.height": { $gte: userEntry.heightLowerLimit, $lte: userEntry.heightHigherLimit, $exist: false },
"details.weight": { $gte: userEntry.weightLowerLimit, $lte: userEntry.weightHigherLimit, $exist: false }
}, function (err, users) {
return res.render('client/search.html', { users: users });
});
上面是要搜索的mongoose查询,userEntry看起来像这样
userEntry={
"gender":2,
"ageLowerLimit":28,"ageHigherLimit":40,
"chestLowerLimit":"","chestHigherLimit":"",
"heightLowerLimit":"","heightHigherLimit":"",
"waistLowerLimit":"","waistHigherLimit":"",
"weightLowerLimit":"","weightHigherLimit":"",
"state":"","city":"",
"country":"","skin_color":"",
"profession_type":"","experience":"",
"hair_type":""
}
我的问题是查找功能,它应搜索性别为“2”且年龄> = 28且年龄=< 40的所有记录(即使一条记录满足它,上面的查询也会给我空数组),给出满足上述条件的所有结果,不管其他字段是空的还是不存在。 任何帮助将不胜感激。
正确建议将查询更改为但仍然提取0条记录
var query = {
details: {}
};
if (userEntry.gender) {
query.gender = userEntry.gender;
}
if(userEntry.ageLowerLimit && userEntry.ageHigherLimit ) {
query.dbo = { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit)};
}
console.log(query);
User.find(query, function (err, users) {
if(!err) {
console.log(users);
return res.render('client/search.html', { users: users });
}
console.log(err);
});
});
尝试获取的记录之一
{ "_id" : ObjectId("59c3f47e6388613b94556b78"), "name" : "tanzeel", "email" : "im_tanzeel@yahoo.co.in", "password" : "$2a$10$kvachEZL0vEPPJiS7bIAMeGMXiZ.MRaZmrBECXB207jme1I4JEn6i", "created_at" : ISODate("2017-09-21T17:18:54.822Z"), "role" : 1, "following" : [ ], "dp" : "/dp/default.jpg", "gender" : 2, "__v" : 0, "dob" : ISODate("1994-11-29T00:00:00Z"), "details" : { "height" : 160, "weight" : 65, "profession_type" : "Actor", "skin_color" : "Tan", "eye_color" : "Black", "waist" : 32, "chest" : 35 } }
答案 0 :(得分:0)
您从MongoDB获得了正确的值,但未获得预期值,因为您的查询构建不正确。另外,$exist
无效的运算符应为$exists
,并且您与details.waist = ""
等某些字段的空字符串进行比较,因为userEntry.weightLowerLimit
为空。但是,您应该正确构建查询以获得预期结果。
可以这样试试......
var query = {
details: {}
};
if (userEntry.gender) {
query.gender = userEntry.gender;
}
if(userEntry.ageLowerLimit && userEntry.ageHigherLimit ) {
query.dbo = { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit)};
}
if(userEntry.chestLowerLimit && userEntry.chestHigherLimit) {
query['details.chest'] = { $gte: userEntry.chestLowerLimit, $lte: userEntry.chestHigherLimit };
}
//... for others conditions
User.find(query, function (err, users) {
if(!err) {
return res.render('client/search.html', { users: users });
}
console.log(err);
});