Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have relation between object in a MongoDB database?

I am using MongoDB at work these days. So far, I find it a great experience.

However I am asked to make relations between collections with MongoDB. This goes against the purpose of the NoSQL concept to me but being a good old noob in this area, I came to ask for other opinions.

For example if we take the common Role / User relationship, is it possible to have a reference collection named "Roles" and to attribute a reference of one item to a User item?

I started thinking about creating an object which provides an ID to the object requested but it feels like something I should not have to do with NoSQL.

So are there people here who have been ask to do the same thing? Did you succeed and how?

like image 823
lollancf37 Avatar asked Aug 09 '11 09:08

lollancf37


People also ask

Can you have relations in MongoDB?

MongoDB Relationships are the representation of how the multiple documents are logically connected to each other in MongoDB. The Embedded and Referenced methods are two ways to create such relationships.

Is MongoDB object relational database?

MongoDB is a well-established, non-relational database system offering improved flexibility and horizontal scalability, but at the cost of some safety features of relational databases, such as referential integrity.

How are relationships represented in MongoDB?

Relationships in MongoDB represent how various documents are logically related to each other. Relationships can be modeled via Embedded and Referenced approaches. Such relationships can be either 1:1, 1:N, N:1 or N:N. Let us consider the case of storing addresses for users.

Does NoSQL support relationship?

NoSQL databases can store relationship data — they just store it differently than relational databases do. In fact, when compared with relational databases, many find modeling relationship data in NoSQL databases to be easier than in relational databases, because related data doesn't have to be split between tables.

How many types of relationships are there in MongoDB?

MongoDB has two types of relationships. Embedded and Reference made. Every relationship has its advantages and usages. These relationships help in improving performance.


2 Answers

MongoDB (and most other NoSQL databases) do not natively support the concept of relations. RDBMSs have native query tools to define and make use of relationships (JOINs, for example) that MongoDB lacks.

This doesn't mean you cannot define relationships/references in NoSQL databases. It simply means there are no native query operators that are relation aware.

There are three different ways to "refer" to one document from another in MongoDB :

  1. Store the referred document's ID (usually an ObjectId) as a field in the referring document. This is the best approach if your app will know in which collection it has to look for the referred document. Example : {_id: ObjectId(...); userId: ObjectId(...) <- reference).

  2. The second approach is using the DBRef convention which defines a reference to a document in a formalized way : { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }. In almost all cases the first approach is preferred but DBRef does allow references to document the application may not know the type or location of. Furhter information here : http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef and a good read on when to use them here : http://valyagolev.net/article/mongo_dbref/

  3. Not technically a reference but in a lot of cases it makes sense to embed (parts of) documents into other documents. Note that normalization of your schema should be less of a focus with NoSQL databases. Example : {_id: ObjectId(...); user: {_id: ObjectId(...), name:"Willy Wonka"}}.

All that said, you can use a completely normalized schema in MongoDB and most ORM libraries will do a lot of the work for you that is not native to MongoDB. In most case this does mean you'd be better off with a traditional RDBMS though. If you're switching to MongoDB because you think it's a fast version of MySQL you have been misinformed. Both have functional sweetspots with only limited overlap. If your app relies heavily on relational functionality don't use NoSQL.

Other articles worth reading to get you up to speed on non-relational thinking : http://www.mongodb.org/display/DOCS/Schema+Design

like image 195
Remon van Vliet Avatar answered Oct 04 '22 04:10

Remon van Vliet


I would do it like this:

db.permissions {
 "Admins" : { "Users" : ["Peter", "Tom"], "Privilages": "rw" },
 "Supervisors" : { "Users" : ["Tom", "Brad", "Angelina"], "Privilages": "w" },
 ...
}

That you are repeating yourself is not a problem inside a NoSQL structure. Normalization is not the optimum for it.

Alternatively, you can just store the $_id of each member in the "Users" array and have the users with their names, phone,... in a seperate collection. Another option is to store user documents in the "Users" array, but this may get cumbersome to administrate.

like image 39
DrColossos Avatar answered Oct 04 '22 03:10

DrColossos