-3

I had two collection I want to perform a kind of left join but want to perform through a router API. I am new to node js. Can you please help me in it.

  • can you explain more what youre trying to do? – mast3rd3mon Apr 26 '17 at 09:43
  • I have a collection say books which has ID and have a second collection say BookSale which has bookID. I want to join both collection based on ID and BookID and get an collection. I want to do it through an API call in router. – Pratik Raj Apr 26 '17 at 09:59
  • are you wanting the api call to join them or do you want to join them then do the api call? – mast3rd3mon Apr 26 '17 at 10:02
  • when i call an api then it will join both collections and return me a collection or array. – Pratik Raj Apr 26 '17 at 10:03
  • right so it joins them before. could you provide an example in your question of the layout, so to speak, of what you want? – mast3rd3mon Apr 26 '17 at 10:11
  • 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) – Amine Apr 26 '17 at 10:22
  • dont think so, pratik raj is asking how to do this in the javascript code, not sql code – mast3rd3mon Apr 26 '17 at 10:58

1 Answers1

-1

For me you have three solutions:

1) Code it yourself with Javascript and doing one function with two Api calls (not good at all)

2) You have the lookup solution which is detailed in this other post (seems the right solution)

EDIT:

3) The third solution is also in the same post and it looks like this : (what most people are doing but as optimized as option 2), it is not exactly the solution you want since it it doesnt merge both collections at once, but you end up with the same kind of data that you wanted

db.collection1.find().forEach(
    function (document) {
        document.collection2 = db.collection2.find( { "collection1": document._id  } ).toArray();
        document.collection3 = db.collection3.find( { "_id": { $in: document.collection3 }  } ).toArray();
    }
);
Community
  • 1
  • 1
Amine
  • 349
  • 2
  • 16