MongoDB cung cấp lệnh update[] để cập nhật các tài liệu của một bộ sưu tập. Để chỉ cập nhật những tài liệu bạn muốn cập nhật, bạn có thể thêm tiêu chí vào câu lệnh cập nhật để chỉ những tài liệu được chọn mới được cập nhật
Các tham số cơ bản trong lệnh là điều kiện để tài liệu cần cập nhật, tiếp theo là sửa đổi cần thực hiện
Ví dụ sau đây cho thấy làm thế nào điều này có thể được thực hiện
Bước 1] Ra lệnh cập nhật
Bước 2] Chọn điều kiện mà bạn muốn sử dụng để quyết định tài liệu nào cần được cập nhật. Trong ví dụ của chúng tôi, chúng tôi muốn cập nhật tài liệu có ID nhân viên 22
Bước 3] Sử dụng lệnh set để sửa đổi Tên trường
Bước 4] Chọn Tên trường bạn muốn sửa đổi và nhập giá trị mới cho phù hợp
db.Employee.update[ {"Employeeid" : 1}, {$set: { "EmployeeName" : "NewMartin"}}];
Nếu lệnh được thực hiện thành công, Kết quả sau sẽ được hiển thị
đầu ra
[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]]
Phần sau sửa đổi các tài liệu phù hợp bằng cách sử dụng phương pháp
db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
2 trong bộ sưu tập db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
8Ví dụ. cập nhậtMany[]
Sao chépdb.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
đầu ra
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
Trong ví dụ trên, tham số đầu tiên là tiêu chí lọc được chỉ định dưới dạng tài liệu,
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
1 chỉ ra rằng tìm tài liệu có {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
2 là {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
3. Tham số thứ hai được sử dụng để chỉ định các trường và giá trị sẽ được sửa đổi trên tài liệu phù hợp ở định dạng {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
4. Sử dụng để chỉ định một hành động để thực hiện. Ở đây chúng tôi muốn đặt giá trị của các trường, vì vậy hãy sử dụng toán tử {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
5 để chỉ định các trường và giá trị được cập nhật ở định dạng {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
6. {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
7 sửa đổi các trường {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
2 của tất cả các tài liệu phù hợp thành {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
9Ở đầu ra,
db.employees.find[]
0 cho biết số lượng tài liệu phù hợp với tiêu chí và db.employees.find[]
1 cho biết số lượng tài liệu được cập nhậtBây giờ, hãy kiểm tra xem nó đã cập nhật một giá trị hay chưa bằng cách sử dụng phương pháp
db.employees.find[]
2 được hiển thị bên dướiKiểm tra tài liệu cập nhật
db.employees.find[]
đầu ra
[
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
Phương pháp
db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
2 thêm trường được chỉ định nếu nó không tồn tại trong tài liệu phù hợp. Ví dụ: phần sau sẽ thêm trường db.employees.find[]
4Ví dụ. cập nhậtMany[]
Sao chépdb.employees.updateMany[{firstName:"Steve"}, { $set: {location: "USA"}}]
đầu ra
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Thực hiện phương pháp
db.employees.find[]
2 sau đây để xem dữ liệu được cập nhậtKiểm tra tài liệu cập nhật
Sao chépdb.employees.find[{firstName:"Steve"}]
đầu ra
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 8500,
location:"USA"
}
Nếu bạn chỉ định một tiêu chí lọc trống
db.employees.find[]
6, thì nó sẽ cập nhật tất cả các tài liệu. Sau đây sẽ cập nhật hoặc thêm trường db.employees.find[]
4 trong tất cả các tài liệuVí dụ. cập nhậtMany[]
db.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
0đầu ra
db.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
1Sử dụng toán tử cập nhật
db.employees.find[]
8 để tăng giá trị của trường theo số lượng đã chỉ định. Những điều sau đây tăng {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
2 lên [
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
0 có mức lương là {
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
9Ví dụ. toán tử $inc
Sao chépdb.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
2đầu ra
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
Cập nhật nhiều trường
Bạn cũng có thể chỉ định nhiều trường để cập nhật. Các cập nhật sau các trường
[
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
2 và [
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
3Thí dụ. Cập nhật nhiều trường
Sao chépdb.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
4đầu ra
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Thực hiện phương pháp
db.employees.find[]
2 sau đây để xem dữ liệu được cập nhậtKiểm tra tài liệu cập nhật
db.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
6đầu ra
db.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
7Phương pháp
db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
2 không cập nhật bất kỳ tài liệu nào nếu không tìm thấy tài liệu phù hợp. Ví dụ: phần sau sẽ không cập nhật bất kỳ tài liệu nàoVí dụ. cập nhậtMany[]
Sao chépdb.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
8đầu ra
db.employees.insertMany[[
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
]]
9Upsert - Thêm nếu không tồn tại
Chỉ định
[
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
6 làm tham số thứ ba trong phương thức [
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
7. [
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 8500
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500
},
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 8500
}
]
8 thêm một tài liệu mới nếu không tìm thấy tài liệu phù hợpVí dụ. Upsert
Sao chépdb.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
0đầu ra
db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
1Trong ví dụ trên, MongoDB thêm một tài liệu mới với _____24____9 mới, bởi vì nó không thể tìm thấy một tài liệu với ____27_______0
Cập nhật toán tử
Bảng sau đây liệt kê các toán tử cập nhật có thể được sử dụng với các phương thức
db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
4 và db.employees.updateMany[{ salary:7000 }, { $set: { salary:8500 }}]
2MethodDescription$currentDateĐặt giá trị của một trường thành ngày hiện tại, dưới dạng Ngày hoặc Dấu thời gian. $incTăng giá trị của trường theo số lượng đã chỉ định. $minChỉ cập nhật trường nếu giá trị đã chỉ định nhỏ hơn giá trị trường hiện có. $maxOnly cập nhật trường nếu giá trị được chỉ định lớn hơn giá trị trường hiện có. $mulNhân giá trị của trường với số lượng đã chỉ định. $renameĐổi tên trường. $setĐặt giá trị của một trường trong tài liệu. $setOnInsertĐặt giá trị của trường nếu một bản cập nhật dẫn đến việc chèn tài liệu. Không ảnh hưởng đến các hoạt động cập nhật sửa đổi các tài liệu hiện có. $unsetXóa trường đã chỉ định khỏi tài liệu