Tập hợp mongodb xóa đối tượng khỏi mảng

Vì vậy, tôi đang sử dụng khung tổng hợp để tổng hợp một số kết quả. Câu hỏi của tôi là làm cách nào để tránh trả lại

// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
3 mỗi lần nếu có kết quả tổng bằng 0

Show

Tôi chỉ muốn hiển thị

// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
4 và loại bỏ
// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
3 khác

Tập hợp mongodb xóa đối tượng khỏi mảng

liên kết

- stackoverflow. com

ghi bàn. 1

câu trả lời được chấp nhận

Những gì bạn đang cố gắng làm là xóa một trường khỏi tài liệu trong đường dẫn dựa trên giá trị của trường. Tôi không tin rằng điều này có thể thực hiện được với khung tổng hợp (kể từ 2. 6). Tuy nhiên, có vẻ như đó không phải là vấn đề, vì việc bỏ qua kết quả

// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
6 ở bất cứ nơi nào bạn đang sử dụng chúng là chuyện nhỏ. Sẽ rất hữu ích khi xem một quy trình tổng hợp đầy đủ, bởi vì có thể có những sửa đổi trước đó trong quy trình sẽ ngăn chặn bất kỳ trường
// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
7 nào được tạo với giá trị 0, có thể nói là giải quyết vấn đề theo hướng khác

Với

// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
8, tôi nhận được chỉ mục là "johndoe". Vấn đề là. Tôi không thể sử dụng
// MongoDB version 5.0.8

> db.collection.insertMany([
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "123456789"},
                {"type" : "office", "number" : "987546321"},
                {"type" : "personal", "number" : "852147963"}
            ]
        },
        services: ["Google", "Instagram", "Twitter", "Facebook"],
        scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
    },
    {
        contact: {
            phone: [
                {"type" : "UAN", "number" : "456321879"},
                {"type" : "office", "number" : "874596321"},
                {"type" : "personal", "number" : "964785123"}
            ]
        },
        services: ["Google", "Instagram", "Twitter"],
        scores: [ 1, 8, 2, 3, 8, 7, 1 ]

    }
]);
9 vì điều này cần 2 hoặc ba đối số làm điểm bắt đầu hoặc làm điểm bắt đầu + số để giữ. Nhưng lấy mảng này

["johndoe", "janedoe", "jamesdoe"]

Bằng cách nào, với một đường dẫn tổng hợp, tôi có thể chuyển đổi mảng này thành **remove "janedoe" (chỉ mục. 1)`` từ mảng, vì vậy sau đường dẫn tổng hợp, tôi muốn cái này

Chúng tôi có thể áp dụng các cách tiếp cận khác nhau tùy thuộc vào yêu cầu của dự án. Hôm nay, chúng ta sẽ học cách sử dụng

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7,
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
8,
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
9,
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 và toán tử vị trí (
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1) để xóa phần tử khỏi mảng hoặc mảng tài liệu khi làm việc trong MongoDB

Chúng ta phải có một bộ sưu tập (các) tài liệu mẫu để thực hành tất cả các cách đã đề cập. Chúng tôi sẽ tạo một bộ sưu tập có tên là

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2 và chỉ chèn một tài liệu

Bạn cũng có thể sử dụng

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2 sau có chứa một tài liệu

Mã ví dụ

________số 8

Nếu bạn không có bộ sưu tập có tên

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2, đừng lo lắng; . Tiếp theo, bạn có thể chạy
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
5 để xem tài liệu đã chèn

Hãy nhớ rằng, bạn sẽ không thấy các tài liệu được cập nhật sau khi thực hiện các ví dụ mã sắp tới. Để làm được điều đó, bạn phải thực hiện lệnh

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
5

Sử dụng MongoDB // MongoDB version 5.0.8 > db.collection.update( { _id : ObjectId("62aae796f27219958a489b89") }, { $pull : { "contact.phone": { "number": "987546321" } } } ); 7 để xóa phần tử khỏi một mảng tài liệu

Mã ví dụ

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);

ĐẦU RA

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}

Ở đây, chúng tôi đang sử dụng toán tử

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7 để xóa tất cả các phiên bản của một giá trị/giá trị khỏi một mảng hiện có khớp với truy vấn bộ lọc hoặc điều kiện đã chỉ định

Trong ví dụ mã trên, chúng tôi đang tìm kiếm một tài liệu trong đó

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
9 bằng với
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
70. Sau khi tài liệu khớp với điều kiện đã chỉ định, chúng tôi xóa đối tượng khỏi một mảng hiện có trong đó
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
71 bằng với
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
72

