我想找到填充或搜索的猫鼬 [英] I want to find populate OR search, mongoose
本文介绍了我想找到填充或搜索的猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型代码:
var user = mongoose.Schema({
email: ....
password: .....
name : ...
company : ...
position : ....
phoneNumber : ...
signDate : Date,
friends: [{ type : mongoose.Schema.Types.ObjectId ,unique: true, ref: 'user' }],
accessToken: .....
});
并且:
var friend = mongoose.Schema({
friends: [{ type : mongoose.Schema.Types.ObjectId ,unique: true, ref: 'user' }]
});
我想使用这两个关键字来搜索所有字段.
var fds = new Friend;
User.findOne({email : user.email})
.populate({
path : 'friends',
match : ''''empty''''
})
.exec(function (err, fd) {
if(!err){
fds = fd.friends;
res.render('account/friend', { layout: false, user: user, friends: fds });
}else{ console.log(err)}
});
我不知道在空白处放什么.
var term = new RegExp(req.body.inputAll, 'i');
User.find().or([{ email: { $regex: term }},
{ name: { $regex: term }},
{ company: { $regex: term }},
{ position: { $regex: term }},
{ phoneNumber: { $regex: term }}])
在其他地方,这是单词OR搜索.但是除了使用匹配"之外,我不知道如何填充或搜索?
Elsewhere this was the word OR search. But I don't know how to populate OR search, or other way besides to using the Match?
推荐答案
您可以使用$or
运算符对match
的查询进行或"运算.像这样:
You can use $or
operator to OR the queries for match
. Something like this :
User.findOne({email : user.email})
.populate({
path : 'friends',
match : {
$or: [
{ email: { $regex: term }},
{ name: { $regex: term }},
{ company: { $regex: term }},
{ position: { $regex: term }},
{ phoneNumber: { $regex: term }}
]
}
})
.exec(function (err, fd) {
if(!err){
fds = fd.friends;
res.render('account/friend', { layout: false, user: user, friends: fds });
}
else{
console.log(err)
}
});
这篇关于我想找到填充或搜索的猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文