View SQL in mongodb

I am currently evaluating mongodb for a project that I started, but I cannot find any information on what the equivalent of SQL representation in mongodb would be. What I need that the SQL view provides is to combine data from different tables (collections) into one collection.

I want nothing but merge some documents together and mark them as one document. Here is an example:

I have the following documents: cc_address us_address billing address shipping_address

But in my application, I would like to see all my addresses and be able to manage them in one document.

In other cases, I may just want a couple of fields from collections:

I have the following documents: fb_contact twitter_contact google_contact reddit_contact

Each of these documents has fields that are aligned, for example, first name and surname firstname, but also have fields that are not aligned. I would like to compile them into a single document that contains only fields that are aligned.

Can this be done using SQL views correctly? Can I execute this function in MongoDb?

+6
source share
3 answers

The question is already quite old. However, since mongodb v3.2 you can use $ lookup to combine data from different collections together until the collections are protected. Since mongodb v3.4 you can also create read-only .

+6
source

There are no "unions" in MongoDB. As JonnyHK said, you can either swing your data, or use embedded documents, or perform multiple queries

However, you can also use Map-Reduce .

or if you are ready to use the development branch, can you test the new aggregation structure although perhaps this is too much? This new structure will be in the upcoming 2.2 , which is ready for production as opposed to 2.1.x.

There is also a SQL-Mongo diagram that can help you learn.

Update. Based on your re-editing, you don't need a Map-Reduce or Aggregation Framework, because you're just asking.

You essentially make connections, request multiple documents, and combine the results. The place for this is in your client-side application.

+3
source

MongoDB queries never span more than one collection since there is no union support. Therefore, if you have related data that you need in the query results, you must either add this related data to the collection that you are requesting (i.e. Denormalize your data) or make a separate request for it from another collection.

+1
source

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


All Articles