Hướng dẫn mongodb remove one element from array - mongodb xóa một phần tử khỏi mảng

Đưa ra tài liệu sau trong Bộ sưu tập Hồ sơ:

{_id: 1, phiếu bầu: [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 phiếu bầu lớn hơn hoặc bằng ($ gTE) 6:

db.profiles.update ({_id: 1}, {$ pull: {phiếu bầu: {$ 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, phiếu bầu: [3, 5]}

Nếu bạn có nhiều mục cùng một giá trị, bạn nên sử dụng $ Pullall thay vì $ Pull.

Trong câu hỏi có nhiều số liên lạc, cùng sử dụng điều này:

thu gom.update ({_id: id}, {$ pullall: {'contact.phone': {number: '+1786543589455'}}}); Nó sẽ xóa mọi mục phù hợp với số đó. trong điện thoại liên lạc

Hãy thử đọc hướng dẫn.

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

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 Trình điều khiển
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 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

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 operator removes from an existing array all instances of a value or values that match a specified condition.

Toán tử

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 có biểu mẫ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" ]
}
] )
7
operator has the form:

{ $pull: { : , : , ... } }

Để chỉ định

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
0 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

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
1 và các phần tử mảng là các tài liệu được nhúng, toán tử
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 áp dụng
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
1 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
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 được chỉ định với
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
5 để biết ví dụ.
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
operator applies the
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
1 as if each array element were a document in a collection. See Remove All Items That Match a 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" ]
}
] )
7 Condition With
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
5 for an example.

Nếu

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
6 được chỉ định để loại bỏ là một mả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 chỉ xóa các phần tử trong mảng phù hợp với chính xác
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
6 được chỉ định, bao gồm cả thứ tự.
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
removes only the elements in the array that match the specified
db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
6 exactly, including order.

Nếu

db.stores.updateMany(
{ },
{ $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } }
)
6 được chỉ định để xóa là một tài liệ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" ]
}
] )
7 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.
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
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,

{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
1 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như
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 với biểu thức toán hạng trống (
{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
3). 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).
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
with an empty operand expression (
{
_id: 1,
fruits: [ 'pears', 'grapes', 'bananas' ],
vegetables: [ 'celery', 'squash' ]
},
{
_id: 2,
fruits: [ 'plums', 'kiwis', 'bananas' ],
vegetables: [ 'broccoli', 'zucchini', 'onions' ]
}
3 ). 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

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

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ỏ

  • {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    5 và
    {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    6 từ mảng
    {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    7

  • {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    8 từ mảng
    {
    _id: 1,
    fruits: [ 'pears', 'grapes', 'bananas' ],
    vegetables: [ 'celery', 'squash' ]
    },
    {
    _id: 2,
    fruits: [ 'plums', 'kiwis', 'bananas' ],
    vegetables: [ 'broccoli', 'zucchini', 'onions' ]
    }
    9

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

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

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

0

{
_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.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

1:

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.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

2 lớn hơn hoặc bằng (

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

3)

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

4:

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.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

5 sau:

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

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

    6.

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

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

    2 lớn hơn hoặc bằng (

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

    3)

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

    4.

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

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

    2 nhỏ hơn hoặc bằng (

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

    1)

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

    2.

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.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

5, 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.profilesBulkWrite.find()

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

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

Tạo bộ sưu tập

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

4:

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

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

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

5 có chứa cả trường

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

6 bằng

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

7 và trường

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

8 bằng

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

9:

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" ]
}
] )
1

Biểu thứ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" ]
}
] )
7 áp dụng điều kiện cho từng phần tử của mảng

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

5 như thể nó là một tài liệu cấp cao nhất.
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
expression applies the condition to each element of the

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

5 array as though it were a top-level document.

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

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

5 không chứa tài liệu nào chứa cả trường

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

6 bằng

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

7 và trường

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

8 bằng

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

9.

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" ]
}
] )
2

Toán tử

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 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

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

8 để chỉ định các điều kiện khớ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" ]
}
] )
7
operator treats each element as a top-level object. The query is applied to each element. The expression does not need to use

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

8 to specify match conditions.

Ngược lại, hoạt động sau khô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 bất kỳ yếu tố nào từ bộ sưu tập gố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" ]
}
] )
7
any elements from the original collection:

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" ]
}
] )
3

Ghi chú

Thả bộ sưu tập

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

4 với:

Sau đó tạo lại nó để chạy ví dụ này.recreate it to run this example.

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

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

4 mới với các tài liệu được nhúng trong các mảng lồng nhau.

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" ]
}
] )
4

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

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);
}
2 với

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

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" ]
}
] )
5

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

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

5 trong mỗi tài liệu mà nó phù hợp.
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);
}
5 đã xóa các tài liệu khỏi

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

5 khi một phần tử của mảng
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);
}
2 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.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" ]
}
] )
6

Mẹo

Làm cách nào để loại bỏ một phần tử cụ thể khỏi một mảng?

Hàm pop (): Phương thức này được sử dụng để loại bỏ các phần tử từ cuối một mảng. Hàm shift (): Phương thức này được sử dụng để loại bỏ các phần tử từ đầu một mảng. Hàm splice (): Phương pháp này được sử dụng để loại bỏ các phần tử khỏi chỉ số cụ thể của một mảng.splice() function: This method is use to remove elements from the specific index of an array.

Làm cách nào để xóa một mục cụ thể trong MongoDB?

Bạn có thể xóa các 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 Remove (). Tham số đầu tiên của phương thức Remove () là một đối tượng truy vấn xác định tài liệu nào sẽ xóa. LƯU Ý: Phương thức Remove () được không dùng nữa. Sử dụng DeleteOne () hoặc DeletMany () thay thế.by using the remove() method. The first parameter of the remove() method is a query object defining which documents to delete. Note: The remove() method is deprecated. Use deleteOne() or deleteMany() instead.

Làm cách nào để loại bỏ nhiều phần tử khỏi một mảng trong MongoDB?

Toán tử $ Pullall: Toán tử $ Pullall sẽ loại bỏ tất cả các phần tử khỏi mảng được xác định.Bây giờ, hãy sử dụng truy vấn sau để xóa tất cả các giá trị của 8 khỏi mảng trong đó _ID là 1. Chúng ta có thể xác minh điều này bằng cách kiểm tra bộ sưu tập bằng phương thức Find () ...
$pull..
$pullAll..

Làm cách nào để bỏ một chỉ số cụ thể trong MongoDB?

Các trường lệnh để thả một chỉ mục duy nhất, chỉ định tên chỉ mục, tài liệu đặc tả chỉ mục (trừ khi chỉ mục là chỉ mục văn bản) hoặc một mảng của tên chỉ mục.Để bỏ chỉ mục văn bản, chỉ định tên chỉ mục thay vì tài liệu đặc tả chỉ mục.specify either the index name, the index specification document (unless the index is a text index), or an array of the index name. To drop a text index, specify the index names instead of the index specification document.