Docs Menu
Docs Home
/
MongoDB ๋งค๋‰ด์–ผ
/ / /

db.collection.findOneAndReplace()

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ์ •์˜
  • ๊ตฌ๋ฌธ
  • ํ–‰๋™
  • ์˜ˆ์‹œ

๋“œ๋ผ์ด๋ฒ„๊ฐ€ ํฌํ•จ๋œ MongoDB

์ด ํŽ˜์ด์ง€์—์„œ๋Š” mongosh ๋ฉ”์„œ๋“œ๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. MongoDB ๋“œ๋ผ์ด๋ฒ„์—์„œ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๋ ค๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ํ•ด๋‹น ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

C#Java SyncNode.jsPyMongoCC++GoJava RSKotlin CoroutineKotlin SyncPHPMongoidRustScala
db.collection.findOneAndReplace( filter, replacement, options )

์ง€์ •๋œ ํ•„ํ„ฐ๋ฅผ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์ผ ๋ฌธ์„œ๋ฅผ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

findOneAndReplace() ๋ฉ”์„œ๋“œ์˜ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

db.collection.findOneAndReplace(
<filter>,
<replacement>,
{
writeConcern: <document>,
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnDocument: <string>,
returnNewDocument: <boolean>,
collation: <document>
}
)

findOneAndReplace() ๋ฉ”์„œ๋“œ์—๋Š” ๋‹ค์Œ ํ•„๋“œ์™€ ์˜ต์…˜์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

ํ•„๋“œ
์œ ํ˜•
์„ค๋ช…
๋ฌธ์„œ

์—…๋ฐ์ดํŠธ์˜ ์„ ํƒ ๊ธฐ์ค€์ž…๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ์—์„œ์™€ ๋™์ผํ•œ ์ฟผ๋ฆฌ ์„ ํƒ๊ธฐ find()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

collection์—์„œ ๋ฐ˜ํ™˜๋œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ๋ฅผ ๋ฐ”๊พธ๋ ค๋ฉด ๋นˆ ๋ฌธ์„œ { }๋ฅผ ์ง€์ •ํ•˜์„ธ์š”.

์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋นˆ ๋ฌธ์„œ๋กœ ๊ธฐ๋ณธ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

์ฟผ๋ฆฌ ์ธ์ˆ˜๊ฐ€ ๋ฌธ์„œ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์„œ

๋Œ€์ฒด ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.

์—…๋ฐ์ดํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

<replacement> ๋ฌธ์„œ์—์„œ๋Š” ๋Œ€์ฒด๋œ ๋ฌธ์„œ์™€ ๋‹ค๋ฅธ _id ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

writeConcern
๋ฌธ์„œ

์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด ์ƒ๋žตํ•˜์„ธ์š”.

{ w: <value>, j: <boolean>, wtimeout: <number> }

ํŠธ๋žœ์žญ์…˜์—์„œ ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ ์ž‘์—…์— ๋Œ€ํ•œ ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•˜์ง€ ๋งˆ์„ธ์š”. ํŠธ๋žœ์žญ์…˜์— ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํŠธ๋žœ์žญ์…˜ ๋ฐ ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋ฌธ์„œ

์„ ํƒ ์‚ฌํ•ญ. ๋ฐ˜ํ™˜ํ•  ํ•„๋“œ์˜ ํ•˜์œ„ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.

์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ์˜ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด ์ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ƒ๋žตํ•˜์„ธ์š”.

ํ”„๋กœ์ ์…˜ ์ธ์ˆ˜๊ฐ€ ๋ฌธ์„œ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ž‘์—… ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์„œ

์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ํ•„ํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ์˜ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

์ •๋ ฌ ์ธ์ˆ˜๊ฐ€ ๋ฌธ์„œ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ž‘์—… ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

cursor.sort()์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

maxTimeMS
์ˆซ์ž
์„ ํƒ ์‚ฌํ•ญ. ์ž‘์—…์„ ์™„๋ฃŒํ•ด์•ผ ํ•˜๋Š” ์‹œ๊ฐ„ ์ œํ•œ์„ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ œํ•œ์„ ์ดˆ๊ณผํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๋ถ€์šธ

