var relationship_schema = new Schema(
{
user:{type: Schema.Types.ObjectId, ref: 'User'},
friend:{type: Schema.Types.ObjectId, ref: 'User'},
is_accepted: {type:Boolean},
rela_type:{type:String,enum:['friend','family']},
...
}
);
var user_schema = new Schema(
{
is_authenticated:{type:String,enum:['real_name_auth','temp_auth'],default:'temp_auth'},
...
}
);
以上两个model,想通过查询筛选出is_accepted为true的relationship,而且relationship中的好友的is_authenticated为real_name_auth。
我的代码如下,目的为筛选auth_user.id有关的relationship,auth_user.id可为relationship中的user也可以为relationship中的friend
Relationship.find().or([{user:auth_user.id,is_accepted:true,"friend.is_authenticated":"real_name_auth"},{friend:auth_user.id,is_accepted:true,"user.is_authenticated":"real_name_auth"}])
.populate('user')
.populate('friend')
.exec(function (err, real_name_friends) {
//为利用查询结果real_name_friends的结果总数
if(auth_user.auth_number>=real_name_friends.length * a ){ },function(err){
if (err) { return next(err); }
});
}
})
结果为0,并不能筛选成功。如果去掉条件"friend.is_authenticated":"real_name_auth" "user.is_authenticated":"real_name_auth"则可筛选出和auth_user.id有关的所有relationship
js中有filter函数可以做筛选。
mongodb也有相应的函数.