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

Tôi đang cố gắng cập nhật một Subelement duy nhất có trong một mảng trong tài liệu MongoDB. Tôi muốn tham chiếu trường bằng chỉ mục mảng của nó [các phần tử trong mảng không có bất kỳ trường nào mà tôi có thể đảm bảo sẽ là số nhận dạng duy nhất]. Có vẻ như điều này sẽ dễ dàng thực hiện, nhưng tôi không thể tìm ra cú pháp.

Đây là những gì tôi muốn làm trong Pseudo-Json.

Before:

{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... old content B ... },
    { ... old content C ... }
  ]
}

After:

{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}

Có vẻ như truy vấn phải là một cái gì đó như thế này:

//pseudocode
db.my_collection.update[
  {_id: ObjectId[document_id], my_array.1 : 1 },
  {my_array.$.content: NEW content B }
]

Nhưng điều này không hoạt động. Tôi đã dành quá lâu để tìm kiếm các tài liệu MongoDB và thử các biến thể khác nhau trên cú pháp này [ví dụ: sử dụng

{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}
8, v.v.]. Tôi không thể tìm thấy bất kỳ lời giải thích rõ ràng nào về cách thực hiện loại cập nhật này trong MongoDB.

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

{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}
9 Toán tử
{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}
9 đị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

{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}
9 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

    {
      _id : ...,
      other_stuff ... ,
      my_array : [
        { ... old content A ... },
        { ... NEW content B ... },
        { ... old content C ... }
      ]
    }
    
    9 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í

    //pseudocode
    db.my_collection.update[
      {_id: ObjectId[document_id], my_array.1 : 1 },
      {my_array.$.content: NEW content B }
    ]
    
    2 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ì

    //pseudocode
    db.my_collection.update[
      {_id: ObjectId[document_id], my_array.1 : 1 },
      {my_array.$.content: NEW content B }
    ]
    
    3

Toán tử

{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}
9 vị trí có biểu mẫu:
{
  _id : ...,
  other_stuff ... ,
  my_array : [
    { ... old content A ... },
    { ... NEW content B ... },
    { ... old content C ... }
  ]
}
9
operator has the form:

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

//pseudocode
db.my_collection.update[
  {_id: ObjectId[document_id], my_array.1 : 1 },
  {my_array.$.content: NEW content B }
]
5 và
//pseudocode
db.my_collection.update[
  {_id: ObjectId[document_id], my_array.1 : 1 },
  {my_array.$.content: NEW content B }
]
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 phần tử đầu tiên phù hợp với
    //pseudocode
    db.my_collection.update[
      {_id: ObjectId[document_id], my_array.1 : 1 },
      {my_array.$.content: NEW content B }
    ]
    
    8 và
    {
      _id : ...,
      other_stuff ... ,
      my_array : [
        { ... old content A ... },
        { ... NEW content B ... },
        { ... old content C ... }
      ]
    }
    
    9
    operator acts as a placeholder for the first element that matches the
    //pseudocode
    db.my_collection.update[
      {_id: ObjectId[document_id], my_array.1 : 1 },
      {my_array.$.content: NEW content B }
    ]
    
    8, and

  • Trường

    //pseudocode
    db.my_collection.update[
      {_id: ObjectId[document_id], my_array.1 : 1 },
      {my_array.$.content: NEW content B }
    ]
    
    9 phải xuất hiện như một phần của
    db.collection.updateOne[
    { : value ... },
    { : { ".$" : value } }
    ]
    0.must appear as part of the
    db.collection.updateOne[
    { : value ... },
    { : { ".$" : value } }
    ]
    0.

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í

{
  _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 value

Khi đượ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 ... }
  ]
}
0

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 : ...,
  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 ... }
  ]
}
2

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 ... }
  ]
}
3

Sau 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 ... }
  ]
}
4

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ị 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 ... }
  ]
}
5

Trong 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 ... }
  ]
}
6

Hoạ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 ... }
  ]
}
7

Nhà đ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:

Làm thế nào để bạn cập nhật một yếu tố cụ thể trong một mảng trong MongoDB?

Để cập nhật một tập hợp các phần tử phù hợp với một số bộ lọc nhất định, chúng ta phải sử dụng toán tử vị trí được lọc $ [] trong đó một trình giữ chỗ cho một giá trị đại diện cho một phần tử duy nhất của mảng. Sau đó, chúng ta phải sử dụng tham số thứ ba [tùy chọn] của phương thức UpdateMany để chỉ định một bộ ArrayFilters.use the filtered positional operator $[] where is a placeholder for a value that represents a single element of the array. We must then use the third parameter [options] of the updateMany method to specify a set of arrayFilters .

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. câu hỏi mảng nếu các lớp liên quan.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. questions array if the associated 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ế [].

Làm cách nào để đẩy một đối tượng mảng trong MongoDB?

Trong MongoDB, toán tử Push $ được sử dụng để nối một giá trị được chỉ định vào một mảng.Nếu trường được đề cập không có trong tài liệu để cập nhật, toán tử Push $ thêm nó dưới dạng trường mới và bao gồm giá trị được đề cập làm yếu tố của nó.Nếu trường cập nhật không phải là trường loại mảng thì hoạt động không thành công.the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.

Chủ Đề