ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MongoDB] Database, Collection, Document ์ƒ์„ฑ, ์‚ญ์ œ
    NoSQL/MongoDB 2022. 10. 31. 22:47

    ๐ŸŒˆ MongoDB  ⇔  RDBMS

    MongoDB RDBMS
    Database Database
    Collection Table
    Document Tuple/Row
    Field Column
    Primary Key ( Default _id ) Primary Key

     

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ ใ…ก use

    use ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

    ์ด๋ฏธ ์กด์žฌํ•  ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    use ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…

    > use demo
    switched to db demo

    ์‚ฌ์šฉ ์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ™•์ธ ํ•˜๋ ค๋ฉด db๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

    > db
    demo

     

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ญ์ œ ใ…ก db.dropDatabase()

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ญ์ œ๋Š” use๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ ํƒ ํ›„ ํ•ด๋‹น ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

    > db.dropDatabase()
     {"dropped" : "demo", "ok" : 1 }

     

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก ํ™•์ธ ใ…ก show dbs

    ์ƒ์„ฑํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์•ˆ๋ณด์ผ๋•Œ๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋„ํ๋จผํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    ๋„ํ๋จผํŠธ๋Š” RDBMS์˜ Row(ํ–‰)๊ณผ ๊ฐ™์€ ๊ฐœ๋…์œผ๋กœ ์ปฌ๋ ‰์…˜์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    > show dbs
    admin    40.00 KiB
    config  108.00 KiB
    local    72.00 KiB

     

    ์ปฌ๋ ‰์…˜ ์ƒ์„ฑ ใ…ก db.createCollection()
    > db.createCollection("demo")
    { ok: 1 }

     

    ์ปฌ๋ ‰์…˜ ์‚ญ์ œ ใ…ก db.์ปฌ๋ ‰์…˜์ด๋ฆ„.drop()
     > db.demo.drop()
     true

     

    ๋„ํ๋จผํŠธ ์‚ฝ์ž…(๋‹จ๊ฑด) ใ…ก db.์ปฌ๋ ‰์…˜์ด๋ฆ„.insertOne({})

    ์ปฌ๋ ‰์…˜์ด ์กด์žฌ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปฌ๋ ‰์…˜ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ๋„ํ๋จผํŠธ๋ฅผ ์‚ฝ์ž…ํ•ด์ค๋‹ˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ _id๊ฐ€ ๊ฐ™์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ ์ด ๊ฐ’์€ ๋„ํ๋จผํŠธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๊ณ ์œ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

    > db.board.insertOne({"title": "board title"})
    {
      acknowledged: true,
      insertedId: ObjectId("635a97fb547961992c6c5eaa")
    }

     

    ๋„ํ๋จผํŠธ ์‚ฝ์ž…(๋‹ค๊ฑด) ใ…ก db.์ปฌ๋ ‰์…˜์ด๋ฆ„.insertMany([{}, {}, ...])

    ์—ฌ๋Ÿฌ ๋„ํ๋จผํŠธ๋ฅผ ๋‹จ์ผ ์ปฌ๋ ‰์…˜์— ์‚ฝ์ž…ํ• ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    > db.board.insertMany([{"title": "board title2"},{"title": "board title2"}])
    {
      acknowledged: true,
      insertedIds: {
        '0': ObjectId("635aa482547961992c6c5eac"),
        '1': ObjectId("635aa482547961992c6c5ead")
      }
    }

     

    ๋„ํ๋จผํŠธ ์‚ญ์ œ(๋‹จ๊ฑด) ใ…ก db.์ปฌ๋ ‰์…˜์ด๋ฆ„.deleteOne({})

    ํ•„ํ„ฐ์— ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์กด์žฌ ํ•ด๋„ ์ฒซ๋ฒˆ์งธ ๋„ํ๋จผํŠธ๋งŒ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

    > db.board.deleteOne({"title":"board title"})
    { acknowledged: true, deletedCount: 1 }

     

    ๋„ํ๋จผํŠธ ์‚ญ์ œ(๋‹ค๊ฑด) ใ…ก db.์ปฌ๋ ‰์…˜์ด๋ฆ„.deleteMany({})

    ํ•„ํ„ฐ์— ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋„ํ๋จผํŠธ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

    > db.board.deleteMany({"title":"board title2"})
    { acknowledged: true, deletedCount: 2 }

     

    ๋Œ“๊ธ€

Designed by Tistory.