Hãy nhớ những điểm sau khi sử dụng toán tử

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7

  • Nếu
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    74 đã cho cần xóa là một mảng, thì toán tử
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    7 chỉ xóa các phần tử khỏi một mảng thỏa mãn chính xác
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    74 đã cho (bao gồm cả thứ tự)
  • Nếu
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    74 đã cho cần xóa là một tài liệu, thì toán tử
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    7 chỉ xóa các phần tử khỏi một mảng chứa các trường & giá trị chính xác. Thứ tự của các trường có thể khác nhau ở đây
  • Nếu
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    79 đã cho và các phần tử của mảng là các tài liệu được nhúng, thì toán tử
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    7 sẽ áp dụng
    // MongoDB version 5.0.8
    
    > db.collection.update(
        { _id : ObjectId("62aae796f27219958a489b89") },
        { $pull : { "contact.phone": { "number": "987546321" } } }
    );
    
    79 như thể mọi phần tử mảng là một tài liệu trong một bộ sưu tập. Để biết thêm chi tiết về điều đó, bạn có thể kiểm tra điều này

Bây giờ, chúng tôi có một tình huống khác mà chúng tôi muốn thực hiện thao tác

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
92 trên tất cả các tài liệu trong bộ sưu tập. Để làm điều đó, chúng tôi sử dụng cú pháp sau sử dụng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7 trong khi
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
94 hoạt động trên tất cả các tài liệu của điều kiện đã chỉ định

Mã ví dụ

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7

ĐẦU RA

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
9

Để có được đầu ra ở trên, chúng tôi xóa phần tử mảng khỏi tất cả các tài liệu trong bộ sưu tập trong đó

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
95 bằng với
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
96

Nhiệm vụ tiếp theo của chúng ta là xóa các phần tử khỏi mảng

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
97 có giá trị lớn hơn hoặc bằng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
98 và nhỏ hơn hoặc bằng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
99 (
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
90 ). Thực hiện truy vấn sau để thực hiện điều đó cho tất cả các tài liệu của bộ sưu tập đã chọn

Mã ví dụ

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
9

ĐẦU RA

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1

Để thực hành với các giá trị kiểu chuỗi, chúng ta có thể làm theo cách sau

Mã ví dụ

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2

ĐẦU RA

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
3

Đối với ví dụ mã này, chúng tôi sử dụng toán tử so sánh

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
91 để có một mảng các giá trị và loại bỏ chúng khỏi mảng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
92 nếu có bất kỳ giá trị nào

Hơn nữa, chúng tôi xóa

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
93 khỏi mảng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
97 nếu nó ở đó trong khi chúng tôi đang sử dụng toán tử
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7 để thực hiện tất cả thao tác xóa này

Sử dụng MongoDB // MongoDB version 5.0.8 > db.collection.update( { _id : ObjectId("62aae796f27219958a489b89") }, { $pull : { "contact.phone": { "number": "987546321" } } } ); 8 để xóa phần tử khỏi một mảng

Mã ví dụ

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0

ĐẦU RA

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
0

Đối với mã này, chúng tôi đang sử dụng toán tử

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
8 để xóa tất cả các lần xuất hiện (trường hợp) của giá trị đã cho khỏi một mảng đã tồn tại. Chẳng hạn, nếu số
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
98 xuất hiện hai lần trong mảng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
97, thì cả hai giá trị
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
98 sẽ bị xóa

Giống như toán tử

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
7 loại bỏ các phần tử mảng bằng cách chỉ định truy vấn bộ lọc,
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
8 xóa/loại bỏ các phần tử mảng khớp với các giá trị được liệt kê

Chúng tôi sử dụng ký hiệu dấu chấm để xử lý các tài liệu nhúng của mảng

Sử dụng MongoDB // MongoDB version 5.0.8 > db.collection.update( { _id : ObjectId("62aae796f27219958a489b89") }, { $pull : { "contact.phone": { "number": "987546321" } } } ); 9 để xóa phần tử khỏi một mảng

Chúng ta có thể sử dụng toán tử

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
9 nếu muốn loại bỏ phần tử đầu tiên hoặc cuối cùng khỏi một mảng. Đối với điều này, chúng tôi chuyển
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
15 hoặc
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
93 cho toán tử
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
9 để xóa một phần tử mảng khỏi đầu tiên hoặc cuối cùng

Mã ví dụ

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
1

ĐẦU RA

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
2

