Answer the question
In order to leave comments, you need to log in
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"]
}
user = {
name: "Vasya",
location: LOCATIONS._id,
interests: [INTERESTS._id, INTERESTS._id, INTERESTS._id]
}
Answer the question
In order to leave comments, you need to log in
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.
What is the problem of selecting cities and countries in one document?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question