์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. true์ธ ๊ฒฝ์šฐ findOneAndReplace()๋Š” ๋‹ค์Œ ์ž‘์—… ์ค‘ ํ•œ ๊ฐ€์ง€๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • filter์™€(๊ณผ) ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์—†์œผ๋ฉด replacement ๋งค๊ฐœ ๋ณ€์ˆ˜์—์„œ ๋ฌธ์„œ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. returnNewDocument์ด(๊ฐ€) true์ด(๊ฐ€) ์•„๋‹Œ ๊ฒฝ์šฐ ์ƒˆ ๋ฌธ์„œ๋ฅผ ์‚ฝ์ž…ํ•œ ํ›„ null์„(๋ฅผ) ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • filter์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ๋ฅผ replacement ๋ฌธ์„œ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

filter ๋˜๋Š” replacement ๋ฌธ์„œ์— _id ํ•„๋“œ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ MongoDB๋Š” ๋Œ€์ฒด ๋ฌธ์„œ์— ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. _id ํ•„๋“œ๊ฐ€ ๋‘ ๋ฌธ์„œ ๋ชจ๋‘์— ์žˆ์œผ๋ฉด ๊ทธ ๊ฐ’์ด ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์—…์„œํŠธ๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋ ค๋ฉด query ํ•„๋“œ๋ฅผ ๊ณ ์œ ํ•˜๊ฒŒ ์ธ๋ฑ์‹ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ๊ฐ’์€ false์ž…๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด

์„ ํƒ ์‚ฌํ•ญ. mongosh 0.13.2๋ถ€ํ„ฐ returnDocument๋Š” returnNewDocument์˜ ๋Œ€์•ˆ์ž…๋‹ˆ๋‹ค. ๋‘ ์˜ต์…˜์ด ๋ชจ๋‘ ์„ธํŠธ์ธ ๊ฒฝ์šฐ returnDocument๊ฐ€ ์šฐ์„ ๋ฉ๋‹ˆ๋‹ค.

returnDocument: "before" ์€ ์›๋ณธ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. returnDocument: "after"์€ ์—…๋ฐ์ดํŠธ๋œ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ถ€์šธ

์„ ํƒ ์‚ฌํ•ญ. true์ธ ๊ฒฝ์šฐ ์›๋ณธ ๋ฌธ์„œ ๋Œ€์‹  ๋Œ€์ฒด ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ๊ฐ’์€ false์ž…๋‹ˆ๋‹ค.

๋ฌธ์„œ

์„ ํƒ ์‚ฌํ•ญ.

์ž‘์—…์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋Œ€์†Œ๋ฌธ์ž ๋ฐ ์•…์„ผํŠธ ํ‘œ์‹œ ๊ทœ์น™๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด ๋น„๊ต์— ๋Œ€ํ•œ ์–ธ์–ด๋ณ„ ๊ทœ์น™์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ •๋ ฌ ์˜ต์…˜์˜ ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•  ๋•Œ locale ํ•„๋“œ๋Š” ํ•„์ˆ˜์ด๊ณ , ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ํ•„๋“œ๋Š” ๋ชจ๋‘ ์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ํ•„๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์ง€์ •๋˜์ง€ ์•Š์•˜์ง€๋งŒ ์ปฌ๋ ‰์…˜์— ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์žˆ๋Š” ๊ฒฝ์šฐ( db.createCollection() ์ฐธ์กฐ), ์ž‘์—…์€ ์ปฌ๋ ‰์…˜์— ์ง€์ •๋œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ปฌ๋ ‰์…˜ ๋˜๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, MongoDB๋Š” ์ด์ „ ๋ฒ„์ „์—์„œ ๋ฌธ์ž์—ด ๋น„๊ต์— ์‚ฌ์šฉ๋œ ๊ฐ„๋‹จํ•œ ์ด์ง„ ๋น„๊ต๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํ•œ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•„๋“œ๋ณ„๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์ •๋ ฌ๊ณผ ํ•จ๊ป˜ ์ฐพ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ์ฐพ๊ธฐ ์™€ ์ •๋ ฌ์—์„œ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์›๋ณธ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. returnDocument๊ฐ€ after๋กœ ์„ค์ •๋˜์–ด ์žˆ๊ฑฐ๋‚˜ returnNewDocument๊ฐ€ true๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ ์—…๋ฐ์ดํŠธ๋œ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

