Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

MongoDB - is DBREF necessary?

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.

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 }
    ]
],
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...