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

查看:368
本文介绍了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,因此在数据库中不是' join ',它非常昂贵.

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天全站免登陆