0

Before I ask my question I would like to say I read these information already:

MongoDB: Combine data from multiple collections into one..how?

Merge two collections on mongodb

Merging two collections in MongoDB

MongoDB and “joins” [duplicate]

but non of them help me my question is as below: I have multiple collection that all of them include country information but from deffrent source with diffrent code as below: collection : A

{
  "_id" : ObjectId("59378477c64086fff293008b"),
  "country_code_2_letter" : "AF",
  "country_code_3_letter" : "AFG",
  "country_name" : "Afghanistan"
},

collection : B

{
  "_id" : ObjectId("59e6d1b705f5978143c46455"),
  "CountryCode" : "MA05110083",
  "CountryName" : "Afghanistan"
},

I would like to merge these 2 collection and have 1 collection as below:

{
  "_id" : ObjectId("59e6d1b705f5978143c46455"),
  "country_name" : "Afghanistan",
  "country_code_2_letter" : "AF",
  "country_code_3_letter" : "AFG",
  "CountryCodeCollectionB" : "MA05110083"
}

and if "country_name" : "Afghanistan" not exist on collection A add whole data to collection A with change "CountryCode" to "country_name" and "CountryCode" to "CountryCodeCollectionB" Ps: I have both JSON file and collection becuase same Scenario I have for another collections that should update every month the easiest way will be highly appritiated

Orlandster
  • 4,706
  • 2
  • 30
  • 45
John Doe
  • 55
  • 1
  • 10
  • 1
    Nice to know you read something. Now please show what you actually tried and explain why the existing answers don't apply to you. Because what you seem to be describing is a "left join" and this is exactly what `$lookup` does by default. – Neil Lunn Oct 18 '17 at 07:12
  • @NeilLunn as I undrestand(if not please correct me) from `$lookup` its add new array to collection but I dont need to add array – John Doe Oct 18 '17 at 07:19
  • 1
    `$lookup` does not "add anything" to a collection. It's a method of `.aggregate()` which simply returns data. Yes the default is an an "array" and yes that is required for a "left join" without destroying the unmatched parent. But there is nothing stopping you from transforming that array. So again show us why you think this does not apply to you because I really don't see any reason why it does not. – Neil Lunn Oct 18 '17 at 07:23
  • @NeilLunn I did `db.country.aggregate([{$lookup:{from:"collection : B",localField:"country_name",foreignField:"country_name",as:"country_d s"}}])` but now I have 2 seperate of each country now but I need 1 country with new field – John Doe Oct 18 '17 at 07:37
  • 1
    You really should add details like code in the question itself. So yes that's **one** pipeline stage. You do realize pipelines typically consist of "multiple" stages? So perhaps you should look for what you do to the array. There are quite a few more aggregation operators than `$lookup` alone. Also add your code to the question and the output you receive from the sample documents provided. – Neil Lunn Oct 18 '17 at 07:40

0 Answers0