首页 文章

Mongoose populate followers同样的文件

提问于
浏览
1

我需要从同一个用户模式填充ObjectId参数我想要做的是“用户”文档,每个记录都有关注者,ref是用户的相同模式 .

var userSchema = new Schema({
  username: {type: String, unique: true, trim: true},
  password: {type: String},
  email: {type: String, trim: true},
  avatar: {type: String},
  fullname: {type: String},
  bio: {type: String},
  phone: {type: Number},
  country: {type: String},
  postal: {type: Number},
  followers: [{ type: Schema.ObjectId, ref: 'User' }],
  followed: [{ type: Schema.ObjectId, ref: 'User' }],
  registered: {type: Date, default: Date.now}

});

var User = mongoose.model('users',userSchema);

现在,当我尝试打印用户信息时,追随者数组返回ObjectId而不是整个信息

我尝试使用此代码段进行打印

User.find().exec(function(error, groups) {    
    return res.status(200).send(groups);
  });

1 回答

  • 1

    你想在这里实现的是

    User.find({})
    .populate('followers')
    .populate('followed')
    .then(users => {
    
    })
    

    这将填充跟随者并跟随其各自的用户对象

相关问题