Blog

Cập nhật Document trong MongoDB

Phương thức update() hoặc save() trong MongoDB được sử dụng để cập nhật Document vào trong một Collection.

Phương thức update() cập nhật các giá trị trong Document đang tồn tại trong khi phương thức save() thay thế Document đang tồn tại với Document đã truyền trong phương thức save() đó.

Phương thức update() trong MongoDB

Phương thức update() cập nhật các giá trị trong Document đang tồn tại.

Cú pháp

Cú pháp cơ bản của phương thức update() là như sau:

>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

Ví dụ

Bạn theo dõi Collection có tên mycol có dữ liệu sau:

{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

Ví dụ sau sẽ thiết lập tiêu đề mới ‘New MongoDB Tutorial’ của Document có tiêu đề là ‘MongoDB Overview’:

>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>

Theo mặc định, MongoDB sẽ chỉ cập nhật một Document đơn, để cập nhật nhiều Document, bạn thiết lập tham số ‘multi’ thành true.

>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})

Phương thức save() trong MongoDB

Phương thức save() thay thế Document đang tồn tại với Document mới đã được truyền trong phương thức save() này.

Cú pháp

Cú pháp cơ bản của phương thức save() như sau:

>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

Ví dụ

Ví dụ sau sẽ thay thế Document với _id là ‘5983548781331adf45ec7’.

>db.mycol.save(
   {
      "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New Topic", "by":"Tutorials Point"
   }
)
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Tutorials Point New Topic", "by":"Tutorials Point"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>

Leave a Reply

Your email address will not be published. Required fields are marked *

bujangjp slotsensa slot88 slot4d slot4d slot4d sv388 slot88 suhubet demo slot slot 4d demo slot https://fabritec.pe/asesor.php suhubet bujangjp https://kibf.pk/ slotsensa buku303 slot4d sbobet slot gacor slot gacor slot gacor totogacor slot thailand https://www.jms.ac.in/admission-procedure/ slot gacor slot luar negeri slot gacor 4d totogacor slot 777 slot gacor slot gacor slotsensa https://adibintanpermata.com/ slot dana slot dana https://united-mgroup.com/ https://cliffcrestinn.com/ slot resmi demo slot slot thailand slot thailand https://www.mikehanrahan.com/ https://www.ugelhuaylas.edu.pe/ totogacor https://hospital.avrasiya.az/ totogacor totogacor demo slot slot 4d totogacor totogacor demo slot totogacor slot thailand totogacor bujangjp situs gacor 4D situs gacor Situs Gacor 4D Thailand Situs Gacor 4D SLOT4D slot gacor