mongodbmongodb-queryrelationshiprelationdbref

MongoDB Referenced Relationships vs DBRef


I started learning MongoDB like a week ago and I am stuck on Relationships. More like I am confused. I get when to use Embedded Relationships and when to use Referenced Relationships. I know Embedded Relationships got some drawbacks which is why we prefer Referenced Relationships over Embedded Relationships. Now, I am learning DBRefs. The thing is, I don't find it helpful in anyway. That's what I think. I hope I am wrong.

In DBrefs, we can reference documents from different collections in one document that's in a different collection. In RefRels, we can reference different documents from different collections in one document that's in a different collection.

I mean, we can perform the same thing using DBrefs what we can perform using Referenced Relationships.

In Referenced Relationships, we create a field in a collection in a document and store ObjectIds of documents from different collections like so:

> db.Employee.insert({"Emp_Name":"Emp_1", "Emp_Address":[ObjectId("some_id_from_Address_collection"), ObjectId("some_id_from_Address_collection"), ObjectId("some_id_from_Address_collection")], "Emp_Phone":[ObjectId("some_id_from_Phone_collection"), ObjectId("some_id_from_Phone_collection"), ObjectId("some_id_from_Phone_collection")]})

In DBrefs, we create a field in a collection in a document and store values using ObjectIds just like we used to do in Referenced Relationships but in a different way. Consider following example:

> db.Employee.insert({"address": {"$ref": "address_home", "$id": ObjectId("534009e4d852427820000002"), "$db": "tutorialspoint"}, "name": "Tom Benzamin"})

We are still using ObjectId to store values in the Employee collection but the syntax is different because in this example, we are mentioning which DB and Collection to look in.

Why not just use Referenced Relationships and save time, instead of using this confusing and lengthy query and waste half of the time ?

Am I missing something ?

Should I even consider learning DBrefs ?


Solution

  • The point of DBRef is it allows referencing a database and a collection from a single field. Without it you would need two fields[*].

    [*] Technically you could use a single field which is, for example, a Hash itself that contains database and collection reference, but then this is essentially the same thing as a DBRef but without the label.

    IF you want to reference documents in other databases, DBRef could be useful. Its usefulness is limited by the fact that generally you still need to handle cross-db operations in your application, as drivers and server generally won't do this seamlessly for you. For example, DBRef is not directly usable with aggregation framework.

    DBRef really just provides a more convenient way to store the database+collection name pair.

    IF all of your collections are in the same database, you don't need DBRef at all (and in fact it would only get in the way).

    Should I even consider learning DBrefs ?

    They are very much a niche feature. Probably not.