Answer the question
In order to leave comments, you need to log in
MongoDB structure optimization: single or multiple collections?
Good day.
I am writing a project, using MongoDB for the first time, the question arose: does it make sense to divide the data into two collections, or is it just to slow down the application?
Suppose this situation: there are users, each user, among other things, has a list of objects. And these objects, in turn, have a fairly extensive list ... I don’t know, let’s say possible states. Whether there is a sense to make a collection of users, and in each document on the user in the list of objects to write down their id, and objects to store in a collection of objects?
I understand that to get information about the objects of one user, two queries will need to be made, but will it be slower than storing massive objects in one collection and searching through them?
And, if not difficult, can anyone provide a link to information on how best to organize MongoDB?
Thank you for attention.
Answer the question
In order to leave comments, you need to log in
It all depends on the number of objects the user has and the size of the list of objects - you can hit the 16 MB limit on the size of the document.
I would advise splitting into two collections. So that different entities lie in different collections. Especially if you get these states from the database separately from the user. And when stored in an array, you won’t be able, for example, to sort the states when requested.
Plus, queries to the database will not be as cumbersome as with an array.
Monga has official courses . Here is a video from there on your question.
if it does not open - here is a direct link to YouTube: link .
Also there the whole third week is devoted to circuit design, also check it out.
If there are problems with the video, I can accumulate links to YouTube
The nearest plans for the development of MongoDB include a lock per collection (in the current version, a lock per base, before it was generally for the entire instance), so I would divide it into separate collections: it will pay off in new versions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question