J
J
Jemeron2014-03-19 19:19:21
NoSQL
Jemeron, 2014-03-19 19:19:21

How to properly organize relation in MongoDB?

I just started learning MongoDB and at the design stage of the application, the question arose of how it would be correct to organize an entity relation for the following case:
There are users (users), their locations (locations, country / city) and user interests (interests).
This is how I would do it using a relational database: I would store the location_id key in the users table and have a user_interests linking table with user_id, interest_id fields.
On MongoDB, I would store all the data in the user document, like this:

user = {
  name: "Vasya",
  location: { country: "Russia", city: "Moscow" },
  interests: ["football", "music", "ruby on rails"]
}

Everything would be cool, but it will be necessary to organize select countries, suggest cities and interests. Therefore, apparently, it will be necessary to take out all the same locations and interests in separate collections.
Then my structure will be like this:
user = {
  name: "Vasya",
  location: LOCATIONS._id,
  interests: [INTERESTS._id, INTERESTS._id, INTERESTS._id]
}

And as a result, when I select, all this will be separate queries inside, without JOINs. Does it make sense then to use a NoSQL solution instead of a relational database? Whether correctly I have drawn a conclusion about structure of relations?
I hope you can help me with this architectural moment.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2014-03-19
@Jemeron

Better use a different practice for obtaining and administering directories.
For example - to get a list of the unique values ​​of a location, use db.collection.distinct("location"). Optionally, you can add a query to limit the selected documents by criteria.
Accordingly, you make a satgest out of this directory, and you get an unadministered dictionary.
If you want an administered dictionary, you can simply store all dictionary items in one document and save the text representation in the user's documents. This is of course not very good if localization is needed. But then you need to use your option.

P
Puma Thailand, 2014-03-19
@opium

What is the problem of selecting cities and countries in one document?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question