db.collection.findOneAndReplace()(์€)๋Š” filter(๊ณผ)์™€ ์ผ์น˜ํ•˜๋Š” ์ปฌ๋ ‰์…˜์˜ ์ฒซ ๋ฒˆ์งธ ์ผ์น˜ ๋ฌธ์„œ๋ฅผ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. sort ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ •๋˜๋Š” ๋ฌธ์„œ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ค‘์š”

์–ธ์–ด ์ผ๊ด€์„ฑ

find() ๋ฐ findAndModify() ํ”„๋กœ์ ์…˜์„ ์ง‘๊ณ„์˜ $project ๋‹จ๊ณ„์™€ ์ผ์น˜์‹œํ‚ค๋Š” ์ž‘์—…์˜ ์ผํ™˜์œผ๋กœ,

projection ํ•„๋“œ์—๋Š” ๋‹ค์Œ ํ˜•์‹์˜ ๋ฌธ์„œ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค:

{ field1: <value>, field2: <value> ... }
ํ”„๋กœ์ ์…˜
์„ค๋ช…
<field>: <1 or true>
ํ•„๋“œ ํฌํ•จ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์ ์…˜ ๊ฐ’์— 0์ด ์•„๋‹Œ ์ •์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋ฉด ์—ฐ์‚ฐ์€ ํ•ด๋‹น ๊ฐ’์„ true๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.
<field>: <0 or false>
ํ•„๋“œ ์ œ์™ธ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
"<field>.$": <1 or true>

$ ๋ฐฐ์—ด ํ”„๋กœ์ ์…˜ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด ํ•„๋“œ์˜ ์ฟผ๋ฆฌ ์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์ ์…˜ ๊ฐ’์— ๋Œ€ํ•ด 0์ด ์•„๋‹Œ ์ •์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋ฉด ์—ฐ์‚ฐ์€ ๊ฐ’์„ true๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ทฐ์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

<field>: <array projection>

๋ฐฐ์—ด ํ”„๋กœ์ ์…˜ ์—ฐ์‚ฐ์ž($elemMatch, $slice)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํฌํ•จํ•  ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ทฐ์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

<field>: <aggregation expression>

ํ”„๋กœ์ ์…˜๋œ ํ•„๋“œ์˜ ๊ฐ’์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌํ„ฐ๋Ÿด ๋ฐ ์ง‘๊ณ„ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ํฌํ•จํ•œ ์ง‘๊ณ„ ์‹ ๋ฐ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ƒˆ ํ•„๋“œ๋ฅผ ํ”„๋กœ์ ์…˜ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ํ•„๋“œ๋ฅผ ์ƒˆ ๊ฐ’์œผ๋กœ ํ”„๋กœ์ ์…˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ํ”„๋กœ์ ์…˜ ๊ฐ’์— ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด์„œ ๋ถ€์šธ์ด ์•„๋‹Œ ๋ฆฌํ„ฐ๋Ÿด(์˜ˆ์‹œ: ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž์—ด, ๋ฐฐ์—ด ๋˜๋Š” ์—ฐ์‚ฐ์ž ํ‘œํ˜„์‹)์„ ์ง€์ •ํ•˜๋ฉด ํ•„๋“œ๊ฐ€ ์ƒˆ ๊ฐ’์œผ๋กœ ํ”„๋กœ์ ์…˜๋ฉ๋‹ˆ๋‹ค.

    • { field: [ 1, 2, 3, "$someExistingField" ] }

    • { field: "New String Value" }

    • { field: { status: "Active", total: { $sum: "$existingArray" } } }

  • ํ•„๋“œ์˜ ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’์„ ํ”„๋กœ์ ์…˜ํ•˜๋ ค๋ฉด ๋‹ค์Œ ์˜ˆ์‹œ์™€ ๊ฐ™์€ $literal ์ง‘๊ณ„ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

    • { field: { $literal: 5 } }

    • { field: { $literal: true } }

    • { field: { $literal: { fieldWithValue0: 0, fieldWithValue1: 1 } } }

