Node.js 如何使用mongoose同时搜索多个查询?
这是一个单一的搜索。我想同时搜索用户名、组织和全名。怎么可能呢Node.js 如何使用mongoose同时搜索多个查询?,node.js,mongodb,mongoose,search,Node.js,Mongodb,Mongoose,Search,这是一个单一的搜索。我想同时搜索用户名、组织和全名。怎么可能呢 await User.find({ username: new RegExp(keyword, 'i')}).select('-password') .then((profile)=>{ if(!profile){ res.json({ status: false, mess
await User.find({ username: new RegExp(keyword, 'i')}).select('-password')
.then((profile)=>{
if(!profile){
res.json({
status: false,
message: 'No profiles found'
})
此代码无效您可以使用mongoose
$或
跨字段搜索
await User.find({ username: new RegExp(keyword, 'i') full_name: new RegExp(keyword, 'i'), organisation: new RegExp(keyword, 'i')}).select('-password')
它在每个文档的
用户名
,组织
,全名
字段中搜索关键字。谢谢您的帮助
User.find({
$or: [
{ username: new RegExp(keyword, 'i') },
{ organization: new RegExp(keyword, 'i') },
{ full_name: new RegExp(keyword, 'i') }
]
})