Database/monogodb

[MongoDB] ๋ชฝ๊ณ  DB $set ์—…๋ฐ์ดํŠธ ์‚ฌ์šฉ ์˜ˆ์ œ, ํŠน์ • field ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

yuri lee 2023. 2. 4. 00:28
๋ฐ˜์‘ํ˜•

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/

๋ฐ˜์‘ํ˜•