Hướng dẫn mongodb update array element by id - mongodb cập nhật phần tử mảng theo id

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 Toán tử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 định vị xác định một phần tử trong một mảng để cập nhật mà không chỉ định rõ ràng vị trí của phần tử trong mảng.

The positional

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 operator identifies an element in an array to update without explicitly specifying the position of the element in the array.

Ghi chú

Định hướng

  • Để dự án, hoặc trả về, một phần tử mảng từ hoạt động đọc, hãy xem toán tử chiếu

    db.students.insertMany( [
    { "_id" : 1, "grades" : [ 85, 80, 80 ] },
    { "_id" : 2, "grades" : [ 88, 90, 92 ] },
    { "_id" : 3, "grades" : [ 85, 100, 90 ] }
    ] )
    5 thay thế.

  • Để cập nhật tất cả các yếu tố trong một mảng, hãy xem tất cả các toán tử vị trí

    db.students.insertMany( [
    { "_id" : 1, "grades" : [ 85, 80, 80 ] },
    { "_id" : 2, "grades" : [ 88, 90, 92 ] },
    { "_id" : 3, "grades" : [ 85, 100, 90 ] }
    ] )
    8 thay thế.

  • Để cập nhật tất cả các yếu tố phù hợp với điều kiện hoặc điều kiện bộ lọc mảng, hãy xem toán tử vị trí được lọc thay vì

    db.students.insertMany( [
    { "_id" : 1, "grades" : [ 85, 80, 80 ] },
    { "_id" : 2, "grades" : [ 88, 90, 92 ] },
    { "_id" : 3, "grades" : [ 85, 100, 90 ] }
    ] )
    9

Toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí có biểu mẫu:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator has the form:

Khi được sử dụng với các hoạt động cập nhật, ví dụ:

db.students.updateOne(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
)
1 và
db.students.updateOne(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
)
2

  • Toán tử

    db.students.insertMany( [
    { "_id" : 1, "grades" : [ 85, 80, 80 ] },
    { "_id" : 2, "grades" : [ 88, 90, 92 ] },
    { "_id" : 3, "grades" : [ 85, 100, 90 ] }
    ] )
    5 vị trí hoạt động như một trình giữ chỗ cho phần tử đầu tiên phù hợp với
    db.students.updateOne(
    { _id: 1, grades: 80 },
    { $set: { "grades.$" : 82 } }
    )
    4 và
    db.students.insertMany( [
    { "_id" : 1, "grades" : [ 85, 80, 80 ] },
    { "_id" : 2, "grades" : [ 88, 90, 92 ] },
    { "_id" : 3, "grades" : [ 85, 100, 90 ] }
    ] )
    5
    operator acts as a placeholder for the first element that matches the
    db.students.updateOne(
    { _id: 1, grades: 80 },
    { $set: { "grades.$" : 82 } }
    )
    4, and

  • Trường

    db.students.updateOne(
    { _id: 1, grades: 80 },
    { $set: { "grades.$" : 82 } }
    )
    5 phải xuất hiện như một phần của
    db.students.updateOne(
    { _id: 1, grades: 80 },
    { $set: { "grades.$" : 82 } }
    )
    6.must appear as part of the
    db.students.updateOne(
    { _id: 1, grades: 80 },
    { $set: { "grades.$" : 82 } }
    )
    6.

Ví dụ:

db.collection.updateOne(
{ : value ... },
{ : { ".$" : value } }
)

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í

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 với các hoạt động UPSERT vì chèn sẽ sử dụng
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 làm tên trường trong tài liệu được chèn.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
with upsert operations because inserts will use the
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 as a field name in the inserted document.

Toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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ỗ
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 là một giá trị duy nhất
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
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
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
placeholder is a single value

Khi được sử dụng với toán tử

{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
1, toán tử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
3.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator does not remove the matching element from the array but rather sets it to
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
3.

Nếu truy vấn khớp với mảng bằng toán tử phủ định, chẳng hạn như

{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
4,
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
5 hoặc
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
6, 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

{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
7, 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

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí hành xử mơ hồ khi lọc trên nhiều trường mảng.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
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

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 không phải lúc nào cũng cập nhật vị trí cần thiết trong mảng.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
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.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
0 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.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
1 lên
db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
2 trong mảng
db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
3, hãy sử dụng toán tử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí nếu bạn không biết vị trí của phần tử trong mảng:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
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 Array như một phần của tài liệu

db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
5.

db.students.updateOne(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
)

Toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
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.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
0 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ử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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ử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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ử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator facilitates updates to arrays that contain embedded documents. Use the positional
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator to access the fields in the embedded documents with the dot notation on the
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator.

db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)

Hãy xem xét tài liệu sau trong bộ sưu tập

db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
0 có giá trị phần tử
db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
3 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ử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí để cập nhật trường
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
4 của phần tử mảng đầu tiên phù hợp với
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
5 bằng với điều kiện
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
6:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator to update the
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
4 field of the first array element that matches the
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
5 equal to
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
6 condition:

Quan trọng

Bạn phải bao gồm trường Array như một phần của tài liệu

db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
5.

db.students.updateOne(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)

Toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}

Sau khi hoạt động, bộ sưu tập

db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
0 chứa 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 ] }
] )
5
operator can update the first array element that matches multiple query criteria specified with the
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
7 operator.

Toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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ử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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ử
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5.

{
_id: 5,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 }
]
}

Hãy xem xét tài liệu sau trong bộ sưu tập

db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
0 có giá trị phần tử
db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
3 là một mảng các tài liệu nhúng:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
operator updates the value of the
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
4 field in the first embedded document that has
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
5 field with a value less than or equal to
db.students.updateOne(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)
5 and a
db.students.updateOne(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)
6 field with a value greater than
db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
1:

db.students.updateOne(
{
_id: 5,
grades: { $elemMatch: { grade: { $lte: 90 }, mean: { $gt: 80 } } }
},
{ $set: { "grades.$.std" : 6 } }
)

Sử dụng toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí để cập nhật trường
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
4 của phần tử mảng đầu tiên phù hợp với
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
5 bằng với điều kiện
{
_id: 4,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 85, mean: 85, std: 8 }
]
}
6:

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
0

Sau khi hoạt động, tài liệu có các giá trị được cập nhật sau:

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 update operator behaves ambiguously when the query has multiple array fields to filter documents in the collection.

Toán tử

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 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ử
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
7.

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
1

Trong ví dụ sau, người dùng cố gắng sửa đổi trường

{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
0, lọc các tài liệu bằng các trường
{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
1,
{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
0 và
db.collection.updateOne(
{ },
{ : { "array.$.field" : value } }
)
3 và cập nhật giá trị 2021 trong trường
{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
0 lên 2022:

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
2

Khi máy chủ thực thi phương thức

{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
5 ở trên, nó sẽ lọc các tài liệu có sẵn bằng các giá trị trong các trường mảng được cung cấp. Mặc dù trường
{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
0 được sử dụng trong bộ lọc, nhưng nó không phải là trường được sử dụng bởi toán tử cập nhật
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí để xác định vị trí nào trong mảng để cập nhật:
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
update operator to determine which position in the array to update:

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
3

Ví dụ đầu ra:

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
4

Phương thức

{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
5 phù hợp với trường
{
"_id" : 4,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
}
0 vào năm 2021, nhưng toán tử cập nhật
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5 vị trí thay thế thay đổi giá trị 2020 thành 2022.
db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
5
update operator instead changed the 2020 value to 2022.

Để tránh kết quả không mong muốn khi khớp trên nhiều mảng, thay vào đó, hãy sử dụng toán tử vị trí được lọc

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )
9

Mẹo

Làm thế nào để MongoDB cập nhật dữ liệu bằng ID?

Các bước thực hiện như sau:..
Bước 1: Trong bước đầu tiên, bạn cần lưu trữ ObjectID vào một biến. anyvariablename = db. ....
Bước 2: Trong bước thứ hai, bạn cần đặt ID mới. ....
Bước 3: Trong bước thứ ba, bạn cần chèn ID mới vào tài liệu. ....
Bước 4: Trong bước thứ tư, bạn cần xóa ID cũ ..

Chúng ta có thể cập nhật ID Field MongoDB không?

Trường _ID là bất biến, đó là, một khi một tài liệu tồn tại trong hệ thống MongoDB của bạn, theo định nghĩa, nó đã được gán một _ID và bạn không thể thay đổi hoặc cập nhật khóa chính của nó. Điều đó nói rằng, _ID có thể bị ghi đè khi bạn chèn các tài liệu mới, nhưng theo mặc định, nó sẽ được điền với một ObjectID.you cannot change or update its primary key. That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID.

Làm thế nào nối một phần tử trong mảng từ MongoDB?

Nếu giá trị là một mảng, $ Push sẽ cộng đồng toàn bộ mảng là một phần tử duy nhất.Để thêm từng phần tử của giá trị một cách riêng biệt, hãy sử dụng $ mỗi công cụ sửa đổi với $ Push.Ví dụ, hãy xem thêm một giá trị vào các mảng trong nhiều tài liệu.Để biết danh sách các công cụ sửa đổi có sẵn cho $ Push, xem các sửa đổi.$push appends the whole array as a single element. To add each element of the value separately, use the $each modifier with $push . For an example, see Append a Value to Arrays in Multiple Documents. For a list of modifiers available for $push , see Modifiers.

$ [] Trong MongoDB là gì?

Sự định nghĩa.$ [] Tất cả toán tử vị trí $ [] chỉ ra rằng toán tử cập nhật nên sửa đổi tất cả các phần tử trong trường mảng được chỉ định.Toán tử $ [] có dạng sau: {: {". $ []": Value}}The all positional operator $[] indicates that the update operator should modify all elements in the specified array field. The $[] operator has the following form: { : { ".$[]" : value } }