$merge
์ด ํ์ด์ง์ ๋ด์ฉ
$merge
์ง๊ณ ํ์ดํ๋ผ์ธ ์ ๊ฒฐ๊ณผ๋ฅผ Atlas cluster์ ์์ ์ปฌ๋ ์
์ ์๋๋ค. ๊ทธ๋ฐ ๋ค์ Atlas Data Federation์ Atlas cluster์์ ๋ก์ปฌ๋ก $merge
๋ฅผ ์คํ ํ์ฌ ์ฒญํฌ ๋จ์์ ๋ฐ์ดํฐ๋ฅผ ๋์ ์ปฌ๋ ์
์ ๋ณํฉํฉ๋๋ค . $merge
์์
์ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์ต์ํ ์ผ๋ถ ๋ฐ์ดํฐ๊ฐ ๋์ ์ปฌ๋ ์
์ ๊ธฐ๋ก๋ฉ๋๋ค.
Atlas Data Federation์์ $merge
๋ ๋ค์์ ์ํํ ์ ์์ต๋๋ค.
์ง์๋๋ ์ฐํฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ์คํด์ค ์ ์ฅ์ ์ค ํ๋์์ ๋ฐ์ดํฐ๋ฅผ ์๋๋ค.
๋์ผํ Atlas ํ๋ก์ ํธ ๋ด์์ ๋์ผํ๊ฑฐ๋ ๋ค๋ฅธ Atlas cluster, ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋๋ ์ปฌ๋ ์ ์ ์๋๋ค.
Atlas Data Federation์ Atlas cluster์ ์ฐ๊ธฐ๋ฅผ ํ์ฉํ๊ธฐ ์ํด ํ์ into
ํ๋์ ๋์ฒด ๊ตฌ๋ฌธ ์ ๋์
ํฉ๋๋ค. Atlas Data Federation์ $merge
์ ์ค๋ช
๋ ๋๋ก ๋ค๋ฅธ ๋ชจ๋ ํ๋๋ฅผ ์ง์ํฉ๋๋ค.
์์ธํ ๋ด์ฉ์ $merge
ํ์ดํ๋ผ์ธ ๋จ๊ณ๋ฅผ ์ฐธ์กฐํ์ธ์.
ํ์ํ ๊ถํ
$merge
๋ฅผ ์ฌ์ฉํ์ฌ Atlas cluster์ ์ปฌ๋ ์
์ ์ฐ๋ ค๋ฉด ๋ค์ ๊ถํ์ ๊ฐ์ง ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ์์ฌ์ผ ํฉ๋๋ค.
๊ณ ๋ ค ์ฌํญ
์ ๊ทธ๋ฆฌ๊ฒ์ด์
์ด ์คํจํ๋ฉด Atlas Data Federation์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ธฐ ์ ์ $merge
๊ฐ ์๋ฃํ ์ฐ๊ธฐ๋ฅผ ๋กค๋ฐฑํ์ง ์์ต๋๋ค.
๊ตฌ๋ฌธ
{ "$merge": { "into": { "atlas": { "projectId": "<atlas-project-ID>", "clusterName": "<atlas-cluster-name>", "db": "<atlas-database-name>", "coll": "<atlas-collection-name>" } }, "on": "<identifier field>"|[ "<identifier field1>", ...], "let": { <variables> }, "whenMatched": "replace|keepExisting|merge|fail|pipeline", "whenNotMatched": "insert|discard|fail" } }
ํ๋
์ด ์น์
์์๋ Atlas Data Federation์ด into
ํ๋์ ์ ๊ณตํ๋ ๋์ฒด ๊ตฌ๋ฌธ์ ์ค๋ช
ํฉ๋๋ค.
ํ๋ | ์ ํ | ์ค๋ช
| ํ์์ฑ |
---|---|---|---|
atlas | ๊ฐ์ฒด | aggregation pipeline์์ ๋ฌธ์๋ฅผ ์์ฑํ ์์น์
๋๋ค. | ํ์ ์ฌํญ |
clusterName | ๋ฌธ์์ด | Atlas cluster์ ์ด๋ฆ. | ํ์ ์ฌํญ |
coll | ๋ฌธ์์ด | Atlas cluster์ ์๋ ์ปฌ๋ ์
์ ์ด๋ฆ์
๋๋ค. | ํ์ ์ฌํญ |
db | ๋ฌธ์์ด | ์ปฌ๋ ์
์ด ํฌํจ๋ Atlas cluster์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ์
๋๋ค. | ํ์ ์ฌํญ |
projectId | ๋ฌธ์์ด | Atlas cluster๊ฐ ํฌํจ๋ ํ๋ก์ ํธ์ ๊ณ ์ ์๋ณ์์
๋๋ค. ์ด๋ ์ฐํฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ์คํด์ค๊ฐ ํฌํจ๋ ํ๋ก์ ํธ์ ID์
๋๋ค. ์๋ตํ๋ฉด ์ฐํฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ์คํด์ค๊ฐ ํฌํจ๋ ํ๋ก์ ํธ์ ID๊ฐ ๊ธฐ๋ณธ๊ฐ์ด ๋ฉ๋๋ค. | ์ต์
|
๋ค๋ฅธ ํ๋ on
, let
, whenMatched
๋ฐ whenNotMatched
์ ๋ํด ์์ธํ ์์๋ณด๋ ค๋ฉด $merge
์ ๋ํ MongoDB ์๋ฒ ์ค๋ช
์๋ฅผ ์ฐธ์กฐํ์ธ์.
์ฐธ๊ณ
์ฌ๋ฌ ํ๋์์ on
๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ``on`` ์๋ณ์ ํ๋์ ๋ณตํฉ ๊ณ ์ ์ธ๋ฑ์ค ๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
์ต์
์ต์
| ์ ํ | ์ค๋ช
| ํ์์ฑ | |
---|---|---|---|---|
background | ๋ถ์ธ | ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์ ๊ทธ๋ฆฌ๊ฒ์ด์
์์
์ ์คํํ๋๋ก ํ๋๊ทธ๋ฅผ ์ง์ ํฉ๋๋ค. ์๋ตํ๋ฉด ๊ธฐ๋ณธ๊ฐ์
์ฟผ๋ฆฌ๊ฐ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๊ณ์ ์คํ๋๋ ๋์ ํ์ฌ ์คํ ์ค์ธ ์ฟผ๋ฆฌ๊ฐ ์๋ฃ๋๊ฑฐ๋ ํตํฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ์คํด์ค ์ฐ๊ฒฐ์ด ๋๊ธธ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ค๋ฅธ ์ ์ฟผ๋ฆฌ๋ฅผ ์ ์ถํ๋ ค๋ ๊ฒฝ์ฐ ์ด ์ต์ ์ ์ฌ์ฉํ์ญ์์ค. | ์ต์
|
์ค๋ณต ๋ฌธ์ ID ํด๊ฒฐ
์์นด์ด๋ธ ๋๋ ๋ฐ์ดํฐ ์ ์ฅ์์์ Atlas cluster๋ก ๋ฌธ์๋ฅผ ์์ฑํ ๋ ๋ฌธ์์ ์ค๋ณต๋ _id
ํ๋๊ฐ ์์ ์ ์์ต๋๋ค. ์ด ์น์
์์๋ Atlas Data Federation์ด ์ค๋ณต์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ๊ณผ ์ ๊ทธ๋ฆฌ๊ฒ์ด์
ํ์ดํ๋ผ์ธ์์ ์ค๋ณต์ ํด๊ฒฐํ๊ธฐ ์ํ ๊ถ์ฅ ์ฌํญ์ ์ค๋ช
ํฉ๋๋ค.
Atlas Data Federation์์ ์ค๋ณต ID ํ์ธ
Atlas Data Federation์ ์ค๋ณต์ ํด๊ฒฐํ๊ธฐ ์ํด ๋ค์์ ์ํํฉ๋๋ค.
๋ฌธ์๋ฅผ ๋ฐ๋ ์์๋๋ก ์ค๋ณต์ด ๋ฐ๊ฒฌ๋ ๋๊น์ง ๋ฌธ์๋ฅผ ๋ฐ๋ ์์๋๋ก Atlas ์ปฌ๋ ์
X
์ ๋ฌธ์๋ฅผ ์๋๋ค.์ค๋ณต๋
_id
ํ๋๊ฐ ์๋ ๋ฌธ์์ ๋ชจ๋ ํ์ ๋ฌธ์๋ฅผ ์ Atlas ์ปฌ๋ ์ Y
์ ์๋๋ค.์ง์ ๋
$merge
๋จ๊ณ๋ฅผ ์คํํ์ฌ ์ปฌ๋ ์ Y
๋ฅผ ์ปฌ๋ ์ X
์ ๋ณํฉํฉ๋๋ค.๊ฒฐ๊ณผ ๋ฌธ์๋ฅผ ์ง์ ๋ Atlas cluster์ ๋์ ์ปฌ๋ ์ ์ ์๋๋ค.
์ฐธ๊ณ
Atlas Data Federation์ _id
ํ๋์์ ์ค๋ณต๋ ๊ฐ๋ง ํ์ธํฉ๋๋ค. ๊ณ ์ ์ธ๋ฑ์ค๊ฐ ์๋ ๋ค๋ฅธ ํ๋์ ์ค๋ณต ๊ฐ์ ํด๊ฒฐ๋์ง ์์ต๋๋ค.
์ค๋ณต ID ์์ ํ๊ธฐ
๋ค์์ ์ํํ์ฌ ์ค๋ณต๋ _id
ํ๋๋ฅผ ์์ ํ ์ ์์ต๋๋ค.
ํ์ดํ๋ผ์ธ์
$sort
๋จ๊ณ๋ฅผ ํฌํจํ์ฌ Atlas Data Federation์ด ๊ฒฐ๊ณผ ๋ฌธ์๋ฅผ ์ฒ๋ฆฌํด์ผ ํ๋ ์์๋ฅผ ์ง์ ํฉ๋๋ค.$merge
๋จ๊ณ๋ก ์ ์ ๋๋ ๋ฌธ์์ ์์์ ๋ฐ๋ผ$merge
๋จ๊ณ์whenMatched
๋ฐwhenNotMatched
์ต์ ๊ฐ์ ์ ์คํ๊ฒ ์ ํํฉ๋๋ค.์์
๋ค์ ์์์์๋
whenMatched
์ต์ ์ดkeepExisting
๋๋replace
๋ก ์ค์ ๋ ๊ฒฝ์ฐ Atlas Data Federation์ด$merge
๋จ๊ณ์์ ์ค๋ณต์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค. ์ด ์์์์๋ ๋ค์ ๋ฌธ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.{ "_id" : 1, "state" : "FL" }, { "_id" : 1, "state" : "NJ" }, { "_id" : 2, "state" : "TX" } whenNotMatched: discard
์ต์ ์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค.์์
์ด ์์์์๋ ๋ค์ ๋ฌธ์๋ฅผ ์ฌ์ฉํ์ฌ
whenNotMatched
์ต์ ์ดdiscard
๋ก ์ค์ ๋ ๊ฒฝ์ฐ Atlas Data Federation์ด ์ค๋ณต์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค.{ "_id" : 1, "state" : "AZ" }, { "_id" : 1, "state" : "CA" }, { "_id" : 2, "state" : "NJ" }, { "_id" : 3, "state" : "NY" }, { "_id" : 4, "state" : "TX" } ์์ ๋์ด๋ ๋ฌธ์์ ๋ํด ๋ค์ ํ์ดํ๋ผ์ธ์ ์คํํ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
db.archivecoll.aggregate([ { "$sort": { "_id": 1, "state": 1, } }, { "$merge": { "into": { "atlas": { "clusterName": "clustername", "db": "clusterdb", "coll": "clustercoll" } }, "on": "_id", "whenMatched": "replace", "whenNotMatched": "discard" } } ]) Atlas Data Federation์
X
๋ฐY
๋ผ๋ ๋ ์ปฌ๋ ์ ์ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ์๋๋ค.Atlas Data Federation์
Y
์ปฌ๋ ์ ์ ๋ฌธ์๋ฅผX
์ปฌ๋ ์ ์ผ๋ก ๋ณํฉํฉ๋๋ค. ํ์ดํ๋ผ์ธ์whenMatched: replace
์ต์ ์ ๊ฒฝ์ฐ Atlas Data Federation์ ์ปฌ๋ ์ X
์_id: 1
๋ฌธ์๋ฅผ ์ปฌ๋ ์ Y
์_id: 1
์ด ์๋ ๋ฌธ์๋ก ๋์ฒดํฉ๋๋ค. ํ์ดํ๋ผ์ธ์whenNotMatched: discard
์ต์ ์ ๊ฒฝ์ฐ Atlas Data Federation์ ์ปฌ๋ ์ X
์ ๋ฌธ์์ ์ผ์นํ์ง ์๋ ์ปฌ๋ ์ Y
์ ๋ฌธ์๋ฅผ ์ญ์ ํฉ๋๋ค. ๋ฐ๋ผ์ ์ค๋ณต ํญ๋ชฉ์ด ์๋ ํ์ดํ๋ผ์ธ์ ๊ฒฐ๊ณผ์๋ ๋ค์ ๋ฌธ์๊ฐ ํฌํจ๋ฉ๋๋ค.{ "_id" : 1, "state" : "CA" } ๊ทธ๋ฐ ๋ค์ Atlas Data Federation์ ์ด ๋ฌธ์๋ฅผ ์ง์ ๋ Atlas cluster์ ๋์ ์ปฌ๋ ์ ์ ๋ณํฉํฉ๋๋ค.
์์
(์ผ)๋ก ๋ฐ์ดํฐ ๋ณํฉ $merge
๋ค์ ์์ $merge
๊ตฌ๋ฌธ์ ์ด๋ฆ์ด myTestCluster
์ธ Atlas cluster์ sampleDB.mySampleData
์ปฌ๋ ์
์ ๊ฒฐ๊ณผ๋ฅผ ์๋๋ค. ์ด ์์ ์์๋ ํ๋ก์ ํธ ID๋ฅผ ์ง์ ํ์ง ์์ต๋๋ค. $merge
๋จ๊ณ์์๋ ์ฐํฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ์คํด์ค๊ฐ ํฌํจ๋ ํ๋ก์ ํธ์ ID๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์์
1 db.mySampleData.aggregate( 2 [ 3 { 4 "$merge": { 5 "into": { 6 "atlas": { 7 "clusterName": "myTestCluster", 8 "db": "sampleDB", 9 "coll": "mySampleData" 10 } 11 }, 12 ... 13 } 14 } 15 ] 16 )
๋ฐฑ๊ทธ๋ผ์ด๋์์ $merge
์คํ
๋ค์ ์์ $merge
๊ตฌ๋ฌธ์ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์ด๋ฆ์ด myTestCluster
์ธ Atlas cluster์ sampleDB.mySampleData
์ปฌ๋ ์
์ ๊ฒฐ๊ณผ๋ฅผ ์๋๋ค. ์ด ์์ ์์๋ ํ๋ก์ ํธ ID๋ฅผ ์ง์ ํ์ง ์์ต๋๋ค. $merge
๋จ๊ณ์์๋ ์ฐํฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ์คํด์ค๊ฐ ํฌํจ๋ ํ๋ก์ ํธ์ ID๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์์
1 db.mySampleData.aggregate( 2 [ 3 { 4 "$merge": { 5 "into": { 6 "atlas": { 7 "clusterName": "myTestCluster", 8 "db": "sampleDB", 9 "coll": "mySampleData" 10 } 11 }, 12 ... 13 } 14 } 15 ], 16 { "background" : true } 17 )