Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join MongoDB collections in Python?

How to join ( in a sense of INNER JOIN from SQL ) two MongoDB collections in Python ? Do I need to use native map/reduce javascript code or to do this in PyMongo ? How to solve this with less code ?

like image 417
Damir Avatar asked Mar 19 '12 13:03

Damir


1 Answers

Mongo stores data differently than in a traditional relational database, and does not support table joins as one might be used to in a SQL database. There is a note on this in the "Database References" documentation. http://www.mongodb.org/display/DOCS/Database+References

If possible, it is preferable to store all data in a single collection. If this is not possible, separate queries will have to be performed on all of the databases, and the data merged programmatically.

As per the documentation, it is possible to link documents in separate collections, either directly or with db references. Separate queries will still have to be performed on each collection.

Similar questions have been asked before. (I have included some links below.) Hopefully the responses will give you some additional insight into how data is stored in MongoDB, and how you can restructure your documents and/or queries such that you can retrieve the data that you need with the fewest number of requests to the database.

Good luck!

MongoDB and "joins"

How do I perform the SQL Join equivalent in MongoDB?

How to join query in mongodb?

"Beginner question regarding joins" http://groups.google.com/group/mongodb-user/browse_thread/thread/edfcf8bd270274f9/

like image 75
Marc Avatar answered Oct 20 '22 08:10

Marc