Hướng dẫn how do you update an array of objects in mongodb using node js? - làm thế nào để bạn cập nhật một mảng các đối tượng trong mongodb bằng cách sử dụng nút js?

Tài liệu về nhà → node.jsNode.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í:

    const query = { name: "Steve Lobsters", "items.type": "pizza" };
    const updateDocument = {
    $set: { "items.$.size": "extra large" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    2:
    const query = { name: "Steve Lobsters", "items.type": "pizza" };
    const updateDocument = {
    $set: { "items.$.size": "extra large" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    2

  • Tất cả các nhà điều hành 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:
    const query = { name: "Steve Lobsters", "items.type": "pizza" };
    const updateDocument = {
    $set: { "items.$.size": "extra large" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    3

  • 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:
    const query = { name: "Steve Lobsters", "items.type": "pizza" };
    const updateDocument = {
    $set: { "items.$.size": "extra large" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    4

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:

  • The positional operator did not find the match needed from the query.

    8: Nhà điều hành cập nhật

  • const query = { name: "Steve Lobsters", "items.type": "pizza" };
    const updateDocument = {
    $set: { "items.$.size": "extra large" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    6: Mảng trong tài liệu để cập nhật

  • const query = { "name": "Popeye" };
    const updateDocument = {
    $push: { "items.$[].toppings": "fresh mozzarella" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    0: Mã định danh cho toán tử vị trí được lọc

  • const 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ả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ập nhật

  • const query = { "name": "Popeye" };
    const updateDocument = {
    $push: { "items.$[].toppings": "fresh mozzarella" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    3: Giá trị để đẩy vào mảng
    const query = { "name": "Popeye" };
    const updateDocument = {
    $push: { "items.$[].toppings": "fresh mozzarella" }
    };
    const result = await pizza.updateOne(query, updateDocument);
    1

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);
0

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:

const query = { name: "Steve Lobsters", "items.type": "pizza" };
const updateDocument = {
$set: { "items.$.size": "extra large" }
};
const result = await pizza.updateOne(query, updateDocument);
1

Làm thế nào để bạn cập nhật một mảng các đối tượng trong nút JS MongoDB?

Để 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 $ []. Toán tử vị trí được lọc $ [] chỉ định các phần tử mảng phù hợp trong tài liệu cập nhật.use the filtered positional operator $[] . The filtered positional operator $[] specifies the matching array elements in the update document.

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

Tìm hiểu cách cập nhật các trường mảng trong các tài liệu trong các bộ sưu tập MongoDB.Bạn có thể sử dụng các phương thức updateOne () hoặc updatemany () để thêm, cập nhật hoặc xóa các phần tử mảng dựa trên các tiêu chí được chỉ định.Nên sử dụng phương thức Updatemany () để cập nhật nhiều mảng trong một bộ sưu tập.use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

Làm thế nào để bạn cập nhật dữ liệu trong MongoDB bằng Node JS Express JS?

Bạn có thể cập nhật một bản ghi hoặc tài liệu vì nó được gọi trong MongoDB, bằng cách sử dụng phương thức UpdateDe ().Tham số đầu tiên của phương thức updateDe () là đối tượng truy vấn xác định tài liệu nào sẽ cập nhật.Lưu ý: Nếu truy vấn tìm thấy nhiều hơn một bản ghi, chỉ có lần xuất hiện đầu tiên được cập nhật.by using the updateOne() method. The first parameter of the updateOne() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated.

Làm thế nào để bạn cập nhật một đối tượng 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ế ().