Bắt đầu từ MongoDB 5.0, cập nhật các nhà khai thác xử lý các trường tài liệu với tên dựa trên chuỗi theo thứ tự từ vựng. Các trường có tên số được xử lý theo thứ tự số. Xem Cập nhật hành vi của các nhà khai thác để biết chi tiết.
Không sử dụng toán tử vị trí
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 với các hoạt động UPSERT vì chèn sẽ sử dụng {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 làm tên trường trong tài liệu được chèn.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 with upsert operations
because inserts will use the {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 as a field name in the inserted document.Toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí không thể được sử dụng cho các truy vấn vượt qua nhiều hơn một mảng, chẳng hạn như các truy vấn di chuyển mảng được lồng trong các mảng khác, vì sự thay thế cho trình giữ chỗ {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 là một giá trị duy nhất{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 placeholder is a single valueKhi được sử dụng với toán tử
db.collection.updateOne[ |
{ : value ... }, |
{ : { ".$" : value } } |
] |
5, toán tử {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí không loại bỏ phần tử phù hợp khỏi mảng mà thay vào đó đặt nó thành db.collection.updateOne[ |
{ : value ... }, |
{ : { ".$" : value } } |
] |
7.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator does not remove the matching element from the array but rather sets it to db.collection.updateOne[ |
{ : value ... }, |
{ : { ".$" : value } } |
] |
7.Nếu truy vấn khớp với mảng bằng toán tử phủ định, chẳng hạn như
db.collection.updateOne[ |
{ : value ... }, |
{ : { ".$" : value } } |
] |
8, db.collection.updateOne[ |
{ : value ... }, |
{ : { ".$" : value } } |
] |
9 hoặc db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
0, thì bạn không thể sử dụng toán tử vị trí để cập nhật các giá trị từ mảng này.Tuy nhiên, nếu phần phủ định của truy vấn nằm trong biểu thức
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
1, thì bạn có thể sử dụng toán tử vị trí để cập nhật trường này.Toán tử cập nhật
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí hành xử mơ hồ khi lọc trên nhiều trường mảng.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 update operator behaves ambiguously when filtering on multiple array fields.Khi máy chủ thực thi một phương thức cập nhật, trước tiên nó sẽ chạy một truy vấn để xác định tài liệu bạn muốn cập nhật. Nếu bản cập nhật các tài liệu lọc trên nhiều trường mảng, cuộc gọi tiếp theo đến toán tử cập nhật
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 không phải lúc nào cũng cập nhật vị trí cần thiết trong mảng.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 update operator doesn't always update the required position in the array.Để biết thêm thông tin, xem ví dụ.example.
Tạo một bộ sưu tập
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
4 với các tài liệu sau:db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
Để cập nhật phần tử đầu tiên có giá trị là
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
5 lên db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
6 trong mảng db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
7, hãy sử dụng toán tử {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí nếu bạn không biết vị trí của phần tử trong mảng:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator if you do not know the position of the element in the array:Quan trọng
Bạn phải bao gồm trường mảng như một phần của tài liệu
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
9.
db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
Toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí hoạt động như một trình giữ chỗ cho trận đấu đầu tiên của tài liệu truy vấn cập nhật.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator acts as a placeholder for the first match of the update query document.Sau khi hoạt động, bộ sưu tập
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
4 chứa các tài liệu sau:{ "_id" : 1, "grades" : [ 85, 82, 80 ] } |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] } |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
Toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí tạo điều kiện cập nhật cho các mảng có chứa các tài liệu nhúng. Sử dụng toán tử {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí để truy cập các trường trong các tài liệu nhúng với ký hiệu dấu chấm trên toán tử {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator facilitates updates to arrays that contain embedded documents. Use the positional {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator to access the fields in the embedded documents with the
dot notation on the {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator.db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
Hãy xem xét tài liệu sau trong bộ sưu tập
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
4 có giá trị phần tử db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
7 là một mảng các tài liệu nhúng:{ |
_id: 4, |
grades: [ |
{ grade: 80, mean: 75, std: 8 }, |
{ grade: 85, mean: 90, std: 5 }, |
{ grade: 85, mean: 85, std: 8 } |
] |
} |
Sử dụng toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí để cập nhật trường db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
8 của phần tử mảng đầu tiên phù hợp với db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
9 bằng với điều kiện { "_id" : 1, "grades" : [ 85, 82, 80 ] } |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] } |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
0:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator to update the db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
8 field of the first array element that matches the db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
9 equal to { "_id" : 1, "grades" : [ 85, 82, 80 ] } |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] } |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
0 condition:Quan trọng
Bạn phải bao gồm trường mảng như một phần của tài liệu
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
9.db.students.updateOne[ |
{ _id: 4, "grades.grade": 85 }, |
{ $set: { "grades.$.std" : 6 } } |
] |
Toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí hoạt động như một trình giữ chỗ cho trận đấu đầu tiên của tài liệu truy vấn cập nhật.
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
0Sau khi hoạt động, bộ sưu tập
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
4 chứa các tài liệu sau:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator can update the first array element that matches multiple query criteria specified with the db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
1 operator.Toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí tạo điều kiện cập nhật cho các mảng có chứa các tài liệu nhúng. Sử dụng toán tử {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí để truy cập các trường trong các tài liệu nhúng với ký hiệu dấu chấm trên toán tử {
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
1
Hãy xem xét tài liệu sau trong bộ sưu tập
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
4 có giá trị phần tử db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
7 là một mảng các tài liệu nhúng:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 operator updates the value of the db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
8 field in the first embedded document that has db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
9 field with a value less than or equal to { "_id" : 1, "grades" : [ 85, 82, 80 ] } |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] } |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
9 and a db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
0 field with a value greater than db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
5:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
2Sử dụng toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí để cập nhật trường db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
8 của phần tử mảng đầu tiên phù hợp với db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
9 bằng với điều kiện { "_id" : 1, "grades" : [ 85, 82, 80 ] } |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] } |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
0:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
3Sau khi hoạt động, tài liệu có các giá trị được cập nhật sau:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 update operator behaves ambiguously when the query has multiple array fields to filter documents in the collection.
Toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 có thể cập nhật phần tử mảng đầu tiên phù hợp với nhiều tiêu chí truy vấn được chỉ định với toán tử db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
1.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
4Hãy xem xét tài liệu sau trong bộ sưu tập
db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
4 có giá trị trường db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
7 là một mảng các tài liệu nhúng:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
5Trong ví dụ dưới đây, toán tử
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 cập nhật giá trị của trường db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
8 trong tài liệu nhúng đầu tiên có trường db.students.updateOne[ |
{ _id: 1, grades: 80 }, |
{ $set: { "grades.$" : 82 } } |
] |
9 có giá trị nhỏ hơn hoặc bằng { "_id" : 1, "grades" : [ 85, 82, 80 ] } |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] } |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
9 và trường db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
0 có giá trị lớn hơn db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
5:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 update operator to determine which position in the array to update:{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
6Hoạt động này cập nhật tài liệu nhúng đầu tiên phù hợp với các tiêu chí, cụ thể là tài liệu nhúng thứ hai trong mảng:
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
7Nhà điều hành cập nhật
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 vị trí hành xử mơ hồ khi truy vấn có nhiều trường mảng để lọc các tài liệu trong bộ sưu tập.{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
9 update operator instead changed the 2020 value to 2022.Hãy xem xét một tài liệu trong bộ sưu tập
db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
3, chứa các mảng thông tin sinh viên:
Trong ví dụ sau, người dùng cố gắng sửa đổi trường db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
4, lọc các tài liệu bằng các trường db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
5, db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
4 và db.students.insertMany[ [ |
{ "_id" : 1, "grades" : [ 85, 80, 80 ] }, |
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }, |
{ "_id" : 3, "grades" : [ 85, 100, 90 ] } |
] ] |
7 và cập nhật giá trị 2021 trong trường db.collection.updateOne[ |
{ }, |
{ : { "array.$.field" : value } } |
] |
4 lên 2022: