猫鼬查询以列出用户加入的所有组

const groupSchema = new Schema({
  name: { type: String,default: "",required: true },participants: [{
    type: Schema.Types.ObjectId,ref: 'User'
  }],});

const userSchema = new Schema({
  username: { type: String,name: { type: String,});

我正在尝试获取给定用户加入的所有组。这是我的尝试

userModel.findOne({ username: data.username },function(err,user) {
  if (user) {
      groupModel
      .find({"participants":{"$in":[user]}
      .populate('participants')         
      .exec(function(err,result) {
       .....
       }
  }
 });

我在上面的查询中得到一个空列表 谢谢

fei89123 回答:猫鼬查询以列出用户加入的所有组

看起来您只需要这样做:

groupModel.find({participants: user._id}).exec(...
本文链接:https://www.f2er.com/3125855.html

大家都在问