티스토리 뷰

3. 스크립트

몽고DB/2022/04/23/모술/ ⑧ CRUD 작업

패스트코드블로그 2022. 4. 22. 23:57

몽고DB 홈페이지 CRUD 예제

 

Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

 

db.inventory.insertOne(
  { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)

db.inventory.insertMany( [
   { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
   { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
   { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );



db.inventory.find( { item: "canvas" } )

 

db.inventory.find( { tags: ["red", "blank"] } )

db.inventory.find( { tags: { $all: ["red", "blank"] } } )

db.inventory.find( { dim_cm: { $gt: 25 } } )

db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

 

When querying using dot notation, the field and nested field must be inside quotation marks.

 

db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

 

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

= SELECT _id, item, status from inventory WHERE status = "A"

 

db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )

= SELECT item, status from inventory WHERE status = "A"

 

db.inventory.updateOne(

   { item: "paper" },

   {

     $set: { "size.uom": "cm", status: "P" },

     $currentDate: { lastModified: true }

   }

)

db.inventory.updateMany(

   { "qty": { $lt: 50 } },

   {

     $set: { "size.uom": "in", status: "P" },

     $currentDate: { lastModified: true }

   }

)



 db.inventory.deleteOne( { status: "D" } )

 db.inventory.deleteMany({ status : "A" })

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함