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 *

https://jmspc.ac.in/ slot4d slotsensa slot gacor https://www.jmsit.ac.in/placement/ suhubet slot4d https://fabritec.pe/asesor.php https://kibf.pk/ slot4d sbobet slot gacor https://www.jms.ac.in/admission-procedure/ slot gacor slot luar negeri slot gacor 4d totogacor totogacor slot4d slot4d slot gacor slot gacor slot gacor https://promo-sign.com/ slot4d demo slot slot thailand slot4d https://www.ugelhuaylas.edu.pe/ slot4d slot4d slot thailand slot4d slot4d https://dulsa.com/ slot4d slot4d slot4d https://inovasorocaba.parquetecsorocaba.com.br/ slot4d https://ppi24.sch.id/ https://fabritec.pe/see/corporacion/ slot4d https://adibintanpermata.com/lp-ql/ https://waterco.co.id/pompa-kolam-renang/ https://cliffcrestinn.com/rates/ slot demo https://www.bhagwati.ac.in/academics/ https://seteilhas.com.br/site/ https://lppm.handayani.ac.id/kklp/ https://www.homeofenglish.edu.kh/ https://www.uniparconstrutora.com.br/ totogacor Slot Gacor situs gacor 4D situs gacor slot4d SLOT4D slot4d slot gacor 4d Slot Thailand slot4d Slot Thailand Slot Thailand Slot Gacor Maxwin Slot Thailand Slot Thailand