Hướng dẫn how do i remove multiple elements from an array in mongodb? - làm cách nào để xóa nhiều phần tử khỏi một mảng trong mongodb?

Xin chào, tôi đang cố gắng xóa nhiều đối tượng khỏi một mảng trông như thế này.

{
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b3"),
"owner" : "1",
"group_name" : "PAASCU Board",
"group_members" : [ 
    {
        "faculty_name" : "Cheska Dela Rosa",
        "faculty_number" : 2,
        "_id" : ObjectId("5a7da1bda21d5f3e8cf005b5")
    }, 
    {
        "faculty_name" : "Earl Sempio",
        "faculty_number" : 7323,
        "_id" : ObjectId("5a7da1bda21d5f3e8cf005b4")
    }, 
    {
        "faculty_number" : 203,
        "faculty_name" : "Sample",
        "_id" : ObjectId("5a7dbf7952bd150a94d83958")
    }, 
    {
        "faculty_number" : 8025,
        "faculty_name" : "Sample Postman",
        "_id" : ObjectId("5a7dc64a1cf5dd3d50167d53")
    }
],
"__v" : 0 }

Nó hoạt động khi tôi xóa một đối tượng bằng cách sử dụng $ kéo bằng mã này.

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})

Nhưng điều gì sẽ xảy ra nếu tôi muốn loại bỏ nhiều đối tượng với

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
9 khác nhau? Tôi đã thử sử dụng phương thức

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

0 giống như cách tôi thêm nhiều đối tượng vào mảng nhưng nó không hoạt động tốt.

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

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 Trình vận hành

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 Xóa khỏi một mảng hiện có tất cả các trường hợp của một giá trị hoặc giá trị phù hợp với một điều kiện được chỉ định.

The

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 operator removes from an existing array all instances of a value or values that match a specified condition.

Toán tử

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 có biểu mẫu:

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
operator has the form:

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

Để chỉ định

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

4 trong tài liệu nhúng hoặc trong một mảng, hãy sử dụng ký hiệu DOT.

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.

Nếu bạn chỉ định

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

5 và các phần tử mảng là các tài liệu được nhúng, toán tử

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 áp dụng

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

5 như thể mỗi phần tử mảng là một tài liệu trong một bộ sưu tập. Xem Xóa tất cả các mục khớp với điều kiện

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 được chỉ định với

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

9 để biết ví dụ.

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
operator applies the

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

5 as if each array element were a document in a collection. See Remove All Items That Match a Specified

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 Condition With

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

9 for an example.

Nếu

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
0 được chỉ định để loại bỏ là một mảng,

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 chỉ xóa các phần tử trong mảng phù hợp với chính xác
db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
0 được chỉ định, bao gồm cả thứ tự.

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
removes only the elements in the array that match the specified
db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
0 exactly, including order.

Nếu

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
0 được chỉ định để xóa là một tài liệu,

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 chỉ xóa các phần tử trong mảng có cùng trường và giá trị chính xác. Thứ tự của các lĩnh vực có thể khác nhau.

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
removes only the elements in the array that have the exact same fields and values. The ordering of the fields can differ.

Bắt đầu từ MongoDB 5.0,

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
5 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 với biểu thức toán hạng trống (
db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
7). Một bản cập nhật trống dẫn đến không có thay đổi và không có mục nhập oplog được tạo (có nghĩa là hoạt động là không có op).

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
with an empty operand expression (
db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
7 ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).

Tạo bộ sưu tập

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )
8:

db.stores.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
] )

Hoạt động sau đây sẽ loại bỏ

  • db.stores.insertMany( [
    {
    _id: 1,
    fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
    vegetables: [ "carrots", "celery", "squash", "carrots" ]
    },
    {
    _id: 2,
    fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
    vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
    }
    ] )
    9 và
    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    0 từ mảng
    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    1

  • db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    2 từ mảng
    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    3

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)

Xác nhận kết quả với

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
4

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}

Tạo bộ sưu tập

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
5:

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

Hoạt động sau đây sẽ xóa tất cả các mục khỏi mảng

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
6 lớn hơn hoặc bằng (
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
7)
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
8:

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

Sau khi hoạt động cập nhật, tài liệu chỉ có các giá trị nhỏ hơn 6:

{ _id: 1, votes: [ 3, 5 ] }