๋‚ด์žฅ๋œ ๋ฌธ์„œ์˜ ํ•„๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„๋“œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์  ํ‘œ๊ธฐ๋ฒ•(์˜ˆ์‹œ: "field.nestedfield": <value>

  • ์ค‘์ฒฉ๋œ ์–‘์‹, ์˜ˆ๋ฅผ ๋“ค์–ด { field: { nestedfield: <value> } }

ํ•„๋“œ๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ํ”„๋กœ์ ์…˜์—์„œ _id: 0์„ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ํ•œ _id ํ•„๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ˜ํ™˜๋œ ๋ฌธ์„œ์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

projection ํ•„๋“œ๋Š” ํฌํ•จ๊ณผ ์ œ์™ธ ์‚ฌ์–‘์„ ๋ชจ๋‘ ํฌํ•จํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹จ, _id ํ•„๋“œ๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค.

  • ํ•„๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํฌํ•จํ•˜๋Š” ํ”„๋กœ์ ์…˜์—์„œ _id ํ•„๋“œ๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์ œ์™ธํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ํ•„๋“œ์ž…๋‹ˆ๋‹ค.

  • ํ•„๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ œ์™ธํ•˜๋Š” ํ”„๋กœ์ ์…˜์—์„œ๋Š” _id ํ•„๋“œ๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ํ•„๋“œ์ด์ง€๋งŒ, _id ํ•„๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์ ์…˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ ์„น์…˜๋„ ๊ฐ™์ด ์ฐธ์กฐํ•˜์„ธ์š”.

์ƒค๋”ฉ๋œ ์ปฌ๋ ‰์…˜์—์„œ db.collection.findOneAndReplace()๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ ํ•„ํ„ฐ ์— ์ƒค๋“œ ํ‚ค์— ๋Œ€ํ•œ ๋™๋“ฑ ์กฐ๊ฑด์ด๋ฐ˜๋“œ์‹œ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ƒค๋”ฉ๋œ ์ปฌ๋ ‰์…˜์˜ ๋ฌธ์„œ์—๋Š” ์ƒค๋“œ ํ‚ค ํ•„๋“œ๊ฐ€ ๋ˆ„๋ฝ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒค๋“œ ํ‚ค๊ฐ€ ์—†๋Š” ๋ฌธ์„œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด null ๋™๋“ฑ์„ฑ ๋งค์น˜๋ฅผ ๋‹ค๋ฅธ ํ•„ํ„ฐ ์กฐ๊ฑด(์˜ˆ์‹œ: _id ํ•„๋“œ)๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{ _id: <value>, <shardkeyfield>: null } // _id of the document missing shard key

์ƒค๋“œ ํ‚ค ํ•„๋“œ๊ฐ€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” _id ํ•„๋“œ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋ฌธ์„œ์˜ ์ƒค๋“œ ํ‚ค ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฝ๊ณ 

์ƒค๋”ฉ๋œ ์ปฌ๋ ‰์…˜์˜ ๋ฌธ์„œ์—๋Š” ์ƒค๋“œ ํ‚ค ํ•„๋“œ๊ฐ€ ๋ˆ„๋ฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ์˜ ์ƒค๋“œ ํ‚ค ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ ์‹ค์ˆ˜๋กœ ์ƒค๋“œ ํ‚ค๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด ์ƒค๋“œ ํ‚ค ๊ฐ’์„ db.collection.findOneAndReplace()์œผ๋กœ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ˜๋“œ์‹œ mongos์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ƒค๋“œ์—์„œ ์ง์ ‘ ์ž‘์—…์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ˜๋“œ์‹œ ํŠธ๋žœ์žญ์…˜์—์„œ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ์žฌ์‹œ๋„ ๊ฐ€๋Šฅ ์“ฐ๊ธฐ๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค .

  • ์ „์ฒด ์ƒค๋“œ ํ‚ค์— ๋™๋“ฑ์„ฑ ํ•„ํ„ฐ๋ฅผ ํฌํ•จ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ƒค๋”ฉ๋œ ์ปฌ๋ ‰์…˜์˜ ๋ฌธ์„œ์—๋Š” ์ƒค๋“œ ํ‚ค ํ•„๋“œ๊ฐ€ ๋ˆ„๋ฝ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. db.collection.findOneAndReplace()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ ๋ˆ„๋ฝ๋œ ์ƒค๋“œ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ˜๋“œ์‹œ mongos์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ƒค๋“œ์—์„œ ์ง์ ‘ ์ž‘์—…์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ์ƒˆ ์ƒค๋“œ ํ‚ค ๊ฐ’์ด null์ด(๊ฐ€) ์•„๋‹Œ ๊ฒฝ์šฐ ํŠธ๋žœ์žญ์…˜ ๋˜๋Š” ์žฌ์‹œ๋„ ๊ฐ€๋Šฅ ์“ฐ๊ธฐ ์ค‘ ํ•˜๋‚˜๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ์ „์ฒด ์ƒค๋“œ ํ‚ค์— ๋™๋“ฑ์„ฑ ํ•„ํ„ฐ๋ฅผ ํฌํ•จ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํŒ

๋ˆ„๋ฝ๋œ ํ‚ค ๊ฐ’์€ null ๋™๋“ฑ์„ฑ ๋งค์น˜์˜ ์ผ๋ถ€๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ null ๊ฐ’ ํ‚ค๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋ ค๋ฉด ์ถ”๊ฐ€ ์ฟผ๋ฆฌ ์กฐ๊ฑด(์˜ˆ: _id ํ•„๋“œ)์„ ์ ์ ˆํžˆ ํฌํ•จํ•˜์„ธ์š”.

๋‹ค์Œ๋„ ์ฐธ์กฐํ•˜์„ธ์š”.

db.collection.findOneAndReplace()๋Š” ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜ ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ค‘์š”

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜์€ ๋‹จ์ผ ๋ฌธ์„œ ์“ฐ๊ธฐ์— ๋น„ํ•ด ๋” ํฐ ์„ฑ๋Šฅ ๋น„์šฉ์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜์˜ ๊ฐ€์šฉ์„ฑ์ด ํšจ๊ณผ์ ์ธ ์Šคํ‚ค๋งˆ ์„ค๊ณ„๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ๋น„์ •๊ทœํ™”๋œ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ (๋‚ด์žฅ๋œ ๋ฌธ์„œ ๋ฐ ๋ฐฐ์—ด) ์€ ๊ณ„์†ํ•ด์„œ ๋ฐ์ดํ„ฐ ๋ฐ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์ตœ์ ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋Œ€๋ถ€๋ถ„์˜ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ๋ชจ๋ธ๋งํ•˜๋ฉด ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜์˜ ํ•„์š”์„ฑ์ด ์ตœ์†Œํ™”๋ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ํŠธ๋žœ์žญ์…˜ ์‚ฌ์šฉ ๊ณ ๋ ค ์‚ฌํ•ญ(์˜ˆ: ๋Ÿฐํƒ€์ž„ ์ œํ•œ ๋ฐ oplog ํฌ๊ธฐ ์ œํ•œ)์€ ํ”„๋กœ๋•์…˜ ๊ณ ๋ ค์‚ฌํ•ญ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

ํŠธ๋žœ์žญ์…˜์ด ๊ต์ฐจ ์ƒค๋“œ ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜(write transaction)์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜ ๋‚ด์—์„œ ์ปฌ๋ ‰์…˜๊ณผ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

upsert: true์ธ db.collection.findOneAndReplace()๋Š” ๊ธฐ์กด ์ปฌ๋ ‰์…˜์ด๋‚˜ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ปฌ๋ ‰์…˜์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ปฌ๋ ‰์…˜์—์„œ ์‹คํ–‰ํ•˜๋ฉด ์ž‘์—…์ด ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

ํŠธ๋žœ์žญ์…˜์—์„œ ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ ์ž‘์—…์— ๋Œ€ํ•œ ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•˜์ง€ ๋งˆ์„ธ์š”. ํŠธ๋žœ์žญ์…˜์— ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํŠธ๋žœ์žญ์…˜ ๋ฐ ์“ฐ๊ธฐ ๊ณ ๋ ค๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

db.collection.findOneAndReplace() ์ž‘์—…์ด ๋ฌธ์„œ๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐ”๊พธ๋ฉด ์ด ์ž‘์—…์€ oplog(์ž‘์—… ๋กœ๊ทธ)์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—…์ด ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ๋ฐ”๊ฟ€ ๋ฌธ์„œ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด ์ž‘์—…์€ oplog์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ๋ฌธ์„œ๋กœ ์ƒ˜ํ”Œ scores ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

db.scores.insertMany([
{ "_id" : 1, "team" : "Fearful Mallards", "score" : 25000 },
{ "_id" : 2, "team" : "Tactful Mooses", "score" : 23500 },
{ "_id" : 3, "team" : "Aquatic Ponies", "score" : 19250 },
{ "_id" : 4, "team" : "Cuddly Zebras", "score" : 15235 },
{ "_id" : 5, "team" : "Garrulous Bears", "score" : 18000 }
]);

๋‹ค์Œ ์ž‘์—…์€ score์ด 20000๋ณด๋‹ค ์ž‘์€ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„์„œ ๋Œ€์ฒดํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.

db.scores.findOneAndReplace(
{ "score" : { $lt : 20000 } },
{ "team" : "Observant Badgers", "score" : 20000 }
)

์ด ์ž‘์—…์€ ๋Œ€์ฒด๋œ ์›๋ณธ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 3, "team" : "Aquatic Ponies", "score" : 19250 }

