MongoDB - 是否需要 DBREF? [英] MongoDB - is DBREF necessary?

查看:43
本文介绍了MongoDB - 是否需要 DBREF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 MongoDB 中的 DBREF 数据类型,文档可能如下图所示.但是在每一行中都有 $ref 字段感觉是多余的,因为每一行显然都指向 users 集合.

Using the DBREF datatype in MongoDB, a document may look like as shown below. But having the $ref field in every row feels redundant as every row obviously points to the users collection.

有没有一种方法可以引用其他文档而不会有一些多余的 $ref 字段?

Is there a way to reference other documents without having the somewhat redundant $ref-field?

{
    $id: {$oid : "4f4603820e25f4c515000001"},
    title:   "User group",
    users: [ 
        {_id: {$ref: "users", $id: { $oid: "4f44af6a024342300e000002"}}, isAdmin: true }
    ]
],

推荐答案

我认为在使用 mongodb 时应该避免使用 Dbref,至少如果您使用需要可扩展性的大型系统.

Dbref in my opinion should be avoided when work with mongodb, at least if you work with big systems that require scalability.

据我所知,所有驱动程序都会发出额外的请求来加载 DBRef,因此它不是在数据库中加入",它非常昂贵.

As i know all drivers make additional request to load DBRef, so it's not 'join' within database, it is very expensive.

有没有办法引用其他文档而不需要有点多余的 $ref-field?

Is there a way to reference other documents without having the somewhat redundant $ref-field?

是的,记住引用,为外键"(类似于 RefUserId 或仅 UserId)创建命名约定,并仅存储引用文档的 id.需要时自行加载参考文档.还要留意任何非规范化,嵌入你可以做的,因为它通常会大大提高性能.

Yes, keep references in the mind, create naming conventions for 'foreign keys' (something like RefUserId or just UserId) and store just id of referenced document. Load referenced documents yourself when needed. Also keep your eyes open for any denormalization, embedding you can do, because it's usually greatly improve performance.

这篇关于MongoDB - 是否需要 DBREF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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