I have updated this blog . As last description was creating some misunderstanding and lot of confusion .
In one of my project , there was a requirement to generate report in .csv format from MongoDB without using any reporting tool and any language .
I had to use only MongoDB query .
In that report , I had to use multiple collection . Since there was not a well structured DB schema .
We are using MongoDB , which is non -relational database . Despite of this fact , there were related collections .
As we know MongoDB is a no-SQL database and doesn’t support joins. But I found an alternate to implement JOINs in MongoDb using Map Reduce.
For Versions below Mongo 2.4:-
Let’s go with a small example :
We have two collections Employee and Department . We have to fetch details of all employees with their department .
1) Create Employee and Department collection .
2) Create a Map . This is a JavaScript function which emits key and value pair and processes for each input document .
3) Create Reduce . This is a JavaScript function which accepts two arguments Key and Value .
4) MongoDB provides mapReduce commmand for Map Reduce operation .
5) ’emp_dept’ is new resulted collection where result will be stored . The Result will be :
For Version Mongo 2.4:-
Go to http://blog.knoldus.com/2014/03/12/easiest-way-to-implement-joins-in-mongodb-2-4/