๋ฐ์ํ
Intro
์๋ ํ์ธ์. ์ด๋ฒ ์๊ฐ์๋ MongoDB์ $set ์ ๋ฐ์ดํธ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
$set
MongoDB์์๋ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ธฐ ์ํด update ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ํน์ ํ๋๋ง ์ ๋ฐ์ดํธ ํ๊ธฐ ์ํด์๋ $set ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋๋ฐ์, ํ๋ฒ ์์๋ฅผ ์ดํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Examples
์๋ฅผ ๋ค์ด ์๋์ ์ปฌ๋ ์ ์ด ์กด์ฌํ๋ค๊ณ ๊ฐ์ ํด๋ด ์๋ค.
db.products.insertOne(
{
_id: 100,
quantity: 250,
instock: true,
reorder: false,
details: { model: "14QQ", make: "Clothes Corp" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "Customer007", rating: 4 } ]
},
{
_id: 101,
quantity: 250,
instock: true,
reorder: false,
details: { model: "14QQ", make: "Clothes Corp" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "Customer007", rating: 4 } ]
}
)
์ฌ๊ธฐ์ _id๊ฐ 100์ธ ๊ฐ์ ํน์ ํ๋๊ฐ์ ๋ฐ๊ฟ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. $set ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ quantity, detail, tags field ๊ฐ์ ์ ๋ฐ์ดํธ ํฉ๋๋ค.
db.products.updateOne(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: "2600", make: "Fashionaires" },
tags: [ "coats", "outerwear", "clothing" ]
}
}
)
์ update๋ฅผ ์คํํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
{
_id: 100,
quantity: 500,
instock: true,
reorder: false,
details: { model: '2600', make: 'Fashionaires' },
tags: [ 'coats', 'outerwear', 'clothing' ],
ratings: [ { by: 'Customer007', rating: 4 } ]
},
{
_id: 101,
quantity: 250,
instock: true,
reorder: false,
details: { model: "14QQ", make: "Clothes Corp" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "Customer007", rating: 4 } ]
}
https://www.mongodb.com/docs/manual/reference/operator/update/set/
๋ฐ์ํ