Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL view in mongodb

Tags:

mongodb

I am currently evaluating mongodb for a project I have started but I can't find any information on what the equivalent of an SQL view in mongodb would be. What I need, that an SQL view provides, is to lump together data from different tables (collections) into a single collection.

I want nothing more than to clump some documents together and label them as a single document. Here's an example:

I have the following documents: cc_address us_address billing_address shipping_address

But in my application, I'd like to see all of my addresses and be able to manage them in a single 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 have fields that align, like firstname lastname and email, but they also have fields that don't align. I'd like to be able to compile them into a single document that only contains the fields that align.

This can be accomplished by Views in SQL correct? Can I accomplish this kind of functionality in MongoDb?

like image 674
KenEucker Avatar asked Jul 16 '12 15:07

KenEucker


People also ask

What are views in MongoDB?

Views act as read-only collections, and are computed on demand during read operations. You must create views in the same database as the source collection. MongoDB executes read operations on views as part of the underlying aggregation pipeline. A view definition pipeline cannot include the $out or the $merge stage.

Can I use SQL query in MongoDB?

Through the coherent structure of tables and the sophisticated transformation that Data Virtuality performs automatically, MongoDB can easily be accessed and queried with just regular SQL statements, and the content can be combined with other data sources, such as relational databases.


3 Answers

The question is quite old already. However, since mongodb v3.2 you can use $lookup in order to join data of different collections together as long as the collections are unsharded. Since mongodb v3.4 you can also create read-only views.

like image 59
Kay Avatar answered Oct 05 '22 23:10

Kay


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

However, you could also use Map-Reduce.

or if you're prepared to use the development branch, you could test the new aggregation framework though maybe it's too much? This new framework will be in the soon-to-be-released 2.2, which is production-ready unlike 2.1.x.

Here's the SQL-Mongo chart also, which may be of some help in your learning.

Update: Based on your re-edit, you don't need Map-Reduce or the Aggregation Framework because you're just querying.

You're essentially doing joins, querying multiple documents and merging the results. The place to do this is within your application on the client-side.

like image 41
Mark Hillick Avatar answered Oct 05 '22 21:10

Mark Hillick


MongoDB queries never span more than a single collection as there is no support for joins. So if you have related data you need available in the results of a query you must either add that related data to the collection you're querying (i.e. denormalize your data), or make a separate query for it from another collection.

like image 36
JohnnyHK Avatar answered Oct 05 '22 23:10

JohnnyHK