Mongoose populate followers同样的文件

时间:2018-02-20 16:14:39

标签: node.js mongodb express mongoose

我需要从同一个用户模式中填充ObjectId参数我想要做的是“用户”文档,每条记录都有粉丝,而ref与用户的模式相同。

set :session_secret, SecureRandom.hex(64)

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

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

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);

1 个答案:

答案 0 :(得分:1)

您想要实现的目标是

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

})

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