在我的代码中人们可以关注其他人。 到目前为止一切都正常,除了这个事实:在用户方案中我有这个字段。
, following: [{ type: Schema.ObjectId, ref: 'Users' }]
由于每个用户都有一个用户名,使用带有用户名的dbref更加灵活。是否有一种类似这样的方法?
, following: [{ type: Users.username, ref: 'Users' }]
非常感谢,g。
[{ type: Schema.ObjectId, ref: 'Users' }]
User.find({}).populate('following', 'username').exec(function(err,doc) {});
{ _id: 'xxxxxxxxxx', username: 'xxxxxxxx' }
mongoose 的 populate 函数在抽象层面上相当于 mongodb 的 lookup,但是 populate 只创建对对象 id 的引用。如果你确保每个用户都有唯一的用户名,那么如果你想使用用户名填充数据,你可以使用 lookup,结果将是相同的。