7

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?

KenEucker
  • 4,932
  • 5
  • 22
  • 28

4 Answers4

8

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.

Kay
  • 624
  • 1
  • 7
  • 17
  • 1
    Thank you for the followup here. I've marked this as the answer since it does, in fact, answer the question. – KenEucker Dec 09 '19 at 22:02
3

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.

Mark Hillick
  • 6,853
  • 1
  • 19
  • 23
  • Maybe I am confusing my own problem here. I get that joins are not in MongoDb, but the use case that I am thinking of doesn't necessarily require joins (I think). I've updated my question with an example of what I want. – KenEucker Jul 16 '12 at 17:33
1

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.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 1
    DBRefs are just the formalized way of storing a foreign `_id` in one collection that can be followed with a separate query to retrieve it. – JohnnyHK Jul 16 '12 at 17:04
0

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

In addition to this answer, mongodb now has on-demand materialized views. In a nutshell, this feature allows you to use aggregate and $merge (in 4.2) to create/update a quick view collection that you can query from faster. The strategy is used to update the quick view collection whenever the main collection has a record change. This has the side effect unlike SQL of increasing your data storage size. But the benefits can be huge depending on your querying needs.

jtlindsey
  • 4,346
  • 4
  • 45
  • 73