Bài 12: MongoDB - Cập nhật Document - MongoDB

Đăng bởi: Admin | Lượt xem: 2256 | Chuyên mục: SQL


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() đó.

1. Phương thức  update() :

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(SELECTION_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'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>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})

2. Phương thức save() :

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("507f191e810c19729de860ea"), 
		"title":"Tutorials Point New Topic",
      "by":"Tutorials Point"
   }
)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("507f191e810c19729de860ea")
})
>db.mycol.find()
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"Tutorials Point New Topic",
   "by":"Tutorials Point"}
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"NoSQL Overview"}
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"Tutorials Point Overview"}
>

3. Phương thức findOneAndUpdate() :

Phương thức findOneAndUpdate() cập nhật các giá trị tồn tại trong docments
Cú pháp:
>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)
Ví dụ :
Gủa sử chúng ta đã khởi tạo 1 collection có tên là empDetails và chèn thêm 3 documents như sau :
> db.empDetails.insertMany(
	[
		{
			First_Name: "Radhika",
			Last_Name: "Sharma",
			Age: "26",
			e_mail: "radhika_sharma.123@gmail.com",
			phone: "9000012345"
		},
		{
			First_Name: "Rachel",
			Last_Name: "Christopher",
			Age: "27",
			e_mail: "Rachel_Christopher.123@gmail.com",
			phone: "9000054321"
		},
		{
			First_Name: "Fathima",
			Last_Name: "Sheik",
			Age: "24",
			e_mail: "Fathima_Sheik.123@gmail.com",
			phone: "9000054321"
		}
	]
)
Following example updates the age and email values of the document with name 'Radhika'.
> db.empDetails.findOneAndUpdate(
	{First_Name: 'Radhika'},
	{ $set: { Age: '30',e_mail: 'radhika_newemail@gmail.com'}}
)
{
	"_id" : ObjectId("5dd6636870fb13eec3963bf5"),
	"First_Name" : "Radhika",
	"Last_Name" : "Sharma",
	"Age" : "30",
	"e_mail" : "radhika_newemail@gmail.com",
	"phone" : "9000012345"
}

4. Phương thức updateOne() :

Phương thức này cập nhật 1 document mà khớp với giá trị đã lọc
Cú pháp :
>db.COLLECTION_NAME.updateOne(<filter>, <update>)
Ví dụ :
> db.empDetails.updateOne(
	{First_Name: 'Radhika'},
	{ $set: { Age: '30',e_mail: 'radhika_newemail@gmail.com'}}
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }
>

5. Phương thức updateMany() :

Phương thức updateMany() cập nhật tất cả các documents mà phù hợp với giá trị đã lọc 
Cú pháp :
>db.COLLECTION_NAME.update(<filter>, <update>)
Ví dụ :
> db.empDetails.updateMany(
	{Age:{ $gt: "25" }},
	{ $set: { Age: '00'}}
)
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
Ta có thể xem giá trị được cập nhật nếu bạn truy xuất nội dung của tài liệu bằng cách sử dụng phương pháp tìm như sau:
> db.empDetails.find()
{ "_id" : ObjectId("5dd6636870fb13eec3963bf5"), "First_Name" : "Radhika", "Last_Name" : "Sharma", "Age" : "00", "e_mail" : "radhika_newemail@gmail.com", "phone" : "9000012345" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf6"), "First_Name" : "Rachel", "Last_Name" : "Christopher", "Age" : "00", "e_mail" : "Rachel_Christopher.123@gmail.com", "phone" : "9000054321" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf7"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" }
>
Bài tiếp theo: MongoDB - Xoá Document >>
vncoder logo

Theo dõi VnCoder trên Facebook, để cập nhật những bài viết, tin tức và khoá học mới nhất!