qq_36390583 2021-07-08 02:57 采纳率: 100%
浏览 21
已采纳

如何对子文档进行筛选查询?

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

  • 写回答

1条回答 默认 最新

  • CSDN专家-Time 2021-07-08 03:42
    关注

    js中有filter函数可以做筛选。
    mongodb也有相应的函数.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 7月9日

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