nahid
nahid

Reputation: 407

Links vs References in Document databases

I am confused with the term 'link' for connecting documents

In OrientDB page http://www.orientechnologies.com/orientdb-vs-mongodb/ it states that they use links to connect documents, while in MongoDB documents are embedded.

Since in MongoDB http://docs.mongodb.org/manual/core/data-modeling-introduction/, documents can be referenced as well, I can not get the difference between linking documents or referencing them.

Upvotes: 2

Views: 4092

Answers (2)

Lvca
Lvca

Reputation: 9060

MongoDB works like RDBMS where the object id is like a foreign key. This means a "JOIN" that is run-time expensive. OrientDB, instead, has direct links that are created only once and have a very low run-time cost.

Upvotes: 2

Pete Garafano
Pete Garafano

Reputation: 4923

The goal of Document Oriented databases is to reduce "Impedance Mismatch" which is the degree to which data is split up to match some sort of database schema from the actual objects residing in memory at runtime. By using a document, the entire object is serialized to disk without the need to split things up across multiple tables and join them back together when retrieved.

That being said, a linked document is the same as a referenced document. They are simply two ways of saying the same thing. How those links are resolved at query time vary from one database implementation to another.

That being said, an embedded document is simply the act of storing an object type that somehow relates to a parent type, inside the parent. For example, I have a class as follows:

class User
{
    string Name
    List<Achievement> Achievements
}

Where Achievement is an arbitrary class (its contents don't matter for this example).

If I were to save this using linked documents, I would save User in a Users collection and Achievement in an Achievements collection with the List of Achievements for the user being links to the Achievement objects in the Achievements collection. This requires some sort of joining procedure to happen in the database engine itself. However, if you use embedded documents, you would simply save User in a Users collection where Achievements is inside the User document.

A JSON representation of the data for an embedded document would look (roughly) like this:

{
    "name":"John Q Taxpayer",
    "achievements":
        [
            {
                "name":"High Score",
                "point":10000
            },
            {
                "name":"Low Score",
                "point":-10000
            }
        ]
}

Whereas a linked document might look something like this: { "name":"John Q Taxpayer", "achievements": [ "somelink1", "somelink2" ] }

Inside an Achievements Collection { "somelink1": { "name":"High Score", "point":10000 } "somelink2": { "name":"High Score", "point":10000 } }

Keep in mind these are just approximate representations.

So to summarize, linked documents function much like RDBMS PK/FK relationships. This allows multiple documents in one collection to reference a single document in another collection, which can help with deduplication of data stored. However it adds a layer of complexity requiring the database engine to make multiple disk I/O calls to form the final document to be returned to user code. An embedded document more closely matches the object in memory, this reduces Impedance Mismatch and (in theory) reduces the number of disk I/O calls.

You can read up on Impedance Mismatch here: http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch

UPDATE

I should add, that choosing the right database to implement for your needs is very important from the start. If you have a lot of questions about each database, it might make sense to contact each supplier and get some of their training material. MongoDB offers 2 free courses you can take to learn more about their product and best uses at MongoDB University. OrientDB does offer training, however it is not free. It might be best to try contacting them directly and getting some sort of pre-sales training (if you are looking to license the db), usually they will put you in touch with some sort of pre-sales consultant to help you evaluate their product.

Upvotes: 5

Related Questions