returnNewDocument๊ฐ€ true์ธ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์€ ๋Œ€์ฒด ๋ฌธ์„œ๋ฅผ ๋Œ€์‹  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ ๋ฌธ์„œ๊ฐ€ ํ•„ํ„ฐ ๊ธฐ์ค€์„ ์ถฉ์กฑํ•˜์ง€๋งŒ, db.collection.findOneAndReplace()๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์„œ๋งŒ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ๋ฌธ์„œ๋กœ ์ƒ˜ํ”Œ scores ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

db.scores.insertMany([
{ "_id" : 1, "team" : "Fearful Mallards", "score" : 25000 },
{ "_id" : 2, "team" : "Tactful Mooses", "score" : 23500 },
{ "_id" : 3, "team" : "Aquatic Ponies", "score" : 19250 },
{ "_id" : 4, "team" : "Cuddly Zebras", "score" : 15235 },
{ "_id" : 5, "team" : "Garrulous Bears", "score" : 18000 }
]);

๋‹ค์Œ ์˜ˆ์‹œ์—์„œ๋Š” score ํ•„๋“œ์— ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํฌํ•จํ•˜์—ฌ ํ•„ํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ ์ค‘ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ๋ฌธ์„œ๋ฅผ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

db.scores.findOneAndReplace(
{ "score" : { $lt : 20000 } },
{ "team" : "Observant Badgers", "score" : 20000 },
{ sort: { "score" : 1 } }
)

