9

The MongoDB docs for DBREFs say:

Unless you have a compelling reason to use DBRefs, use manual references instead.

Why? DBREFs seem more easy to use, since they encode the database and collection names, which would lead to less hard-coding in the application. Plus, DBREF is a standard format that many drivers understand.

This question is related, but not exactly the same:

MongoDB - is DBREF necessary?

The answer to that question is that embedding/denormalization is preferable to linking, but it doesn't answer the question of why manual linking is preferable to DBREFs.

Community
  • 1
  • 1
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
  • 2
    In short, it's because it's overkill for most situations as you end up storing the same collection name in every single DBRef which bloats the size of your docs. – JohnnyHK Jan 05 '16 at 18:39
  • @JohnnyHK, that's a good point, but may not apply when using the WiredTiger storage engine https://www.mongodb.com/blog/post/new-compression-options-mongodb-30 – Max Heiber Jan 05 '16 at 18:58

1 Answers1

0

Here a conclusion of all I viewed.

Using DBRef is not a join operation, it will automatically query the second or more times, depends on how much DBRef you have got in this collection fields.

Assuming you have a collection that its model has 10 DBRef, you make query for 10 elements' list of it and one of these DBRef is really needed. Once you query, Mongodb will runs 101(1 + 10*10) queries, automatically, no matter you need these DBRef or not. If you query these field manually, just a few coding and only 11(1 + 1*10) queries are needed.

So, what do you say?

Kroderia
  • 623
  • 4
  • 15
  • 1
    sounds plausible, happy to mark correct as long as I can verify: you have a source for this information or an example of how to verify? – Max Heiber Jul 07 '16 at 15:52
  • Well, tested just now. I enable the profiling in mongodb and make a query all for a Class A, which with 3 `DBRef` fields of Class B. The query returns 4 A. In log, it shows 1 query in collection A and 12 queries in collection B. You can test it yourself, it's hard to paste the logging message in comment. – Kroderia Jul 07 '16 at 19:28
  • Moreover, in the logging of `spring-boot-starter-data-mongodb`, it shows only one query. – Kroderia Jul 07 '16 at 19:37
  • 3
    That's a good point @Kroderia, but those refs are not populated automatically. From the docs: _The drivers [1] do not automatically resolve DBRefs into documents._ That means you still have the option to query the raw documents with unresolved references and save those requests. – Danziger May 22 '17 at 16:22
  • 1
    @kroderia, it sounds like you're using an abstraction layer / OOM that is generating bad queries. – Max Heiber Jun 25 '17 at 16:37