DbRef与猫鼬-mongoose-dbref还是填充? [英] DbRef with Mongoose - mongoose-dbref or populate?

查看:99
本文介绍了DbRef与猫鼬-mongoose-dbref还是填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下2种模式:

公司活动:

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推荐会员的方式是否正确? 我正在尝试做一些与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. DBRefObjectId,集合名称和可选数据库名称的元组.

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.

猫鼬ref:字段仅包含ObjectId,而猫鼬模式定义了一个集合对象ID引用的对象.

Mongoose ref: fields, however, contain just an ObjectId and it's the Mongoose schema that defines what one collection the ObjectIds reference.

因此,猫鼬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与猫鼬-mongoose-dbref还是填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