์ด ์ž‘์—…์€ ๋Œ€์ฒด๋œ ์›๋ณธ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 4, "team" : "Cuddly Zebras", "score" : 15235 }

์ด ๋ช…๋ น์˜ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๊ฒฐ๊ณผ๋Š” ๋ฌธ์„œ ๋ฐ”๊พธ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋‹ค์Œ ๋ฌธ์„œ๋กœ ์ƒ˜ํ”Œ scores ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

db.scores.insertMany([
{ "_id" : 1, "team" : "Fearful Mallards", "score" : 25000 },
{ "_id" : 2, "team" : "Tactful Mooses", "score" : 23500 },
{ "_id" : 3, "team" : "Aquatic Ponies", "score" : 19250 },
{ "_id" : 4, "team" : "Cuddly Zebras", "score" : 15235 },
{ "_id" : 5, "team" : "Garrulous Bears", "score" : 18000 }
])

๋‹ค์Œ ์ž‘์—…์—์„œ๋Š” ํ”„๋กœ์ ์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ๋ฌธ์„œ์— team ํ•„๋“œ๋งŒ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

db.scores.findOneAndReplace(
{ "score" : { $lt : 22250 } },
{ "team" : "Therapeutic Hamsters", "score" : 22250 },
{ sort : { "score" : 1 }, projection: { "_id" : 0, "team" : 1 } }
)

