2
How to implement joins in mongo db? Joins in mongo db are implemented using map reduce terminology. Lets consider a scenario where you have a collestion of users and these users do have roles assigned to them. Now consider we have to apply join to determine role of user. First we need to connect to mongo > mongo We need to select database to interact. This command will select faculties database if it already exists or create new if it doesn’t exists > use faculties Now we need to create collections over this db (which are like tables in mysql). Let’s create two collections one is users and other one i s roles. > db.createCollection(‘users’) > db.createCollection(‘roles’) Now next step is to add some record sets to these collections > db.users.insert({name: ‘Jakes’, role:1}) > db.users.insert({name: ‘Jakes’, role:2}) > db.roles.insert({role_id:1, rolename:’HOD’}) > db.roles.insert({role_id:2, rolename:’Director’}) Now lets start process of applying join. For this first we need to create map function. > var map = function() { var output = {name: this.name, rolename:db.roles.findOne({role_id:this.role}).rolename} emit(this._id, output)

How to implement joins in mongo db

Embed Size (px)

Citation preview

Page 1: How to implement joins in mongo db

How to implement joins in mongo db?

Joins in mongo db are implemented using map reduce terminology.

Lets consider a scenario where you have a collestion of users and these

users do have roles assigned to them. Now consider we have to apply join

to determine role of user.

● First we need to connect to mongo

> mongo

● We need to select database to interact. This command will select

faculties database if it already exists or create new if it doesn’t exists

> use faculties

● Now we need to create collections over this db (which are like tables

in mysql). Let’s create two collections one is users and other one is

roles.

> db.createCollection(‘users’)

> db.createCollection(‘roles’)

● Now next step is to add some record sets to these collections

> db.users.insert({name: ‘Jakes’, role:1})

> db.users.insert({name: ‘Jakes’, role:2})

> db.roles.insert({role_id:1, rolename:’HOD’})

> db.roles.insert({role_id:2, rolename:’Director’})

● Now lets start process of applying join. For this first we need to create

map function.

> var map = function()

{

var output = {name: this.name,

rolename:db.roles.findOne({role_id:this.role}).rolename}

emit(this._id, output)

Page 2: How to implement joins in mongo db

};

● Now time to create map function

> var reduce = { function(key, values) {

var outs = {name:null, rolename:null}

values.forEach(function(v) {

if(outs.name == null) { outs.name = v.name }

if(outs.rolename == null) {outs.rolename = v.rolename }

});

return outs;

};

● Now time to apply map reduce over the collection to be joined.

> db.users.mapReduce(map,reduce,{out:’result_coll’})

● Now result of join will be stored in result_coll, to check the output you

can query result_coll.

> db.result_coll.find()

[Thanks for reading]

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

Author: Prasoon Sharma

Email: [email protected]