db.collection.bulkWrite()
๋๋ผ์ด๋ฒ๊ฐ ํฌํจ๋ MongoDB
์ด ํ์ด์ง์์๋ mongosh
๋ฉ์๋์ ๋ํด ์ค๋ช
ํฉ๋๋ค. MongoDB ๋๋ผ์ด๋ฒ์์ ๋์ผํ ๋ฉ์๋๋ฅผ ๋ณด๋ ค๋ฉด ํด๋น ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ํด๋น ํ์ด์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
์ ์
db.collection.bulkWrite()
์คํ ์์๋ฅผ ์ ์ดํ์ฌ ์ฌ๋ฌ ์ฐ๊ธฐ ์์ ์ ์ํํฉ๋๋ค.
๋ฐํํฉ๋๋ค: - ์์
์ด ์ฐ๊ธฐ ๊ณ ๋ ค
์ ํจ๊ป ์คํ๋ ๊ฒฝ์ฐ ๋ถ์ธ
acknowledged
๋ฅผtrue
๋ก, ์ฐ๊ธฐ ๊ณ ๋ ค๊ฐ ๋นํ์ฑํ๋ ๊ฒฝ์ฐfalse
๋ก ๋ฐํํฉ๋๋ค. - ๊ฐ ์ฐ๊ธฐ ์์ ์ ๊ฐ์์ ๋๋ค.
- ์ฑ๊ณต์ ์ผ๋ก ์ฝ์
๋๊ฑฐ๋ ์
์คํธ๋ ๊ฐ ๋ฌธ์์ ๋ํด
_id
๋ฅผ ํฌํจํ๋ ๋ฐฐ์ด์ ๋๋ค.
- ์์
์ด ์ฐ๊ธฐ ๊ณ ๋ ค
ํธํ์ฑ
db.collection.bulkWrite()
์(๋) ๋ค์ ํ๊ฒฝ์์ ํธ์คํ
๋๋ ๋ฐฐํฌ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
MongoDB Atlas: ํด๋ผ์ฐ๋์์์ MongoDB ๋ฐฐํฌ๋ฅผ ์ํ ์์ ๊ด๋ฆฌํ ์๋น์ค
์ฐธ๊ณ
์ด ๋ช ๋ น์ ๋ชจ๋ MongoDB Atlas ํด๋ฌ์คํฐ์์ ์ง์๋ฉ๋๋ค. ๋ชจ๋ ๋ช ๋ น์ ๋ํ Atlas ์ง์์ ๊ดํด ์์ธํ ์์๋ณด๋ ค๋ฉด ์ง์๋์ง ์๋ ๋ช ๋ น์ ์ฐธ์กฐํ์ญ์์ค.
MongoDB Enterprise: MongoDB์ ๊ตฌ๋ ๊ธฐ๋ฐ ์์ฒด ๊ด๋ฆฌ ๋ฒ์
MongoDB Community: MongoDB์ ์์ค ์ฌ์ฉ ๊ฐ๋ฅ ๋ฌด๋ฃ ์์ฒด ๊ด๋ฆฌ ๋ฒ์
์ฐธ๊ณ
Atlas UI ์์๋ ๋๋ ์ฐ๊ธฐ ์์ ์ ์ํํ ์ ์์ต๋๋ค. ์ฌ๋ฌ ๋ฌธ์๋ฅผ ์ฝ์ ํ๋ ค๋ฉด ๋ฌธ์ ๋ฐฐ์ด์ ์ฝ์ ํด์ผ ํฉ๋๋ค. ์์ธํ ์์๋ณด๋ ค๋ฉด Atlas ๋ฌธ์์์ ๋ฌธ์ ์์ฑ, ๋ณด๊ธฐ, ์ ๋ฐ์ดํธ ๋ฐ ์ญ์ ๋ฅผ ์ฐธ์กฐํ์ธ์.
๊ตฌ๋ฌธ
bulkWrite()
๋ฉ์๋์ ํ์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
db.collection.bulkWrite( [ <operation 1>, <operation 2>, ... ], { writeConcern : <document>, ordered : <boolean> } )
bulkWrite()
๋ฉ์๋๋ ๋ค์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
Parameter | ์ ํ | ์ค๋ช
| ||
---|---|---|---|---|
| ๋ฐฐ์ด | ์ฐ๊ธฐ (write) ์์
์ ์ ํจํ ์ฐ์ฐ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ๊ฐ ์์ ์ ์ฌ์ฉ๋ฒ์ ์ฐ๊ธฐ ์์ ์ ์ฐธ์กฐํ์ธ์. | ||
| ๋ฌธ์ | ์ ํ ์ฌํญ์ ๋๋ค. ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ํํํ๋ ๋ฌธ์์ ๋๋ค. ๊ธฐ๋ณธ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ ค๋ฉด ์๋ตํ์ธ์. ํธ๋์ญ์ ์์ ์คํ๋๋ ๊ฒฝ์ฐ ์์ ์ ๋ํ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ๋ช ์์ ์ผ๋ก ์ค์ ํ์ง ๋ง์ธ์. ํธ๋์ญ์ ์ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ํธ๋์ญ์ ๋ฐ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฐธ์กฐํ์ธ์. | ||
| ๋ถ์ธ | ์ ํ ์ฌํญ์
๋๋ค. ์์ ์คํ ์ฐธ์กฐ |
ํ๋
bulkWrite()
์ ์ฐ๊ธฐ ์์
์ ๋ฐฐ์ด์ ๊ฐ์ ธ์์ ๊ฐ ์์
์ ์คํํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์์
์ ์์๋๋ก ์คํ๋ฉ๋๋ค. ์ฐ๊ธฐ ์์
์คํ ์์๋ฅผ ์ ์ดํ๋ ค๋ฉด ์์
์คํ์ ์ฐธ์กฐํ์ธ์.
์ฐ๊ธฐ ์์
insertOne
๋จ์ผ ๋ฌธ์๋ฅผ collection์ ์ฝ์ ํฉ๋๋ค.
db.collection.bulkWrite( [ { insertOne : { "document" : <document> } } ] )
updateOne ๋ฐ updateMany
updateOne
์ ํํฐ์ ์ผ์นํ๋ ์ปฌ๋ ์
์ ๋จ์ผ ๋ฌธ์๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค. ์ฌ๋ฌ ๋ฌธ์๊ฐ ์ผ์นํ๋ ๊ฒฝ์ฐ updateOne
์ ์ฒซ ๋ฒ์งธ ์ผ์นํ๋ ๋ฌธ์๋ง ์
๋ฐ์ดํธํฉ๋๋ค.
db.collection.bulkWrite( [ { updateOne : { "filter": <document>, "update": <document or pipeline>, "upsert": <boolean>, "collation": <document>, "arrayFilters": [ <filterdocument1>, ... ], "hint": <document|string> } } ] )
updateMany
์ ํํฐ์ ์ผ์นํ๋ ์ปฌ๋ ์
์ ๋ชจ๋ ๋ฌธ์๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
db.collection.bulkWrite( [ { updateMany : { "filter" : <document>, "update" : <document or pipeline>, "upsert" : <boolean>, "collation": <document>, "arrayFilters": [ <filterdocument1>, ... ], "hint": <document|string> } } ] )
ํ๋ | ์ฐธ๊ณ ์ฌํญ |
---|---|
| ์
๋ฐ์ดํธ์ ์ ํ ๊ธฐ์ค์
๋๋ค. ๋ฉ์๋์ ๋์ผํ ์ฟผ๋ฆฌ ์ ํ๊ธฐ๋ฅผ |
| ์ํํ ์ ๋ฐ์ดํธ ์์ ์ ๋๋ค. ๋ค์ ์ค ํ๋๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
|
| ์ ํ ์ฌํญ. ์ ์ํธ๋ฅผ ์ํํ ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ถ์ธ์ ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก |
| ์ ํ ์ฌํญ์ ๋๋ค. ํํฐ ๋ฌธ์์ ๋ฐฐ์ด๋ก, ๋ฐฐ์ด ํ๋์ ๋ํ ์ ๋ฐ์ดํธ ์์ ์ ์ํด ์์ ํ ๋ฐฐ์ด ์์๋ฅผ ๊ฒฐ์ ํฉ๋๋ค. |
| ์ ํ ์ฌํญ์ ๋๋ค. ์์ ์ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ ฌ์ ์ง์ ํฉ๋๋ค. |
| ์ ํ ์ฌํญ์
๋๋ค. |
์์ธํ ๋ด์ฉ์ db.collection.updateOne()
๋ฐ db.collection.updateMany()
๋ฅผ ์ฐธ์กฐํ์ธ์.
replaceOne
replaceOne
์ ํํฐ์ ์ผ์นํ๋ ์ปฌ๋ ์
์ ๋จ์ผ ๋ฌธ์๋ฅผ ๋์ฒดํฉ๋๋ค. ์ฌ๋ฌ ๋ฌธ์๊ฐ ์ผ์นํ๋ ๊ฒฝ์ฐ replaceOne
์ ์ฒซ ๋ฒ์งธ ์ผ์นํ๋ ๋ฌธ์๋ง ๋์ฒดํฉ๋๋ค.
db.collection.bulkWrite([ { replaceOne : { "filter" : <document>, "replacement" : <document>, "upsert" : <boolean>, "collation": <document>, "hint": <document|string> } } ] )
ํ๋ | ์ฐธ๊ณ ์ฌํญ |
---|---|
| ๊ต์ฒด ์์
์ ์ ํ ๊ธฐ์ค์
๋๋ค. |
| ๋์ฒด ๋ฌธ์์ ๋๋ค. ๋ฌธ์์๋ ์ ๋ฐ์ดํธ ์ฐ์ฐ์๊ฐ ํฌํจ๋ ์ ์์ต๋๋ค. |
| ์ ํ ์ฌํญ. ์
์ํธ๋ฅผ ์ํํ ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ถ์ธ์
๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก |
| ์ ํ ์ฌํญ์ ๋๋ค. ์์ ์ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ ฌ์ ์ง์ ํฉ๋๋ค. |
| ์ ํ ์ฌํญ์
๋๋ค. |
์์ธํ ๋ด์ฉ์ db.collection.replaceOne()
์ ์ฐธ๊ณ ํ์ธ์.
deleteOne๊ณผ deleteMany
deleteOne
์ ํํฐ์ ์ผ์นํ๋ ์ปฌ๋ ์
์ ๋จ์ผ ๋ฌธ์๋ฅผ ์ญ์ ํฉ๋๋ค. ์ฌ๋ฌ ๋ฌธ์๊ฐ ์ผ์นํ๋ ๊ฒฝ์ฐ deleteOne
์ ์ฒซ ๋ฒ์งธ ์ผ์นํ๋ ๋ฌธ์๋ง ์ญ์ ํฉ๋๋ค.
db.collection.bulkWrite([ { deleteOne : { "filter" : <document>, "collation" : <document> // Available starting in 3.4 } } ] )
deleteMany
ํํฐ์ ์ผ์นํ๋ ์ปฌ๋ ์
์ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ญ์ ํฉ๋๋ค.
db.collection.bulkWrite([ { deleteMany: { "filter" : <document>, "collation" : <document> // Available starting in 3.4 } } ] )
ํ๋ | ์ฐธ๊ณ ์ฌํญ |
---|---|
| ์ญ์ ์์
์ ์ ํ ๊ธฐ์ค์
๋๋ค. |
| ์ ํ ์ฌํญ์ ๋๋ค. ์์ ์ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ ฌ์ ์ง์ ํฉ๋๋ค. |
์์ธํ ๋ด์ฉ์ db.collection.deleteOne()
๋ฐ db.collection.deleteMany()
๋ฅผ ์ฐธ์กฐํ์ธ์.
_id
ํ๋
๋ฌธ์์ _id ํ๋๊ฐ ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ mongod
์์ _id
ํ๋๋ฅผ ์ถ๊ฐํ๊ณ ์ฝ์
๋๋ ์
์ํธํ๊ธฐ ์ ์ ๋ฌธ์์ ๊ณ ์ ํ ObjectId()
๋ฅผ ํ ๋นํฉ๋๋ค. ๋๋ถ๋ถ์ ๋๋ผ์ด๋ฒ๋ ObjectId๋ฅผ ์์ฑํ๊ณ _id
ํ๋๋ฅผ ์ฝ์
ํ์ง๋ง, ๋๋ผ์ด๋ฒ๋ ์ ํ๋ฆฌ์ผ์ด์
์ด ์ํํ์ง ์๋ ๊ฒฝ์ฐ mongod
์์ _id
๋ฅผ ์์ฑํ์ฌ ์ฑ์๋๋ค.
๋ฌธ์์ _id
ํ๋๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ์ค๋ณต ํค ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ๋ ค๋ฉด _id
๊ฐ์ด ์ปฌ๋ ์
๋ด์์ ๊ณ ์ ํด์ผ ํฉ๋๋ค.
์
๋ฐ์ดํธ ๋๋ ๊ต์ฒด ์์
์์๋ ์๋ณธ ๋ฌธ์์ ๋ค๋ฅธ _id
๊ฐ์ ์ง์ ํ ์ ์์ต๋๋ค.
์ฐ์ฐ ์คํ
ordered
๋งค๊ฐ๋ณ์๋ bulkWrite()
์ด ์์
์ ์์๋๋ก ์คํํ ์ง ์ฌ๋ถ๋ฅผ ์ง์ ํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์์
์ ์์๋๋ก ์คํ๋ฉ๋๋ค.
๋ค์ ์ฝ๋๋ ์ฐ์ฐ์ด 5๊ฐ ํฌํจ๋ bulkWrite()
๋ฅผ ๋ํ๋
๋๋ค.
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ] )
๊ธฐ๋ณธ๊ฐ ordered : true
์ํ์์๋ ์ฒซ ๋ฒ์งธ ์ฐ์ฐ insertOne
๋ถํฐ ๋ง์ง๋ง ์ฐ์ฐ deleteMany
๊น์ง ๊ฐ ์ฐ์ฐ์ด ์์๋๋ก ์คํ๋ฉ๋๋ค.
ordered
๋ฅผ ๊ฑฐ์ง์ผ๋ก ์ค์ ํ๋ฉด ์ฑ๋ฅ์ ๋์ด๊ธฐ ์ํด mongod
๋ก ์์
์์๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ์ ํ๋ฆฌ์ผ์ด์
์ ์์
์คํ ์์์ ์์กดํด์๋ ์ ๋ฉ๋๋ค.
๋ค์ ์ฝ๋๋ ์ฐ์ฐ 6๊ฐ๊ฐ ํฌํจ๋ ์ ๋ ฌ๋์ง ์์ bulkWrite()
๋ฅผ ๋ํ๋
๋๋ค.
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false
์ ์ฌ์ฉํ๋ฉด ์ฐ์ฐ ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด deleteOne
๋๋ deleteMany
๋ insertOne
, updateOne
, updateMany
๋๋ replaceOne
์์
์ ์ด์ ๋๋ ์ดํ ์คํ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ ๋ง์ ๋ฌธ์ ๋๋ ๋ ์ ์ ๋ฌธ์๋ฅผ ์ ๊ฑฐํ ์ ์์ต๋๋ค.
๊ฐ ๊ทธ๋ฃน์ ์์
์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ maxWriteBatchSize ๊ฐ์ ์ด๊ณผํ ์ ์์ต๋๋ค. maxWriteBatchSize
์ ๊ธฐ๋ณธ๊ฐ์ 100,000
์
๋๋ค. ์ด ๊ฐ์ hello.maxWriteBatchSize
ํ๋์ ํ์๋ฉ๋๋ค.
์ด ์ ํ์ ์ค๋ฅ ๋ฉ์์ง์ ํฌ๊ธฐ๊ฐ ๋๋ฌด ์ปค์ง๋ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํฉ๋๋ค. ๊ทธ๋ฃน์ด ์ด ์ ํ์ ์ด๊ณผํ๋ฉด ํด๋ผ์ด์ธํธ ๋๋ผ์ด๋ฒ๋ ๊ทธ๋ฃน์ ์ ํ ๊ฐ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ ์์ ๊ทธ๋ฃน์ผ๋ก ๋๋๋๋ค. ์๋ฅผ ๋ค์ด maxWriteBatchSize
๊ฐ์ด 100,000
์ธ ๊ฒฝ์ฐ ๋๊ธฐ์ด์ด 200,000
๊ฐ์ ์์
์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ผ๋ฉด ๋๋ผ์ด๋ฒ๋ ๊ฐ๊ฐ 100,000
๊ฐ์ ์์
์ด ์๋ 2๊ฐ์ ๊ทธ๋ฃน์ ๋ง๋ญ๋๋ค.
์ฐธ๊ณ
์ด ๋๋ผ์ด๋ฒ๋ ๊ณ ๊ธ API๋ฅผ ์ฌ์ฉํ ๋์๋ง ๊ทธ๋ฃน์ ์์ ๊ทธ๋ฃน ์ฌ๋ฌ ๊ฐ๋ก ๋๋๋๋ค. db.runCommand()
์(๋ฅผ) ์ง์ ์ฌ์ฉํ๋ฉด(์์: ๋๋ผ์ด๋ฒ๋ฅผ ์์ฑํ ๋) ํ๋๋ฅผ ์ด๊ณผํ๋ ์ฐ๊ธฐ ๋ฐฐ์น๋ฅผ ์คํํ๋ ค๊ณ ํ ๋ MongoDB์์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋จ์ผ ๋ฐฐ์น์ ๋ํ ์ค๋ฅ ๋ณด๊ณ ์๊ฐ ๋๋ฌด ์ปค์ง๋ฉด MongoDB๋ ๋๋จธ์ง ๋ชจ๋ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋น ๋ฌธ์์ด๋ก ์๋ฆ
๋๋ค. ์ด ํฌ๊ธฐ๊ฐ 1MB
๋ณด๋ค ํฐ ์ค๋ฅ ๋ฉ์์ง๊ฐ ๋ ๊ฐ ์ด์ ์์ผ๋ฉด ์๋ฆฝ๋๋ค.
ํฌ๊ธฐ ๋ฐ ๊ทธ๋ฃนํ ๋ฉ์ปค๋์ฆ์ ๋ด๋ถ ์ฑ๋ฅ ์ธ๋ถ ์ ๋ณด์ด๋ฉฐ ํฅํ ๋ฒ์ ์์ ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค.
์ค๋ ์ปฌ๋ ์
์์ ordered
์์
๋ชฉ๋ก์ ์คํํ๋ ๊ฒ์ unordered
๋ชฉ๋ก์ ์คํํ๋ ๊ฒ๋ณด๋ค ์ผ๋ฐ์ ์ผ๋ก ๋๋ฆฝ๋๋ค. ์ ๋ ฌ๋ ๋ชฉ๋ก์์๋ ๊ฐ ์์
์ด ์ด์ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ ค์ผ ํ๊ธฐ ๋๋ฌธ์
๋๋ค.
๊ณ ์ ์ฌ์ด์ฆ ์ปฌ๋ ์
bulkWrite()
์ฐ๊ธฐ ์์
์ ๊ณ ์ ์ฌ์ด์ฆ ์ปฌ๋ ์
์์ ์ฌ์ฉํ ๋ ์ ํ์ด ์์ต๋๋ค.
update
๊ธฐ์ค์ ๋ฐ๋ผ ์์ ๋๋ ๋ฌธ์์ ํฌ๊ธฐ๊ฐ ์ปค์ง๋ฉด updateOne
๋ฐ updateMany
์ WriteError
๋ฅผ ๋ฐ์์ํต๋๋ค
replaceOne
์ replacement
๋ฌธ์์ ํฌ๊ธฐ๊ฐ ์๋ณธ ๋ฌธ์๋ณด๋ค ํฐ ๊ฒฝ์ฐ WriteError
์ ๋ฐ์์ํต๋๋ค.
deleteOne
๋ฐ deleteMany
์ ๊ณ ์ ์ฌ์ด์ฆ ์ปฌ๋ ์
์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ WriteError
๋ฅผ ๋ฐ์์ํต๋๋ค.
Time Series ์ปฌ๋ ์
bulkWrite()
์ฐ๊ธฐ ์์
์ time series ์ปฌ๋ ์
์์ ์ฌ์ฉํ ๋ ์ ํ์ด ์์ต๋๋ค. time series ์ปฌ๋ ์
์๋ insertOne
๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค๋ฅธ ๋ชจ๋ ์์
์ WriteError
๋ฅผ ๋ฐํํฉ๋๋ค.
Error Handling
db.collection.bulkWrite()
๋ ์ค๋ฅ ๋ฐ์ ์ BulkWriteError
์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค. ํธ๋์ญ์
๋ด๋ถ์ ์ค๋ฅ ์ฒ๋ฆฌ๋ฅผ ์ฐธ์กฐํ์ธ์.
์ฐ๊ธฐ ๊ณ ๋ ค ์ค๋ฅ๋ฅผ ์ ์ธํ๊ณ ์ ๋ ฌ๋ ์์ ์ ์ค๋ฅ ๋ฐ์ ํ ์ค์ง๋๋ ๋ฐ๋ฉด ์ ๋ ฌ๋์ง ์์ ์์ ์ ํธ๋์ญ์ ๋ด์์ ์คํ๋๋ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ ๋๊ธฐ์ด์ ๋จ์ ์๋ ์ฐ๊ธฐ ์์ ์ ๊ณ์ ์ฒ๋ฆฌํฉ๋๋ค. ํธ๋์ญ์ ๋ด๋ถ์ ์ค๋ฅ ์ฒ๋ฆฌ๋ฅผ ์ฐธ์กฐํ์ธ์.
์ฐ๊ธฐ ๊ณ ๋ ค ์ค๋ฅ๋ writeConcernErrors
ํ๋์ ํ์๋๊ณ ๊ธฐํ ๋ชจ๋ ์ค๋ฅ๋ writeErrors
ํ๋์ ํ์๋ฉ๋๋ค. ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์
๋ ฅ๋ _id
๊ฐ ๋์ ์ฑ๊ณตํ ์ฐ๊ธฐ ์ฐ์ฐ์ ์๊ฐ ํ์๋ฉ๋๋ค. ์์๊ฐ ์ง์ ๋ ์ฐ์ฐ์์๋ ๋ฐ์ํ ๋จ์ผ ์ค๋ฅ๊ฐ ํ์๋ฉ๋๋ค. ํํธ ์์๊ฐ ์ง์ ๋์ง ์์ ์ฐ์ฐ์์๋ ๊ฐ ์ค๋ฅ๊ฐ ํ ๋ฐฐ์ด์ ํ์๋ฉ๋๋ค.
์คํค๋ง ์ ํจ์ฑ ๊ฒ์ฌ ์ค๋ฅ
์ปฌ๋ ์
์์ ์คํค๋ง ์ ํจ์ฑ ๊ฒ์ฌ ๋ฅผ ์ฌ์ฉํ๊ณ validationAction
์ด(๊ฐ) error
(์ผ)๋ก ์ค์ ํ๋ค ๊ฒฝ์ฐ ์๋ชป๋ ๋ฌธ์ ๋ฅผ ์ฝ์
ํ๊ฑฐ๋ ์๋ชป๋ ๊ฐ์ผ๋ก ๋ฌธ์ ๋ฅผ ์
๋ฐ์ดํธํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. operations
๋ฐฐ์ด ์์ ์ ํจํ์ง ์์ ์ฐ์ฐ ์์ ์๋ ์ฐ์ฐ์ด ์คํ๋์ด ์ปฌ๋ ์
์ ๊ธฐ๋ก๋ฉ๋๋ค. ordered
ํ๋ ๋ ๋๋จธ์ง ์์
์ด ์คํ๋๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํฉ๋๋ค.
ํธ๋์ญ์
db.collection.bulkWrite()
๋ ๋ถ์ฐ ํธ๋์ญ์
๋ด์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ค์
๋๋ถ๋ถ์ ๊ฒฝ์ฐ ๋ถ์ฐ ํธ๋์ญ์ ์ ๋จ์ผ ๋ฌธ์ ์ฐ๊ธฐ์ ๋นํด ๋ ํฐ ์ฑ๋ฅ ๋น์ฉ์ด ๋ฐ์ํ๋ฏ๋ก ๋ถ์ฐ ํธ๋์ญ์ ์ ๊ฐ์ฉ์ฑ์ด ํจ๊ณผ์ ์ธ ์คํค๋ง ์ค๊ณ๋ฅผ ๋์ฒดํ ์๋ ์์ต๋๋ค. ๋๋ถ๋ถ์ ์๋๋ฆฌ์ค์์ ๋น์ ๊ทํ๋ ๋ฐ์ดํฐ ๋ชจ๋ธ (๋ด์ฅ๋ ๋ฌธ์ ๋ฐ ๋ฐฐ์ด) ์ ๊ณ์ํด์ ๋ฐ์ดํฐ ๋ฐ ์ฌ์ฉ ์ฌ๋ก์ ์ต์ ์ผ ๊ฒ์ ๋๋ค. ์ฆ, ๋๋ถ๋ถ์ ์๋๋ฆฌ์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ ํ๊ฒ ๋ชจ๋ธ๋งํ๋ฉด ๋ถ์ฐ ํธ๋์ญ์ ์ ํ์์ฑ์ด ์ต์ํ๋ฉ๋๋ค.
์ถ๊ฐ ํธ๋์ญ์ ์ฌ์ฉ ๊ณ ๋ ค ์ฌํญ(์: ๋ฐํ์ ์ ํ ๋ฐ oplog ํฌ๊ธฐ ์ ํ)์ ํ๋ก๋์ ๊ณ ๋ ค์ฌํญ์ ์ฐธ์กฐํ์ธ์.
ํธ๋์ญ์ ๋ด ์ฝ์ ๋ฐ ์ ์ํธ
๊ธฐ๋ฅ ํธํ์ฑ ๋ฒ์ (fcv) "4.4"
์ด์์ ๊ฒฝ์ฐ, ์กด์ฌํ์ง ์๋ ์ปฌ๋ ์
์ ๋ํด ํธ๋์ญ์
์์ upsert: true
๋ก ์ฝ์
์ฐ์ฐ ๋๋ ์
๋ฐ์ดํธ ์ฐ์ฐ์ ์คํํ๋ฉด ์ปฌ๋ ์
์ด ์์์ ์ผ๋ก ์์ฑ๋ฉ๋๋ค.
์ฐธ๊ณ
์ค๋ ๊ฐ ์ฐ๊ธฐ ํธ๋์ญ์ ์์๋ ์ ์ปฌ๋ ์ ์ ์์ฑํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ํ๋์ ์ค๋์์ ๊ธฐ์กด ์ปฌ๋ ์ ์ ์ฐ๊ณ ๋ค๋ฅธ ์ค๋์์ ์์์ ์ผ๋ก ์ ์ปฌ๋ ์ ์ ์์ฑํ๋ ๊ฒฝ์ฐ, MongoDB๋ ๋์ผํ ํธ๋์ญ์ ์์ ๋ ์์ ์ ๋ชจ๋ ์ํํ ์ ์์ต๋๋ค.
์ฐ๊ธฐ ๊ณ ๋ ค ๋ฐ ํธ๋์ญ์
ํธ๋์ญ์ ์์ ์คํ๋๋ ๊ฒฝ์ฐ ์์ ์ ๋ํ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ๋ช ์์ ์ผ๋ก ์ค์ ํ์ง ๋ง์ธ์. ํธ๋์ญ์ ์ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ํธ๋์ญ์ ๋ฐ ์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฐธ์กฐํ์ธ์.
ํธ๋์ญ์ ๋ด๋ถ์ ์ค๋ฅ ์ฒ๋ฆฌํ๊ธฐ
MongoDB 4.2๋ถํฐ db.collection.bulkWrite()
์ฐ์ฐ์ด ํธ๋์ญ์
๋ด๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์ด ๋ฉ์๋๋(ํธ๋์ญ์
์ธ๋ถ์์์ ๋์ผํ๊ฒ) BulkWriteException์ ๋ฐ์์ํต๋๋ค.
4.0์์๋ bulkWrite
์ฐ์ฐ์ ํธ๋์ญ์
๋ด๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ, ๋ฐ์ํ ์ค๋ฅ๊ฐ BulkWriteException
(์ผ)๋ก ๋ํ๋์ง ์์ต๋๋ค.
๋๋ ์ฐ๊ธฐ์ ์์๊ฐ ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ์๋ ํธ๋์ญ์ ๋ด๋ถ์์๋ ๋๋ ์ฐ๊ธฐ์ ์ฒซ ์ค๋ฅ๋ก ์ธํด ๋๋ ์ฐ๊ธฐ ์ ์ฒด๊ฐ ์คํจํ๊ณ ํธ๋์ญ์ ์ด ์ค๋จ๋ฉ๋๋ค.
์์
์์๊ฐ ์ง์ ๋ ๋๋ ์ฐ๊ธฐ ์์
bulkWrite()
์์
์์ ๋ฐ ์ค๋ฅ ์ฒ๋ฆฌ๋ฅผ ์ดํดํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก bulkWrite()
(์)๋ ์์๊ฐ ์ง์ ๋ ์์
๋ชฉ๋ก์ ์คํํฉ๋๋ค.
์์ ์ ์ฐ์์ ์ผ๋ก ์คํ๋ฉ๋๋ค.
์์ ์ ์ค๋ฅ๊ฐ ์๋ ๊ฒฝ์ฐ ํด๋น ์์ ๊ณผ ์ดํ ์์ ์ ์คํ๋์ง ์์ต๋๋ค.
์ค๋ฅ ์์ ์ด์ ์ ๋์ด๋ ์์ ์ด ์๋ฃ๋์์ต๋๋ค.
๋ค์ bulkWrite()
์์์์๋ pizzas
์ปฌ๋ ์
์ ์ฌ์ฉํฉ๋๋ค.
db.pizzas.insertMany( [ { _id: 0, type: "pepperoni", size: "small", price: 4 }, { _id: 1, type: "cheese", size: "medium", price: 7 }, { _id: 2, type: "vegan", size: "large", price: 8 } ] )
๋ค์ bulkWrite()
์์์์๋ ์ด๋ฌํ ์์
์ pizzas
์ปฌ๋ ์
์์ ์คํํฉ๋๋ค.
0}์ ์ฌ์ฉํ์ฌ ๋ ๊ฐ์ ๋ฌธ์๋ฅผ ์ถ๊ฐํฉ๋๋ค.
insertOne
0}์ ์ฌ์ฉํ์ฌ ๋ฌธ์๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค.
updateOne
0}์(๋ฅผ) ์ฌ์ฉํ์ฌ ๋ฌธ์๋ฅผ ์ญ์ ํฉ๋๋ค.
deleteOne
0}์ ์ฌ์ฉํ์ฌ ๋ฌธ์๋ฅผ ๋ฐ๊ฟ๋๋ค.
replaceOne
try { db.pizzas.bulkWrite( [ { insertOne: { document: { _id: 3, type: "beef", size: "medium", price: 6 } } }, { insertOne: { document: { _id: 4, type: "sausage", size: "large", price: 10 } } }, { updateOne: { filter: { type: "cheese" }, update: { $set: { price: 8 } } } }, { deleteOne: { filter: { type: "pepperoni"} } }, { replaceOne: { filter: { type: "vegan" }, replacement: { type: "tofu", size: "small", price: 4 } } } ] ) } catch( error ) { print( error ) }
์๋ฃ๋ ์์ ์ ์์ฝ์ ํฌํจํ๋ ์์ ์ถ๋ ฅ:
{ acknowledged: true, insertedCount: 2, insertedIds: { '0': 3, '1': 4 }, matchedCount: 2, modifiedCount: 2, deletedCount: 1, upsertedCount: 0, upsertedIds: {} }
์ด์ bulkWrite()
์์๋ฅผ ์คํํ๊ธฐ ์ ์ ์ปฌ๋ ์
์
_id
๊ฐ 4
์ธ ๋ฌธ์๊ฐ ์ด๋ฏธ ํฌํจ๋์ด ์๋ ๊ฒฝ์ฐ ๋ ๋ฒ์งธ
insertOne
์์
์ ๋ํด ๋ค์๊ณผ ๊ฐ์ ์ค๋ณต ํค ์์ธ๊ฐ ๋ฐํ๋ฉ๋๋ค.
writeErrors: [ WriteError { err: { index: 1, code: 11000, errmsg: 'E11000 duplicate key error collection: test.pizzas index: _id_ dup key: { _id: 4 }', op: { _id: 4, type: 'sausage', size: 'large', price: 10 } } } ], result: BulkWriteResult { result: { ok: 1, writeErrors: [ WriteError { err: { index: 1, code: 11000, errmsg: 'E11000 duplicate key error collection: test.pizzas index: _id_ dup key: { _id: 4 }', op: { _id: 4, type: 'sausage', size: 'large', price: 10 } } } ], writeConcernErrors: [], insertedIds: [ { index: 0, _id: 3 }, { index: 1, _id: 4 } ], nInserted: 1, nUpserted: 0, nMatched: 0, nModified: 0, nRemoved: 0, upserted: [] } }
bulkWrite()
์์๋ ์์๊ฐ ์ง์ ๋์ด ์์ผ๋ฏ๋ก ์ฒซ ๋ฒ์งธ insertOne
์์
๋ง ์๋ฃ๋ฉ๋๋ค.
์ค๋ฅ๊ฐ ์๋ ๋ชจ๋ ์์
์ ์๋ฃํ๋ ค๋ฉด ordered
๋ฅผ
false
๋ก ์ค์ ํ ์ํ์์ bulkWrite()
๋ฅผ ์คํํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ์น์
์ ์ฐธ์กฐํ์ธ์.
์์๊ฐ ์ง์ ๋์ง ์์ ๋๋ ์ฐ๊ธฐ ์์
์์๊ฐ ์ง์ ๋์ง ์์ bulkWrite()
๋ฅผ ์ง์ ํ๋ ค๋ฉด ordered
๋ฅผ false
๋ก ์ค์ ํฉ๋๋ค.
์์๊ฐ ์ง์ ๋์ง ์์ bulkWrite()
์ฐ์ฐ ๋ชฉ๋ก์์:
์์ ์ ๋ณ๋ ฌ๋ก ์คํ๋ ์ ์์ต๋๋ค(๋ณด์ฅ๋์ง ์์). ์์ธํ ๋ด์ฉ์ ์์๊ฐ ์ง์ ๋ ์์ ๊ณผ ์์๊ฐ ์ง์ ๋์ง ์์ ์์ ์ ์ฐธ์กฐํ์ธ์.
์ค๋ฅ๊ฐ ์๋ ์์ ์ ์๋ฃ๋์ง ์์ต๋๋ค.
์ค๋ฅ ์์ด ๋ชจ๋ ์์ ์ด ์๋ฃ๋ฉ๋๋ค.
pizzas
collection ์์๋ฅผ ๊ณ์ ์งํํ์ฌ collection์ ์ ๊ฑฐํ๊ณ ๋ค์ ์์ฑํฉ๋๋ค.
db.pizzas.insertMany( [ { _id: 0, type: "pepperoni", size: "small", price: 4 }, { _id: 1, type: "cheese", size: "medium", price: 7 }, { _id: 2, type: "vegan", size: "large", price: 8 } ] )
๋ค์ ์์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
bulkWrite()
๋pizzas
์ปฌ๋ ์ ์์ ์์๊ฐ ์ง์ ๋์ง ์์ ์์ ์ ์คํํฉ๋๋ค.๋ ๋ฒ์งธ
insertOne
์์ ์๋ ์ฒซ ๋ฒ์งธinsertOne
์ ๋์ผํ_id
์์ผ๋ฏ๋ก ์ค๋ณต ํค ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
try { db.pizzas.bulkWrite( [ { insertOne: { document: { _id: 3, type: "beef", size: "medium", price: 6 } } }, { insertOne: { document: { _id: 3, type: "sausage", size: "large", price: 10 } } }, { updateOne: { filter: { type: "cheese" }, update: { $set: { price: 8 } } } }, { deleteOne: { filter: { type: "pepperoni"} } }, { replaceOne: { filter: { type: "vegan" }, replacement: { type: "tofu", size: "small", price: 4 } } } ], { ordered: false } ) } catch( error ) { print( error ) }
์ค๋ณต ํค ์ค๋ฅ์ ์๋ฃ๋ ์์ ์ ์์ฝ์ ํฌํจํ๋ ์์ ์ถ๋ ฅ:
writeErrors: [ WriteError { err: { index: 1, code: 11000, errmsg: 'E11000 duplicate key error collection: test.pizzas index: _id_ dup key: { _id: 3 }', op: { _id: 3, type: 'sausage', size: 'large', price: 10 } } } ], result: BulkWriteResult { result: { ok: 1, writeErrors: [ WriteError { err: { index: 1, code: 11000, errmsg: 'E11000 duplicate key error collection: test.pizzas index: _id_ dup key: { _id: 3 }', op: { _id: 3, type: 'sausage', size: 'large', price: 10 } } } ], writeConcernErrors: [], insertedIds: [ { index: 0, _id: 3 }, { index: 1, _id: 3 } ], nInserted: 1, nUpserted: 0, nMatched: 2, nModified: 2, nRemoved: 1, upserted: [] } }
์ค๋ณต ํค ์ค๋ฅ๋ก ์ธํด ๋ ๋ฒ์งธ insertOne
์ฐ์ฐ์ด ์คํจํ์ต๋๋ค. ์์๊ฐ ์ง์ ๋์ง ์์ bulkWrite()
์์ ์ค๋ฅ๊ฐ ์๋ ๋ชจ๋ ์์
์ด ์๋ฃ๋ฉ๋๋ค.
์ฐ๊ธฐ ๊ณ ๋ ค๊ฐ ์๋ ๋๋ ์ฐ๊ธฐ ์์
pizzas
collection ์์๋ฅผ ๊ณ์ ์งํํ์ฌ collection์ ์ ๊ฑฐํ๊ณ ๋ค์ ์์ฑํฉ๋๋ค.
db.pizzas.insertMany( [ { _id: 0, type: "pepperoni", size: "small", price: 4 }, { _id: 1, type: "cheese", size: "medium", price: 7 }, { _id: 2, type: "vegan", size: "large", price: 8 } ] )
๋ค์ bulkWrite()
์์์์๋ pizzas
์ปฌ๋ ์
์์ ์์
์ ์คํํ๊ณ 100๋ฐ๋ฆฌ์ด์ ์๊ฐ ์ ํ์ด ์๋
"majority"
์ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ค์ ํฉ๋๋ค.
try { db.pizzas.bulkWrite( [ { updateMany: { filter: { size: "medium" }, update: { $inc: { price: 0.1 } } } }, { updateMany: { filter: { size: "small" }, update: { $inc: { price: -0.25 } } } }, { deleteMany: { filter: { size: "large" } } }, { insertOne: { document: { _id: 4, type: "sausage", size: "small", price: 12 } } } ], { writeConcern: { w: "majority", wtimeout: 100 } } ) } catch( error ) { print( error ) }
๋๋ถ๋ถ์ ๋ณต์ ๋ณธ ์ธํธ ๋
ธ๋๊ฐ ์์
์ ํ์ธํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด wtimeout
์ ์ด๊ณผํ๋ ๊ฒฝ์ฐ ์ด ์์์์๋ ์ฐ๊ธฐ ๊ณ ๋ ค ์ค๋ฅ์ ์๋ฃ๋ ์์
์ ์์ฝ์ด ๋ฐํ๋ฉ๋๋ค.
result: BulkWriteResult { result: { ok: 1, writeErrors: [], writeConcernErrors: [ WriteConcernError { err: { code: 64, codeName: 'WriteConcernTimeout', errmsg: 'waiting for replication timed out', errInfo: { wtimeout: true, writeConcern: [Object] } } } ], insertedIds: [ { index: 3, _id: 4 } ], nInserted: 0, nUpserted: 0, nMatched: 2, nModified: 2, nRemoved: 0, upserted: [], opTime: { ts: Timestamp({ t: 1660329086, i: 2 }), t: Long("1") } } }