์ด ์ž‘์—…์€ team ํ•„๋“œ๋งŒ ์žˆ๋Š” ์›๋ณธ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "team" : "Cuddly Zebras" }

๋‹ค์Œ ์ž‘์—…์€ ์™„๋ฃŒ๊นŒ์ง€ 5ms์˜ ์‹œ๊ฐ„ ์ œํ•œ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

try {
db.scores.findOneAndReplace(
{ "score" : { $gt : 25000 } },
{ "team" : "Emphatic Rhinos", "score" : 25010 },
{ maxTimeMS: 5 }
);
} catch(e){
print(e);
}

์ž‘์—…์ด ์‹œ๊ฐ„ ์ œํ•œ์„ ์ดˆ๊ณผํ•˜๋ฉด ์ž‘์—…์€ ๋‹ค์Œ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Error: findAndModifyFailed failed: { "ok" : 0, "errmsg" : "operation exceeded time limit", "code" : 50 }

๋‹ค์Œ ์ž‘์—…์—์„œ๋Š” ํ•„ํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์—…์„œํŠธ ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ฒด ๋ฌธ์„œ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

try {
db.scores.findOneAndReplace(
{ "team" : "Fortified Lobsters" },
{ "_id" : 6019, "team" : "Fortified Lobsters" , "score" : 32000},
{ upsert : true, returnDocument: "after" }
);
} catch (e){
print(e);
}

์ด ์—ฐ์‚ฐ์€ ๋‹ค์Œ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

{
"_id" : 6019,
"team" : "Fortified Lobsters",
"score" : 32000
}

returnDocument: "before"๋ฅผ ์„ค์ •ํ•œ ๊ฒฝ์šฐ ๋ฐ˜ํ™˜ํ•  ์›๋ณธ ๋ฌธ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ž‘์—…์€ null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋Œ€์†Œ๋ฌธ์ž ๋ฐ ์•…์„ผํŠธ ํ‘œ์‹œ ๊ทœ์น™๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด ๋น„๊ต์— ๋Œ€ํ•œ ์–ธ์–ด๋ณ„ ๊ทœ์น™์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ๋ฌธ์„œ๋กœ ์ƒ˜ํ”Œ myColl ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

db.myColl.insertMany([
{ _id: 1, category: "cafรฉ", status: "A" },
{ _id: 2, category: "cafe", status: "a" },
{ _id: 3, category: "cafE", status: "a" }
]);

๋‹ค์Œ ์ž‘์—…์—๋Š” ๋ฐ์ดํ„ฐ ์ •๋ ฌ ์˜ต์…˜์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

db.myColl.findOneAndReplace(
{ category: "cafe", status: "a" },
{ category: "cafร‰", status: "Replaced" },
{ collation: { locale: "fr", strength: 1 } }
);

์—ฐ์‚ฐ์€ ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 1, "category" : "cafรฉ", "status" : "A" }

๋Œ์•„๊ฐ€๊ธฐ

db.collection.findOneAndDelete

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