问题描述
我有以下两个架构:
公司活动:
var companyEventSchema = new Schema({
name : String,
description
date : Date,
attendees : [ { type : Schema.ObjectId, ref : 'Member' } ],
]});
和会员
var memberSchema = new Schema({
name : String,
emailAddress: String,
password :String,
created: { type: Date, default: Date.now }
});
我从 companyEventSchema 中引用 Member 的方式是否正确?我正在尝试做一些很长的 dbref 行.
Is the way i've ref'd Member from companyEventSchema correct?I'm trying to do something a long the lines of a dbref.
我看到有一个单独的项目...mongoose-dbref
I saw theres a separate project for that though... mongoose-dbref
然而,猫鼬文档说上面提供了类似dbref的功能"
However, the mongoose docs say the above provides "dbref like functionality"
哪个更有效?
推荐答案
你只需要使用一个实际的 DBRef
(和 mongoose-dbref
)用于字段可以包含引用可能多个集合中的文档的 ObjectId 的情况.DBRef
是 ObjectId
、集合名称和可选数据库名称的元组.
You only need to use an actual DBRef
(and mongoose-dbref
) for the case where a field can contain ObjectIds that reference documents in potentially more than one collection. A DBRef
is a tuple of an ObjectId
, a collection name, and an optional database name.
Mongoose ref:
字段仅包含一个 ObjectId
,并且 Mongoose 架构定义了 ObjectIds 引用的一个集合.
Mongoose ref:
fields, however, contain just an ObjectId
and it's the Mongoose schema that defines what one collection the ObjectIds reference.
因此 Mongoose ref:
字段效率更高,应始终使用,除非您需要 DBRef
提供的多集合引用支持.
So Mongoose ref:
fields are more efficient and should always be used unless you need the multi-collection reference support that DBRef
provides.
这篇关于DbRef 与猫鼬 - 猫鼬-dbref 还是填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!