gpt4 book ai didi

javascript - Mongoose 查找功能搜索未返回所需结果

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:32 25 4
gpt4 key购买 nike

我在 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 } }

最佳答案

您从 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);
});

关于javascript - Mongoose 查找功能搜索未返回所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46873930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com