DB-ref在没有Schema.ObjectId的mongoose?
在我的代码中,人们可以跟随其他人。 到目前为止,除了这个事实之外,一切都还好:在userScheme中我有这个字段。
, following: [{ type: Schema.ObjectId, ref: 'Users' }]
由于每个用户都有一个用户名,所以我使用dbref的用户名更加通用。 有没有办法做这样的事情?
, following: [{ type: Users.username, ref: 'Users' }]
非常感谢,g
不,只有引用另一个集合的_id
属性的ObjectId
值可以用作参考。
在源代码中确认。
你只能通过ObjectId引用像你的第一个代码片段的集合。
[{ type: Schema.ObjectId, ref: 'Users' }]
使用populate()进行查询时,可以指定要返回的字段。 在你的情况下,它会看起来像
User.find({}).populate('following', 'username').exec(function(err,doc) {});
以下数组中的每个对象都是这样的
{ _id: 'xxxxxxxxxx', username: 'xxxxxxxx' }
- node.js mongodb – collection.find()。toArray(callback) – callback不会被调用
- mongooseorderby不工作
- Meteor.startup()之前总是调用meteor帮助器函数,而不是一次调用
- 如果主站closures,如何连接到从站 – 复制
- 所有Schema的模式方法 – Mongoose方法的inheritance
- mongooseselect与!布尔或布尔和过去的date
- 在套接字重新连接上设置集合:会话时设置TTL索引时出错
- node.js mongoose删除不在当前这些文档列表中的预处理文档的子集
- 什么是node.js + mongodb应用程序的基本架构?