Sử dụng MongoDB { "_id" : ObjectId("62aae796f27219958a489b89"), "contact" : { "phone" : [ { "type" : "UAN", "number" : "123456789" }, { "type" : "personal", "number" : "852147963" } ] }, "services" : [ "Google", "Instagram", "Twitter", "Facebook" ], "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ] } { "_id" : ObjectId("62aae796f27219958a489b8a"), "contact" : { "phone" : [ { "type" : "UAN", "number" : "456321879" }, { "type" : "office", "number" : "874596321" }, { "type" : "personal", "number" : "964785123" } ] }, "services" : [ "Google", "Instagram", "Twitter" ], "scores" : [ 1, 8, 2, 3, 8, 7, 1 ] } 0 để xóa trường chứa phần tử đã cho

Mã ví dụ

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
3

ĐẦU RA

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
4

Ở đây, chúng tôi sử dụng toán tử

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 để xóa một trường cụ thể chứa giá trị đã cho. Chẳng hạn, chúng tôi đang xóa trường
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
97 khớp với truy vấn bộ lọc và chứa
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
98 dưới dạng phần tử mảng

Hãy nhớ rằng khi chúng ta sử dụng toán tử

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 với toán tử vị trí (
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1), nó không loại bỏ phần tử phù hợp mà đặt nó thành
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
24. Xem phần sau để thực hành điều đó

Sử dụng Toán tử vị trí MongoDB để đặt phần tử thành { "_id" : ObjectId("62aae796f27219958a489b89"), "contact" : { "phone" : [ { "type" : "UAN", "number" : "123456789" }, { "type" : "personal", "number" : "852147963" } ] }, "services" : [ "Google", "Instagram", "Twitter", "Facebook" ], "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ] } { "_id" : ObjectId("62aae796f27219958a489b8a"), "contact" : { "phone" : [ { "type" : "UAN", "number" : "456321879" }, { "type" : "office", "number" : "874596321" }, { "type" : "personal", "number" : "964785123" } ] }, "services" : [ "Google", "Instagram", "Twitter" ], "scores" : [ 1, 8, 2, 3, 8, 7, 1 ] } 24 thay vì xóa

Mã ví dụ

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
5

ĐẦU RA

// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
6

Đoạn mã này sử dụng

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 với toán tử vị trí để đặt phần tử của mảng thành
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
24 thay vì loại bỏ nó. Chúng tôi sử dụng
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1 để cập nhật phần tử đầu tiên của mảng có giá trị là
{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
29 trong mảng
// MongoDB version 5.0.8

> db.collection.update(
    { _id : ObjectId("62aae796f27219958a489b89") },
    { $pull : { "contact.phone": { "number": "987546321" } } }
);
92

Cách làm này có thể sử dụng khi chúng ta không biết vị trí của một phần tử trong mảng. Mặt khác, chúng ta cũng có thể sử dụng

{
    "_id" : ObjectId("62aae796f27219958a489b89"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "123456789" },
            { "type" : "personal", "number" : "852147963" }
         ]
     },
     "services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
     "scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
    "_id" : ObjectId("62aae796f27219958a489b8a"),
    "contact" : {
        "phone" : [
            { "type" : "UAN", "number" : "456321879" },
            { "type" : "office", "number" : "874596321" },
            { "type" : "personal", "number" : "964785123" }
        ]
     },
     "services" : [ "Google", "Instagram", "Twitter" ],
     "scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
31 (tất cả các toán tử vị trí) nếu chúng ta muốn cập nhật tất cả các phần tử trong trường mảng đã cho

Làm cách nào để xóa một đối tượng khỏi một mảng trong MongoDB?

Nếu $pull chỉ xóa các phần tử trong mảng khớp với

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

Trình vỏ MongoDB cung cấp các phương thức sau để xóa tài liệu khỏi bộ sưu tập. .
Để xóa nhiều tài liệu, hãy sử dụng db. thu thập. xóaMany()
Để xóa một tài liệu, hãy sử dụng db. thu thập. xóaOne()

Làm cách nào để xóa mảng lồng nhau trong MongoDB?

MongoDB xóa một phần tử khỏi chỉ mục mảng .
Đọc tài liệu từ cơ sở dữ liệu
Cập nhật tài liệu và xóa mục trong mảng
Thay thế tài liệu trong cơ sở dữ liệu

$$ có nghĩa là gì trong MongoDB?

Trong biểu thức, ký hiệu đô la $ đánh giá thành đường dẫn trường; . e. cung cấp quyền truy cập vào trường . Ví dụ, biểu thức $eq $eq. [ "$price", "$1" ] thực hiện kiểm tra sự bằng nhau giữa giá trị trong trường có tên price và giá trị trong trường có tên 1 trong tài liệu.