0

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

Community
  • 1
  • 1
VISHAL PRAKASH
  • 49
  • 1
  • 10
  • Possible duplicate of [How do I perform the SQL Join equivalent in MongoDB?](http://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb) – harry-potter Sep 29 '16 at 07:50
  • I have already mentioned the link in my question. Please read the question properly – VISHAL PRAKASH Sep 29 '16 at 08:36

0 Answers0