Simple join query

Data set:

db.createCollection(“employee”);
db.employee.insert(
{
_id: 1,
name: { first: ‘John’, last: ‘Backus’ },
city: ‘New York’,
department:1
}
);

db.employee.insert(
{
_id: 2,
name: { first: ‘Merry’, last: ‘Desuja’ },
city: ‘London’,
department:2
}
);
db.createCollection(“department”);
db.department.insert(
{
_id: 1,
department: ‘Manager’
}
);

db.department.insert(
{
_id: 2,
department: ‘Accountant’
}
);

Join Query on department field:

function addDepartmentInEmployee(){
db.department.find().forEach(function(obj){
var id=obj._id.valueOf();
var id1=obj.department;
print(id);
db.employee.update({department:id}, {$set:{“post”:id1}});
});
}
addDepartmentInEmployee();