MongoDB/Morphia and Joins

317 views
Skip to first unread message

Apen

unread,
Aug 5, 2015, 3:06:50 AM8/5/15
to mongodb-user
Hi Everyone,

I am fairly new to the community of MongoDB and I had a query regarding: "How does MongoDB support joins?" I have gone through much of the documentation but couldn't find anything which could answer my curiosity. I would like to know a detailed explanation of how MongoDB works with joins and how this is implemented. 

Also I would like to understand how Morphia would be using @Reference annotation to implement this join functionality as both of these questions are much related to each other.

Please help me understand it better and throw some light.

Thank you

Rhys Campbell

unread,
Aug 5, 2015, 4:44:52 AM8/5/15
to mongodb-user
Hi,

There are no joins in MongoDB. You either need to denormalise or perform the joins in your application.

There is a detailed discussion of it here...


Rhys

Girish Websight

unread,
Aug 5, 2015, 9:19:51 AM8/5/15
to mongodb-user
hi

Apen

unread,
Aug 6, 2015, 5:05:20 PM8/6/15
to mongodb-user
Hi Rhys,

Thank for you for the response.

I have learnt how to perform joins at the application level. I wanted to know how a join or @Reference annotation in Morphia works, under the hood. Please help me out if you can.

Thanks

Justin Lee

unread,
Aug 6, 2015, 5:10:13 PM8/6/15
to mongod...@googlegroups.com
When Morphia sees a field with @Reference on it, it will transparently make another query against the mapped collection for that ID value and set the field accordingly.  Because of the multiple query nature of @Reference fields, you shouldn't over use them.  You can also mark them as lazy if you don't need to fetch the referenced documents every time.  In this way, the query isn't executed until you explicitly ask for that field.

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "10gen.com", "antwerkz.com" ],
  location : "New York, NY" }

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/6b3ba520-ec89-43ed-b45e-eb4ab635dd56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Apen

unread,
Aug 6, 2015, 5:21:47 PM8/6/15
to mongodb-user
Hi Justin,

Thank for the response.

I wanted something more to understand as what would the transparent query do to map the collection and set the field. This is not just for implementing at the application level, but I am very keen on understanding it under the hood. So can you please let me know what exactly happens when @Reference is used and what is carried out transparently for mapping ID to collection.

It would be of great help,

Thanks again.

Justin Lee

unread,
Aug 7, 2015, 10:13:02 AM8/7/15
to mongod...@googlegroups.com
When that object is being loaded, when the mapper finds a field with @Reference on it, it makes another query to fetch that object and then assigns that field with the reference to the newly loaded object.  Then it proceeds with loading the rest of the original object.

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "10gen.com", "antwerkz.com" ],
  location : "New York, NY" }

Apen

unread,
Aug 9, 2015, 10:33:57 PM8/9/15
to mongodb-user
Thank you Justin.

Can you please point me to a material and documentation where I could understand more with the flow and look at the mapper code and assigning of objects in @Reference annotation. It would certainly help me understand the bigger picture.

Thanks.
Reply all
Reply to author
Forward
0 new messages