Without JOINs, what is the correct way to process data in document databases?

I understand that JOINs are impossible or incredulous in documents. I come from a relational database and try to figure out how to handle such scenarios.

Let's say I have a team of employees where I store all the information related to the employee. The following is a typical employee document:

{ "id": 1234, "firstName": "John", "lastName": "Smith", "gender": "Male", "dateOfBirth": "3/21/1967", "emailAddresses":[ { "email": " johnsmith@mydomain.com ", "isPrimary": "true" }, { "email": " jsmith@someotherdomain.com ", "isPrimary": "false" } ] } 

Suppose also that I have a separate collection of projects that stores project data that looks something like this:

 { "id": 444, "projectName": "My Construction Project", "projectType": "Construction", "projectTeam":[ { "_id": 2345, "position": "Engineer" }, { "_id": 1234, "position": "Project Manager" } ] } 

If I want to return a list of all my projects along with project teams, how can I handle so that I return all the relevant information about the people in the team, that is, names, email addresses, etc.?

Are these two separate requests? One for projects, and another for people whose identifier appears in the project collection?

If so, how do I insert data about people, that is, full names, email addresses? Then I do a foreach loop in my application to update the data?

If I rely on my application to process all the relevant data, is this not a performance measure that would offset the performance benefits of document databases such as MongoDB?

Thank you for your help.

+6
source share
2 answers

"... how can I make sure that I return all the relevant information about the people in the team, that is, full names, email addresses, etc. Are these two separate requests?"

These are either two separate requests, or you are denormalized in a Project document. In our applications, we make the second request and save the data as normal as possible in the documents.

In fact, it’s not so common to see the "_id" key anywhere except for a top-level document. In addition, for collections in which you intend to have millions of documents, you save storage by keeping keys "short." Instead of "position", not "position" instead of "projectName", "type" instead of "projectType", "pos", instead of "projectName"), use "name". It seems trivial, but it does add up. You will also want to put the index on the team.empId command, so the query “how many projects work in the Joe Average” works well.

 { "_id": 444, "name": "My Construction Project", "type": "Construction", "team":[ { "empId": 2345, "pos": "Engineer" }, { "empId": 1234, "pos": "Project Manager" } ] } 

Another thing you're used to is that you don’t have to write the whole document every time you want to update a separate field or, say, add a new member to the team. You can make targeted updates that uniquely identify the document, but only update a single element of a field or array.

 db.projects.update( { _id : 444 }, { $addToSet : "team" : { "empId": 666, "position": "Minion" } } ); 

2 requests to get one first, but you will pass it.

+7
source

Mongo DB is a document storage database. It supports high availability and scalability.

To return a list of all your projects along with the project team (details) according to my understanding, you will need to run 2 queries. Since mongoDb does not have FK restrictions, we need to maintain it at the program level. Instead of FK restrictions, 1) if the data is less, then we can insert the data as an auxiliary document. 2), and not a normalized way to design db, in MongoDb we have to design according to the access pattern. that is, how we need to request data more likely. (However, the time for updating is longer (slow), but at the end of the user, the performance mainly depends on the reading activity, which will be better than the RDBMS).

The following link provides a mongo db certificate course for free. Mongo DB University They also have a forum that is pretty good.

0
source

Source: https://habr.com/ru/post/974557/


All Articles