Devdit
 

MongoDB update where ด้วย updateOne

790

MongoDB update where ด้วย updateOne บทความนี้สอนใช้คำสั่ง updateOne เพื่อแก้ไขข้อมูล หรือ document ใน collection พร้อมกำหนดเงื่อนไข และค่าใหม่ตามที่ต้องการ พร้อมแสดงผลลัพธ์ออกสู่หน้าจอ สามารถเขียนคำสั่งได้ดังนี้

 

ตัวอย่าง MongoDB update where ด้วย updateOne

db.product.updateOne(
    { "_id" : ObjectId("62e60ea9ff7943c624b35893") },
    { $set: { "name" : "MongoDB" } }
)

ผลลัพธ์

{ acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0 }

คำอธิบาย

MongoDB update where จากตัวอย่างใช้คำสั่ง db.product.updateOne คือแก้ไขข้อมูลในตาราง หรือ collection ชื่อ product ด้วยคำสั่ง updateOne โดย where หรือเงื่อนไขคือ _id ต้องมีค่าเท่ากับ ObjectId("62e60ea9ff7943c624b35893") และใช้คำสั่ง $set เพื่อกำหนด field และข้อมูลที่จะแก้ไข "name" : "MongoDB" คือ แก้ไขข้อมูลจาก field name เป็น MongoDB

เขียน 1 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