【发布时间】:2018-02-21 00:34:27
【问题描述】:
我需要从相同的用户模式填充 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);
现在,当我尝试打印用户信息关注者数组时,返回 ObjectId 而不是全部信息
我尝试用这个 sn-p 打印
User.find().exec(function(error, groups) {
return res.status(200).send(groups);
});
【问题讨论】:
-
请在您尝试检索用户信息的位置显示您的代码
-
@t3__rry 添加到我的问题中
-
正如@Kxng Kombian 建议的那样,您可以使用Mongoose 的
populationmongoosejs.com/docs/populate.html#population 但在您的定义中,您需要mongoose.Schema.Types.ObjectId -
还是得到[{followers:[5a8c52a9a11412b04b48a933],对象id而不是对象信息
标签: node.js mongodb express mongoose