时,这将匹配所有
我在搜索参数中传递空白('')值,它没有显示记录。我想要,当我传递null或空白值时,它会忽略搜索查询的参数,然后显示记录。我的代码是
var _branchName = req.query.branchName;
var _personName = req.query.personName;
var _clientID = req.query.clientID;
var _searchQRY = [
{
branchName: { $regex: _branchName, $options: '-i' }
},
{
personName: { $regex: _personName, $options: '-i' }
},
{
"client._id": _clientID
},
{
isDeleted: { $ne: true }
}];
objModel.find({ $and: _searchQRY }, function (err, results) {
res.json({ status: config.responseStatus, record: results })
});
在此中,我通过" _clientid"为空白('')。那时,我想忽略该参数并搜索保留参数。
您可以像
那样做smthvar _branchName = req.query.branchName;
var _personName = req.query.personName;
var _clientID = req.query.clientID;
var _searchQRY = [
{
branchName: { $regex: _branchName, $options: '-i' }
},
{
personName: { $regex: _personName, $options: '-i' }
},
{
isDeleted: { $ne: true }
}];
if (_clientID) {
_searchQRY.push(
{
"client._id": _clientID
}
);
}
objModel.find({ $and: _searchQRY }, function (err, results) {
res.json({ status: config.responseStatus, record: results })
});
即。仅当您在请求中指定它时,将client._id
包括在搜索查询中。
您能做的是,当_clientID
为空白时,将其值更改为对象这样的对象
if (_clientID == '')
_clientID = { $exists : true};
当_clientid为空字符串''
client._id