[英]Populate does not work with $regex mongodb
我有这个查询工作正常但是当我使用正则表达式时子文档没有填充
let u = new RegExp(`${req.params.username}`, 'i');
let user = await User.findOne({
userName: { $regex: u },
}).populate({
path: 'profiles',
populate: {
path: 'companyId',
match: { _id: { $exists: true } },
},
});
它总是将子字段返回为空
这个查询在没有正则表达式的情况下工作正常
let user = await User.findOne({
userName: req.params.username,
}).populate({
path: 'profiles',
populate: {
path: 'companyId',
match: { _id: { $exists: true } },
},
});
请尝试下面的代码,这是经过测试的代码,'.*'表示如果字符串匹配任何地方像字符串是查找并且我想用外观搜索它会返回所有包含外观的记录。
let user = await User.findOne({
userName: {
$regex: `.*${req.params.username}.*`,
$options: 'i'
},
}).populate({
path: 'profiles',
populate: {
path: 'companyId',
match: { _id: { $exists: true } },
},
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.