Tài liệu về nhà → node.js → Node.js
Trên trang này
- Tổng quan
- Tài liệu mẫu
- Chỉ định các yếu tố mảng
- Phần tử mảng phù hợp đầu tiên
- Phù hợp với tất cả các phần tử mảng
- Phù hợp với nhiều phần tử mảng
Trong hướng dẫn này, bạn có thể tìm hiểu cách sử dụng các toán tử cập nhật mảng sau để sửa đổi một mảng được nhúng trong tài liệu:
Nhà điều hành vị trí:
2:const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument]; Tất cả các nhà điều hành vị trí:
3:const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
3const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument]; Toán tử vị trí được lọc:
4:const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
4const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
Xem Hướng dẫn Máy chủ MongoDB trên các toán tử cập nhật để biết danh sách đầy đủ.Update Operators for a complete list.
Các ví dụ trong hướng dẫn này sử dụng các tài liệu mẫu sau từ bộ sưu tập
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
5. Bộ sưu tập chứa các tài liệu mô tả khách hàng và đơn đặt hàng pizza của họ là các phần tử mảng trong trường gọi là const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
6.[{ name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "beverage", name: "Water", size: "17oz", }, { type: "pizza", size: "large", toppings: ["pepperoni"], }, { type: "pizza", size: "medium", toppings: ["mushrooms", "sausage", "green peppers"], comment: "Extra green peppers please!", }, { type: "pizza", size: "large", toppings: ["pineapple, ham"], comment: "red pepper flakes on top", }, { type: "calzone", fillings: ["canadian bacon", "sausage", "onion"], }, { type: "beverage", name: "Diet Pepsi", size: "16oz", }, ], }, { name: "Popeye", address: "1 Sweethaven", items: [ { type: "pizza", size: "large", toppings: ["garlic, spinach"], }, { type: "calzone", toppings: ["ham"], }, ], }]
Bạn có thể chỉ định các phần tử mảng nào để cập nhật bằng toán tử vị trí. Các toán tử vị trí có thể chỉ định các phần tử đầu tiên, tất cả hoặc một số mảng 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 DOT. Ký hiệu DOT là một cú pháp truy cập thuộc tính để điều hướng các đối tượng BSON. Để tìm hiểu thêm, xem ký hiệu chấm.dot notation. Dot notation is a property access syntax for navigating BSON objects. To learn more, see dot notation.
Để cập nhật phần tử mảng đầu tiên của mỗi tài liệu phù hợp với truy vấn của bạn, hãy sử dụng toán tử vị trí
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2.Toán tử vị trí
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2 tham chiếu mảng phù hợp với truy vấn. Bạn không thể sử dụng toán tử này để tham khảo một mảng lồng nhau. Đối với các trường hợp bạn muốn truy cập một mảng lồng nhau, hãy sử dụng toán tử vị trí được lọc.filtered positional operator.Quan trọng
Không sử dụng toán tử
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2 trong cuộc gọi { name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
0 vì trình điều khiển coi const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2 làm tên trường trong tài liệu chèn.Đoạn mã sau đây cho thấy cách cập nhật kích thước của mục đặt hàng pizza đầu tiên lên "cực lớn" cho khách hàng tên là Steve tôm hùm.
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
Truy vấn khớp với tất cả các tài liệu có chứa một phần tử được nhúng trong mảng
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
6 có chứa giá trị { name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
3 trong trường { name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
4. { name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
5 chỉ định hoạt động cập nhật để đặt khớp phần tử mảng đầu tiên trong const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
6 thành "cực lớn".Sau khi bạn chạy phương thức cập nhật, tài liệu khách hàng của bạn cho Steve tôm hùm giống như sau:
{ name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
Lưu ý rằng chúng tôi đã bao gồm cả hai trường
{ name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
7 và { name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
8 trong truy vấn để khớp với mảng trong đó chúng tôi áp dụng toán tử const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2. Nếu chúng tôi bỏ qua trường { name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "extra large", ... }, ... ] }
8 từ truy vấn và chỉ định toán tử const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
2 trong bản cập nhật của chúng tôi, chúng tôi sẽ gặp phải lỗi sau:The positional operator did not find the match needed from the query.
Để thực hiện cập nhật trên tất cả các phần tử mảng của mỗi tài liệu phù hợp với truy vấn của bạn, hãy sử dụng tất cả các toán tử vị trí
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
3.Đoạn mã sau đây thêm "mozzarella mới" vào toppings của tất cả các mục đặt hàng của Popeye.
const query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
Sau khi bạn chạy phương thức cập nhật, tài liệu khách hàng của bạn cho Popeye giống như sau:
{ name:"Popeye", address: "1 Sweethaven", items: [ { type: "pizza", ... , toppings: ["garlic", "spinach", "fresh mozzarella"], }, { type: "calzone", ... , toppings: ["ham", "fresh mozzarella"], }, ] }
Để thực hiện bản cập nhật trên tất cả các phần tử mảng được nhúng của từng tài liệu phù hợp với truy vấn của bạn, hãy sử dụng toán tử vị trí được lọc
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
4.Toán tử vị trí được lọc
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
4 chỉ định các phần tử mảng phù hợp trong tài liệu cập nhật. Để xác định các phần tử mảng nào phù hợp, hãy ghép nối toán tử này với The positional operator did not find the match needed from the query.
5 trong một đối tượng The positional operator did not find the match needed from the query.
6.Thuật ngữ
The positional operator did not find the match needed from the query.
5 là một giá trị giữ chỗ mà bạn gán thể hiện một phần tử của tên trường mảng có tiền tố nó. Giá trị này phải bắt đầu bằng chữ thường và chỉ chứa các ký tự chữ và số.Để thêm topping "tỏi" vào một số mục đặt hàng nhất định, hãy định dạng tài liệu cập nhật của bạn như sau:
{ $push: { items.$[orderItem].toppings: "garlic" } }
Tài liệu cập nhật này chỉ định những điều sau:
8: Nhà điều hành cập nhậtThe positional operator did not find the match needed from the query.
6: Mảng trong tài liệu để cập nhậtconst query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
0: Mã định danh cho toán tử vị trí được lọcconst query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
1: Trường trên phần tử mảngconst query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
6 để cập nhậtconst query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
3: Giá trị để đẩy vào mảngconst query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
1const query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
Tiếp theo, thêm các tiêu chí phù hợp trong đối tượng
const query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
5 của bạn trong tham số const query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
6 của hoạt động cập nhật của bạn. Đối tượng này là một mảng các truy vấn chỉ định các thành phần mảng nào sẽ bao gồm trong bản cập nhật. Để thêm topping "tỏi" để đặt hàng các mục thuộc loại "pizza" và "kích thước lớn", hãy vượt qua const query = { "name": "Popeye" }; const updateDocument = { $push: { "items.$[].toppings": "fresh mozzarella" } }; const result = await pizza.updateOne[query, updateDocument];
5 sau đây:arrayFilters: [ { orderItem.type: "pizza" }, { orderItem.size: "large" } ]
Đoạn trích sau đây hiển thị phương thức cập nhật hoàn chỉnh:
const query = { name: "Steve Lobsters" }; const updateDocument = { $push: { "items.$[orderItem].toppings": "garlic" } }; const options = { arrayFilters: [{ "orderItem.type": "pizza", "orderItem.size": "large", }] }; const result = await pizza.updateMany[query, updateDocument, options];
Sau khi bạn chạy phương thức, tài liệu khách hàng của bạn cho Steve tôm hùm giống như sau:
{ name: "Steve Lobsters", address: "731 Yexington Avenue", items: [ { type: "pizza", size: "large", toppings: ["pepperoni", "garlic"] }, { type: "pizza", size: "large", toppings: ["pineapple", "ham", "garlic"], ...}, ... ] }
Giả sử tôm hùm Steve muốn điều chỉnh lệnh của họ để thêm "salami" như một topping cho tất cả các loại pizza có pepperoni. Để thực hiện cập nhật, hãy sử dụng toán tử vị trí được lọc như sau:
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
0Sau khi bạn chạy phương thức cập nhật, tài liệu khách hàng của bạn cho Steve tôm hùm giống như sau:
const query = { name: "Steve Lobsters", "items.type": "pizza" }; const updateDocument = { $set: { "items.$.size": "extra large" } }; const result = await pizza.updateOne[query, updateDocument];
1