Hướng dẫn update all elements in array mongodb - cập nhật tất cả các phần tử trong mảng mongodb

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

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 Tất cả toán tử vị trí
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 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.
Hướng dẫn update all elements in array mongodb - cập nhật tất cả các phần tử trong mảng mongodb

The all positional operator

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 indicates that the update operator should modify all elements in the specified array field.

Toán tử

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 có biểu mẫu sau:

{ <update operator>: { "<array>.$[]" : value } }

Sử dụng trong các hoạt động cập nhật, ví dụ:

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
1 và
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
2, để sửa đổi tất cả các thành phần mảng cho tài liệu hoặc tài liệu phù hợp với điều kiện truy vấn. Ví dụ:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)

Ví dụ, xem Cập nhật tất cả các yếu tố trong một mảng.Update All Elements in an Array.

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.

Nếu hoạt động UPSERT dẫn đến một phần chèn,

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
3 phải bao gồm một khớp bình đẳng chính xác trên trường mảng để sử dụng toán tử vị trí
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 trong câu lệnh cập nhật.

Ví dụ: thao tác UPSERT sau, sử dụng

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 trong tài liệu cập nhật, chỉ định điều kiện khớp bình đẳng chính xác trên trường mảng:

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)

Nếu không có tài liệu nào như vậy, hoạt động sẽ dẫn đến việc chèn tài liệu sau:

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

Nếu hoạt động UPSERT không bao gồm khớp bình đẳng chính xác và không có tài liệu phù hợp nào được tìm thấy để cập nhật, hoạt động UPSERT sẽ xảy ra lỗi.

Ví dụ: các hoạt động sau đây sẽ xảy ra lỗi nếu không tìm thấy tài liệu phù hợp nào để cập nhật:

db.emptyCollection.updateOne(
{ },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
db.emptyCollection.updateOne(
{ myArray: 5 },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)

Toán tử

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 có thể được sử dụng cho các truy vấn vượt qua nhiều mảng và mảng lồng nhau.

Ví dụ, xem bản cập nhật các mảng lồng nhau kết hợp với

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
7Update Nested Arrays in Conjunction with
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
7

Tạo bộ sưu tập

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
8:

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

Để tăng tất cả các yếu tố trong mảng

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
9 bằng

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

0 cho tất cả các tài liệu trong bộ sưu tập, hãy sử dụng toán tử
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 vị trí:
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
operator:

db.students.updateMany(
{ },
{ $inc: { "grades.$[]": 10 } },
)

Toán tử tất cả các vị trí

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 hoạt động như một trình giữ chỗ cho tất cả các yếu tố trong trường mảng.
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
operator acts as a placeholder for all elements in the array field.

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

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
8 chứa các tài liệu sau:

{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 102 ] }
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }

Toán tử vị trí

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 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. Để truy cập các trường trong các tài liệu nhúng, hãy sử dụng ký hiệu dấu chấm với toán tử
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8.
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
positional operator facilitates updates to arrays that contain embedded documents. To access the fields in the embedded documents, use the dot notation with the
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
operator.

db.collection.updateOne(
{ <query selector> },
{ <update operator>: { "array.$[].field" : value } }
)

Tạo bộ sưu tập

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

6:

db.students2.insertMany( [
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 8 },
{ "grade" : 85, "mean" : 90, "std" : 6 },
{ "grade" : 85, "mean" : 85, "std" : 8 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 8 },
{ "grade" : 87, "mean" : 90, "std" : 5 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
}
] )

Để sửa đổi giá trị của trường

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

7 cho tất cả các phần tử trong mảng
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
9, hãy sử dụng toán tử
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 vị trí:
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
operator:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
0

Sau khi hoạt động, bộ sưu tập có các tài liệu sau:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
1

Tạo bộ sưu tập

db.emptyCollection.updateOne(
{ },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
db.emptyCollection.updateOne(
{ myArray: 5 },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
0:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
2

Để tăng tất cả các yếu tố trong mảng

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
9 bằng

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

0 cho tất cả các tài liệu ngoại trừ các phần tử có giá trị
db.emptyCollection.updateOne(
{ },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
db.emptyCollection.updateOne(
{ myArray: 5 },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
3 trong mảng
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
9, hãy sử dụng toán tử
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 vị trí:except those with the value
db.emptyCollection.updateOne(
{ },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
db.emptyCollection.updateOne(
{ myArray: 5 },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
3 in the
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
9 array, use the all positional
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
operator:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
3

Toán tử tất cả các vị trí

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 hoạt động như một trình giữ chỗ cho tất cả các yếu tố trong trường mảng.
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8
operator acts as a placeholder for all elements in the array field.

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

db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
8 chứa các tài liệu sau:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
4

Toán tử vị trí

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 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. Để truy cập các trường trong các tài liệu nhúng, hãy sử dụng ký hiệu dấu chấm với toán tử
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8.

Tạo bộ sưu tập

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

6:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
5

Để sửa đổi giá trị của trường

{ "_id" : ObjectId(...), "myArray" : [ 10, 10 ] }

7 cho tất cả các phần tử trong mảng
db.collection.updateOne(
{ myArray: [ 5, 8 ] },
{ $set: { "myArray.$[]": 10 } },
{ upsert: true }
)
9, hãy sử dụng toán tử
db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
8 vị trí:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
6

Sau khi hoạt động, bộ sưu tập có các tài liệu sau:

db.collection.updateOne(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
7

Tạo bộ sưu tập db.emptyCollection.updateOne( { }, { $set: { "myArray.$[]": 10 } }, { upsert: true })db.emptyCollection.updateOne( { myArray: 5 }, { $set: { "myArray.$[]": 10 } }, { upsert: true })0:

Làm thế nào để bạn cập nhật dữ liệu trong một mảng các đối tượng trong MongoDB?

Cập nhật tài liệu trong một mảng Nhà điều hành $ định vị 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ử $ định vị để 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ử $.The positional $ operator facilitates updates to arrays that contain embedded documents. Use the positional $ operator to access the fields in the embedded documents with the dot notation on the $ operator.

Làm cách nào để cập nhật nhiều yếu tố trong MongoDB?

Cập nhật nhiều trường của một tài liệu.Chúng tôi có thể sử dụng các nhà khai thác $ Set và $ Inc để cập nhật bất kỳ trường nào trong MongoDB.Toán tử $ SET sẽ đặt giá trị mới được chỉ định trong khi toán tử $ Inc sẽ tăng giá trị theo giá trị được chỉ định.use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value.

Làm cách nào để cập nhật một mảng lồng nhau trong MongoDB?

Cập nhật các mảng lồng nhau kết hợp với toán tử vị trí $ [] $ [], kết hợp với toán tử $ [] tất cả các toán tử vị trí, có thể được sử dụng để cập nhật các mảng lồng nhau.Các bản cập nhật sau đây các giá trị lớn hơn hoặc bằng 8 trong các lớp lồng nhau.The $[] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades.

Làm cách nào để cập nhật dữ liệu hiện có trong MongoDB?

Shell MongoDB cung cấp các phương pháp sau để cập nhật các tài liệu trong một bộ sưu tập:..
Để cập nhật một tài liệu duy nhất, hãy sử dụng DB.thu thập.Cập nhật ().
Để cập nhật nhiều tài liệu, hãy sử dụng DB.thu thập.Updatemany ().
Để thay thế một tài liệu, sử dụng DB.thu thập.thay thế ().