SlideShare ist ein Scribd-Unternehmen logo
1 von 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 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)
};
● 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: Prasoon.sharma1983@gmail.com

Weitere ähnliche Inhalte

Ähnlich wie How to implement joins in mongo db

introtomongodb
introtomongodbintrotomongodb
introtomongodb
saikiran
 
need help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docxneed help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docx
niraj57
 

Ähnlich wie How to implement joins in mongo db (20)

Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
 
introtomongodb
introtomongodbintrotomongodb
introtomongodb
 
MongoDB-presentation.pptx
MongoDB-presentation.pptxMongoDB-presentation.pptx
MongoDB-presentation.pptx
 
Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB installation,CRUD operation & JavaScript shell
MongoDB installation,CRUD operation & JavaScript shellMongoDB installation,CRUD operation & JavaScript shell
MongoDB installation,CRUD operation & JavaScript shell
 
need help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docxneed help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docx
 
MongoDB
MongoDBMongoDB
MongoDB
 
mongo.pptx
mongo.pptxmongo.pptx
mongo.pptx
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg Solutions
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for Beginners
 
Accessing data through hibernate: what DBAs should tell to developers and vic...
Accessing data through hibernate: what DBAs should tell to developers and vic...Accessing data through hibernate: what DBAs should tell to developers and vic...
Accessing data through hibernate: what DBAs should tell to developers and vic...
 
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
 
A quick tour of Mysql 8 roles
A quick tour of Mysql 8 rolesA quick tour of Mysql 8 roles
A quick tour of Mysql 8 roles
 
Getting Started with MongoDB
Getting Started with MongoDBGetting Started with MongoDB
Getting Started with MongoDB
 
Lobos Introduction
Lobos IntroductionLobos Introduction
Lobos Introduction
 
SCons an Introduction
SCons an IntroductionSCons an Introduction
SCons an Introduction
 
SQL and Integrity Constraints (2).pptx
SQL and Integrity Constraints (2).pptxSQL and Integrity Constraints (2).pptx
SQL and Integrity Constraints (2).pptx
 
The Django Book chapter 5 Models
The Django Book chapter 5 ModelsThe Django Book chapter 5 Models
The Django Book chapter 5 Models
 
Mongoose and MongoDB 101
Mongoose and MongoDB 101Mongoose and MongoDB 101
Mongoose and MongoDB 101
 

How to implement joins in mongo db

  • 1. 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)
  • 2. }; ● 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: Prasoon.sharma1983@gmail.com