I am new to mongodb and I am trying to join two collections.
db.count.insert([
{
"[\"job_id\"]" : 543,
"count": 12,
"name": "vishal"
},
{
"[\"job_id\"]" : 543,
"count": 111,
"name": "neha"
},
{
"[\"job_id\"]" : 543,
"count": 1121,
"name": "rohan"
},
{
"[\"job_id\"]" : 543,
"count": 15,
"name": "vishal"
}
]);
db.names.insert([
{
"[\"job_id\"]" : 543,
"name": "neha"
},
{
"[\"job_id\"]" : 543,
"name": "rohan"
},
{
"[\"job_id\"]" : 543,
"name": "vishal"
}
])
Now I want to join the data on the basis of name so I wrote the following code:
db.page_view.find().forEach(
function (newView) {
newView.jobid = db.ad_click.findOne( { "name": newView.name} );
db.viewsReloaded.insert(newView);
}
);
How can I do the same for job_id? I tried the following code but it throws error.
db.page_view.find().forEach(
function (newView) {
newView.jobid = db.ad_click.findOne( { "[\"job_id\"]": newView.job_id} );
db.viewsReloaded2.insert(newView);
}
);
What should I do? Please Help!
P.S: I got the above code for join from this link