Làm cách nào để cập nhật toàn bộ mảng trong mongodb?

Để cập nhật nhiều phần tử, hãy sử dụng $[]. $[] là toán tử tất cả vị trí chỉ ra rằng toán tử cập nhật sẽ sửa đổi tất cả các phần tử trong trường mảng đã chỉ định

Trước tiên chúng ta hãy tạo một bộ sưu tập với các tài liệu -

> db.demo385.insertOne({"ServerLogs": [
..       {
..          "status":"InActive"
..       },
..       {
..          "status":"InActive"
..       },
..       {
..          "status":"InActive"
..       }
..    ]
.. }
.. );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e5b6a7522064be7ab44e7f5")
}

Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find() -

> db.demo385.find().pretty();

Điều này sẽ tạo ra đầu ra sau -

{
   "_id" : ObjectId("5e5b6a7522064be7ab44e7f5"),
   "ServerLogs" : [
      {
      "status" : "InActive"
      },
      {
         "status" : "InActive"
      },
      {
         "status" : "InActive"
      }
   ]
}

Sau đây là truy vấn để cập nhật nhiều phần tử trong một mảng trong MongoDB -

> db.demo385.update(
..    { "_id" : ObjectId("5e5b6a7522064be7ab44e7f5") },
..    { "$set": { "ServerLogs.$[].status": "Active" }}
.. )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find() -

> db.demo385.find().pretty();

Điều này sẽ tạo ra đầu ra sau -

{
   "_id" : ObjectId("5e5b6a7522064be7ab44e7f5"),
   "ServerLogs" : [
      {
         "status" : "Active"
      },
      {
         "status" : "Active"
      },
      {
         "status" : "Active"
      }
   ]
}

Làm cách nào để cập nhật toàn bộ mảng trong mongodb?

Cập nhật ngày 02-04-2020 13. 47. 05

Để chỉ định một bản cập nhật, hãy sử dụng trình tạo

Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
3. Trình tạo
Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
3 cung cấp các phương thức tiện ích tĩnh để xây dựng thông số kỹ thuật cập nhật. Để biết thêm thông tin về cách sử dụng trình tạo
Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
3 với mảng, hãy xem hướng dẫn của chúng tôi về trình tạo Cập nhật

Ví dụ sau thực hiện các hành động này

  • Truy vấn tài liệu mẫu

  • Nối "17" vào mảng

    Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
    6 trong tài liệu phù hợp với bộ lọc truy vấn

Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());

Ví dụ trước cập nhật tài liệu gốc sang trạng thái sau

> db.demo385.find().pretty();
1

Chỉ định các phần tử mảng

Bạn có thể chỉ định phần tử mảng nào sẽ cập nhật bằng toán tử vị trí. Các toán tử vị trí có thể chỉ định phần tử mảng đầu tiên, tất cả hoặc một số phần tử nhất định để cập nhật

Để chỉ định các phần tử trong một mảng với các toán tử vị trí, hãy sử dụng ký hiệu dấu chấm. Ký hiệu dấu chấm là một cú pháp truy cập thuộc tính để điều hướng các đối tượng BSON

Để biết thêm thông tin, hãy xem Nhập thủ công máy chủ trên

ký hiệu dấu chấm

Phần tử mảng phù hợp đầu tiên

Để cập nhật phần tử mảng đầu tiên phù hợp với bộ lọc truy vấn của bạn, hãy sử dụng toán tử

Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
7 vị trí. Trường mảng phải xuất hiện như một phần của bộ lọc truy vấn của bạn để sử dụng toán tử
Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
7 vị trí

Thí dụ

Ví dụ sau thực hiện các hành động này

  • Truy vấn tài liệu có trường

    Bson filter = Filters.eq("_id", 1);Bson update = Updates.push("qty", 17);FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()                                    .returnDocument(ReturnDocument.AFTER);Document result = collection.findOneAndUpdate(filter, update, options);System.out.println(result.toJson());
    6 chứa giá trị "18"

  • Giảm giá trị mảng đầu tiên trong tài liệu khớp với bộ lọc truy vấn bằng "3"

> db.demo385.find().pretty();
5

Ví dụ trước cập nhật tài liệu gốc sang trạng thái sau

> db.demo385.find().pretty();
6

Để biết thêm thông tin về các phương thức và toán tử được đề cập trong phần này, hãy xem các tài nguyên sau

Mảng có thể được cập nhật?

Thay vào đó, mỗi khi bạn muốn cập nhật một mảng, bạn sẽ muốn chuyển một mảng mới vào hàm thiết lập trạng thái của mình . Để làm điều đó, bạn có thể tạo một mảng mới từ mảng ban đầu ở trạng thái của mình bằng cách gọi các phương thức không thay đổi của nó như filter() và map(). Sau đó, bạn có thể đặt trạng thái của mình thành mảng mới kết quả.

$[] trong MongoDB là gì?

Định nghĩa. $[] Toán tử tất cả 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 đã chỉ định . Toán tử $[] có dạng sau. {