Hoạt động

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
9 sau:

  • Tạo bộ sưu tập

    {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    0.

  • Loại bỏ tất cả các mục khỏi mảng

    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    6 lớn hơn hoặc bằng (
    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    7)
    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    8.

  • Loại bỏ tất cả các mục khỏi mảng

    db.stores.updateMany(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
    )
    6 nhỏ hơn hoặc bằng (
    {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    5)
    {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    6.

try {
db.profilesBulkWrite.bulkWrite( [
{
insertOne: {
"document": { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }
}
},
{
updateOne: {
"filter": { _id: 1 },
"update": { $pull: { votes: { $gte: 6 } } }
}
},
{
updateOne: {
"filter": {_id: 1},
"update": { $pull: { votes: { $lte: 3 } } }
}
}
] );
} catch (e) {
print(e);
}

Ghi chú

bulkWrite()

Sau khi hoạt động

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
9, bạn có thể xác nhận tài liệu chỉ có giá trị dưới 6 và lớn hơn 3 bằng cách sử dụng thao tác sau:

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
0

Hoạt động trả về như sau:

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
1

Tạo bộ sưu tập

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
8:

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
2

Hoạt động sau đây sẽ loại bỏ tất cả các phần tử khỏi mảng

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
9 có chứa cả trường

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

0 bằng

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

1 và trường

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

2 bằng

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

3:

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
3

Biểu thức

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 áp dụng điều kiện cho từng phần tử của mảng
{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
9 như thể nó là một tài liệu cấp cao nhất.

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
expression applies the condition to each element of the
{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
9 array as though it were a top-level document.

Sau khi hoạt động, mảng

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
9 không chứa tài liệu nào chứa cả trường

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

0 bằng

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

1 và trường

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

2 bằng

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

3.

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
4

Toán tử

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 coi từng phần tử là một đối tượng cấp cao nhất. Truy vấn được áp dụng cho từng yếu tố. Biểu thức không cần sử dụng

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

2 để chỉ định các điều kiện khớp.

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
operator treats each element as a top-level object. The query is applied to each element. The expression does not need to use

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

2 to specify match conditions.

Ngược lại, thao tác sau không

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1 bất kỳ yếu tố nào từ bộ sưu tập gốc:

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

1
any elements from the original collection:

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
5

Ghi chú

Sau khi hoạt động

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
9, bạn có thể xác nhận tài liệu chỉ có giá trị dưới 6 và lớn hơn 3 bằng cách sử dụng thao tác sau:

Hoạt động trả về như sau:recreate it to run this example.

Tạo bộ sưu tập

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
8:

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
6

Sau đó, bạn có thể chỉ định nhiều điều kiện trên các phần tử của mảng

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

6 với

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

2

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
7

Hoạt động đã cập nhật mảng

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
9 trong mỗi tài liệu mà nó phù hợp.

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

9 đã xóa các tài liệu khỏi
{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
9 khi một phần tử của mảng

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

6 nhúng phù hợp với các điều kiện lựa chọn trong dòng được tô sáng.

db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
8

Mẹo

Làm cách nào để loại bỏ tất cả các đối tượng khỏi một mảng trong MongoDB?

Toán tử Pull $ sẽ loại bỏ khỏi một mảng hiện có tất cả các phiên bản của giá trị hoặc giá trị khớp với một điều kiện được chỉ định. Toán tử Pull $ có biểu mẫu: {$ pull: {: ,,, ...}} để chỉ định một trong một tài liệu nhúng hoặc trong một mảng, sử dụng ký hiệu dấu chấm. removes from an existing array all instances of a value or values that match a specified condition. The $pull operator has the form: { $pull: { : , : , ... } } To specify a in an embedded document or in an array, use dot notation.

Làm cách nào để xóa nhiều đối tượng trong MongoDB?

Shell MongoDB cung cấp các phương pháp sau để xóa tài liệu khỏi bộ sưu tập:..
Để xóa nhiều tài liệu, sử dụng DB.thu thập.Deletemany ().
Để xóa một tài liệu duy nhất, sử dụng DB.thu thập.Xóa ().

Làm cách nào để chỉnh sửa 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.

$ Push làm gì trong MongoDB?

Nếu trường không có trong tài liệu để cập nhật, $ Push sẽ thêm trường mảng với giá trị làm phần tử của nó.Nếu trường không phải là một mảng, hoạt động sẽ thất bại.Nếu giá trị là một mảng, $ Push sẽ cộng đồng toàn bộ mảng là một phần tử duy nhất.Để thêm từng phần tử của giá trị một cách riêng biệt, hãy sử dụng $ mỗi công cụ sửa đổi với $ Push.adds the array field with the value as its element. If the field is not an array, the operation will fail. If the value is an array, $push appends the whole array as a single element. To add each element of the value separately, use the $each modifier with $push .