Để 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" } ] }
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ậtVí 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
6 trong tài liệu phù hợp với bộ lọc truy vấnBson 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[]];
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ấmPhầ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
6 chứa giá trị "18"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[]];
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