Mảng lồng nhau của dự án MongoDB

Hôm nay, chúng ta sẽ tìm hiểu cách sử dụng các giai đoạn tổng hợp

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
4 và
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
5, vòng lặp
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
6 và phương pháp
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
7 để chiếu các trường lồng nhau trong khi truy vấn dữ liệu trong MongoDB

Show

Trong MongoDB, chúng tôi có thể truy xuất tất cả các tài liệu bằng phương pháp

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
8, nhưng nếu chúng tôi chỉ muốn truy cập vào các trường lồng nhau cụ thể thì sao?. Đây là nơi chúng tôi sử dụng phép chiếu

Chúng ta có thể chiếu các trường lồng nhau theo nhiều cách khác nhau. Sau đây, chúng ta sẽ tìm hiểu về các giải pháp sau để chiếu các trường lồng nhau

  1. Sử dụng giai đoạn tổng hợp
    // MongoDB version 5.0.8
    
    > var current_location = "posting_locations";
    > var project = {};
    > project["id"] = "$"+current_location+".city_id";
    > project["name"] = "$"+current_location+".city_name";
    > project["regions"] = 1;
    
    > var find = {};
    > find[current_location] = {"$exists":true};
    
    > db.nested.aggregate([
        { $match : find },
        { $project : project }
    ]).pretty()
    
    4
  2. Sử dụng giai đoạn tổng hợp
    // MongoDB version 5.0.8
    
    > var current_location = "posting_locations";
    > var project = {};
    > project["id"] = "$"+current_location+".city_id";
    > project["name"] = "$"+current_location+".city_name";
    > project["regions"] = 1;
    
    > var find = {};
    > find[current_location] = {"$exists":true};
    
    > db.nested.aggregate([
        { $match : find },
        { $project : project }
    ]).pretty()
    
    5
  3. Sử dụng vòng lặp
    // MongoDB version 5.0.8
    
    > var current_location = "posting_locations";
    > var project = {};
    > project["id"] = "$"+current_location+".city_id";
    > project["name"] = "$"+current_location+".city_name";
    > project["regions"] = 1;
    
    > var find = {};
    > find[current_location] = {"$exists":true};
    
    > db.nested.aggregate([
        { $match : find },
        { $project : project }
    ]).pretty()
    
    6
  4. Sử dụng hàm
    // MongoDB version 5.0.8
    
    > var current_location = "posting_locations";
    > var project = {};
    > project["id"] = "$"+current_location+".city_id";
    > project["name"] = "$"+current_location+".city_name";
    > project["regions"] = 1;
    
    > var find = {};
    > find[current_location] = {"$exists":true};
    
    > db.nested.aggregate([
        { $match : find },
        { $project : project }
    ]).pretty()
    
    7

Để tìm hiểu các cách tiếp cận trên, hãy tạo một bộ sưu tập có tên

{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
3 chứa một tài liệu. Bạn cũng có thể sử dụng truy vấn dưới đây để liên hệ với chúng tôi

Mã ví dụ

// MongoDB version 5.0.8

> db.nested.insertOne(
    {
        "name": {
            "first_name": "Mehvish",
            "last_name": "Ashiq",
         },
         "contact": {
            "phone":{"type": "manager", "number": "123456"},
            "email":{ "type": "office", "mail": "[email protected]"}
         },
         "country_name" : "Australien",
         "posting_locations" : [
             {
                 "city_id" : 19398,
                 "city_name" : "Bondi Beach (Sydney)"
             },
             {
                  "city_id" : 31101,
                  "city_name" : "Rushcutters Bay (Sydney)"
             },
             {
                  "city_id" : 31022,
                  "city_name" : "Wolly Creek (Sydney)"
             }
          ],
          "regions" : {
              "region_id" : 796,
              "region_name" : "Australien: New South Wales (Sydney)"
          }
    }
);

Sử dụng

{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
4 trên vỏ mongo để xem dữ liệu đã chèn

Sử dụng Giai đoạn tổng hợp // MongoDB version 5.0.8 > var current_location = "posting_locations"; > var project = {}; > project["id"] = "$"+current_location+".city_id"; > project["name"] = "$"+current_location+".city_name"; > project["regions"] = 1; > var find = {}; > find[current_location] = {"$exists":true}; > db.nested.aggregate([ { $match : find }, { $project : project } ]).pretty() 4 để chiếu các trường lồng nhau trong MongoDB

Mã ví dụ

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()

ĐẦU RA

{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}

Ở đây, chúng tôi lưu trường cấp một có tên là

{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
6 trong một biến có tên là
{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
7

Sau đó, chúng ta sử dụng biến đó để truy cập vào

{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
8 và
{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
9 và lưu chúng vào đối tượng
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
0 đồng thời sử dụng ký hiệu ngoặc để tạo thuộc tính cho đối tượng
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
0. Ngoài ra, chúng tôi lưu trường
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
2 trong trường
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
3

Tiếp theo, chúng ta có một đối tượng khác có tên là

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
4 mà chúng ta sẽ sử dụng trong phương thức
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
5 để khớp với các tài liệu. Trong phương pháp
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
5, chúng tôi sử dụng giai đoạn
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();
7 để khớp các tài liệu và
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
4 để chiếu các trường, cho dù được lồng vào nhau hay ở cấp độ đầu tiên

Chúng tôi sử dụng

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
4 để chỉ định trường nào chúng tôi muốn hiển thị trong đầu ra. Chúng tôi có thể sử dụng giải pháp sau nếu chúng tôi chỉ quan tâm đến việc chiếu các trường lồng nhau được chỉ định mà không có bất kỳ truy vấn bộ lọc nào

Mã ví dụ

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();

ĐẦU RA

{
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "city_id" : [
                19398,
                31101,
                31022
        ],
        "city_name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}

Sử dụng // MongoDB version 5.0.8 > var current_location = "posting_locations"; > var project = {}; > project["id"] = "$"+current_location+".city_id"; > project["name"] = "$"+current_location+".city_name"; > project["regions"] = 1; > var find = {}; > find[current_location] = {"$exists":true}; > db.nested.aggregate([ { $match : find }, { $project : project } ]).pretty() 5 Giai đoạn tổng hợp để nhận các trường lồng nhau không bao gồm các trường được chỉ định trong MongoDB

Mã ví dụ

// MongoDB version 5.0.8

> db.nested.aggregate({
        $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
}).pretty()

ĐẦU RA

{
        "country_name" : "Australien",
        "posting_locations" : [
                {
                        "city_name" : "Bondi Beach (Sydney)"
                },
                {
                        "city_name": "Rushcutters Bay (Sydney)"
                },
                {
                        "city_name": "Wolly Creek (Sydney)"
                }
        ]
}

Ở đây, chúng tôi sử dụng toán tử, dùng để xóa trường hoặc mảng trường đã chỉ định

Hãy nhớ rằng chúng tôi sử dụng ký hiệu dấu chấm để chỉ định tài liệu được nhúng hoặc mảng tài liệu. Toán tử

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
5 không hoạt động nếu trường đã cho không tồn tại

Khi chúng ta sử dụng

{
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "city_id" : [
                19398,
                31101,
                31022
        ],
        "city_name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
3 để so khớp các phần tử của một mảng, toán tử
// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
5 sẽ thay thế các phần tử phù hợp bằng
{
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "city_id" : [
                19398,
                31101,
                31022
        ],
        "city_name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
5 thay vì loại bỏ chúng khỏi mảng. Hành vi này hỗ trợ giữ vị trí phần tử và kích thước mảng nhất quán

Sử dụng Vòng lặp // MongoDB version 5.0.8 > var current_location = "posting_locations"; > var project = {}; > project["id"] = "$"+current_location+".city_id"; > project["name"] = "$"+current_location+".city_name"; > project["regions"] = 1; > var find = {}; > find[current_location] = {"$exists":true}; > db.nested.aggregate([ { $match : find }, { $project : project } ]).pretty() 6 để lấy các trường lồng nhau trong MongoDB

Mã ví dụ

// MongoDB version 5.0.8

> var bulk = db.newcollection.initializeUnorderedBulkOp(),
   counter = 0;

> db.nested.find().forEach(function(doc) {
    var document = {};
    document["name"] = doc.name.first_name + " " + doc.name.last_name;
    document["phone"] = doc.contact.phone.number;
    document["mail"] = doc.contact.email.mail;
    bulk.insert(document);
    counter++;
    if (counter % 1000 == 0) {
        bulk.execute();
        bulk = db.newcollection.initializeUnorderedBulkOp();
    }
});

> if (counter % 1000 != 0) { bulk.execute(); }

Bạn sẽ thấy một cái gì đó tương tự như sau

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 1,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

Tiếp theo, thực hiện lệnh bên dưới trên vỏ mongo của bạn để xem các trường được chiếu

// MongoDB version 5.0.8

> db.newcollection.find().pretty();

ĐẦU RA

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
0

Để tìm hiểu mã ví dụ này, giả sử chúng ta muốn lấy một số trường lồng nhau nhất định và chèn chúng vào một bộ sưu tập mới. Tại đây, việc chèn các trường đã chuyển đổi dưới dạng tài liệu vào bộ sưu tập mới có thể ảnh hưởng đến hoạt động của chúng tôi dựa trên kích thước của bộ sưu tập lồng nhau

Chúng tôi có thể tránh hiệu suất chèn chậm này bằng cách sử dụng API không có thứ tự mới. Nó sẽ hợp lý hóa các thao tác chèn bằng cách gửi hàng loạt và cung cấp cho chúng tôi phản hồi trong thời gian thực về việc thao tác thành công hay thất bại

Vì vậy, chúng tôi đang sử dụng API

{
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "city_id" : [
                19398,
                31101,
                31022
        ],
        "city_name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
7 để chèn cấu trúc dữ liệu mong muốn vào bộ sưu tập
{
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "city_id" : [
                19398,
                31101,
                31022
        ],
        "city_name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
9, nơi các tài liệu hoàn toàn mới sẽ được tạo bằng vòng lặp ________0____6 của con trỏ bộ sưu tập
{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
3. Để tạo thuộc tính mới, chúng tôi sử dụng ký hiệu ngoặc

Đối với mã này, chúng tôi giả sử có một lượng lớn dữ liệu. Vì vậy, chúng tôi sẽ gửi các thao tác đến một máy chủ theo đợt của

// MongoDB version 5.0.8

> db.nested.aggregate({
        $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
}).pretty()
2 để thực hiện thao tác chèn hàng loạt

Do đó, nó mang lại cho chúng tôi hiệu suất tốt vì chúng tôi không gửi từng yêu cầu mà chỉ gửi một lần cho mỗi 1000 yêu cầu tới máy chủ

Sử dụng Phương pháp // MongoDB version 5.0.8 > var current_location = "posting_locations"; > var project = {}; > project["id"] = "$"+current_location+".city_id"; > project["name"] = "$"+current_location+".city_name"; > project["regions"] = 1; > var find = {}; > find[current_location] = {"$exists":true}; > db.nested.aggregate([ { $match : find }, { $project : project } ]).pretty() 7 để chiếu các trường lồng nhau trong MongoDB

Mã ví dụ

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
1

Bây giờ, hãy chạy truy vấn sau để xem đầu ra

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
2

ĐẦU RA

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
3

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

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()
7 để thực hiện thu nhỏ bản đồ trên tất cả các tài liệu của bộ sưu tập
{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}
3. Để làm được điều đó, chúng ta phải tuân theo quy trình ba bước được giải thích ngắn gọn bên dưới

  • Xác định hàm
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    6 để xử lý mọi tài liệu đầu vào. Trong hàm này, từ khóa
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    7 đề cập đến tài liệu hiện tại đang được xử lý bằng thao tác thu nhỏ bản đồ và hàm
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    8 ánh xạ các giá trị đã cho vào các khóa và trả về chúng
  • Ở đây, chúng tôi định nghĩa hàm
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    9 tương ứng, là nơi thực tế diễn ra quá trình tổng hợp dữ liệu. Phải mất hai đối số (
    {
            "country_name" : "Australien",
            "posting_locations" : [
                    {
                            "city_name" : "Bondi Beach (Sydney)"
                    },
                    {
                            "city_name": "Rushcutters Bay (Sydney)"
                    },
                    {
                            "city_name": "Wolly Creek (Sydney)"
                    }
            ]
    }
    
    0 và
    {
            "country_name" : "Australien",
            "posting_locations" : [
                    {
                            "city_name" : "Bondi Beach (Sydney)"
                    },
                    {
                            "city_name": "Rushcutters Bay (Sydney)"
                    },
                    {
                            "city_name": "Wolly Creek (Sydney)"
                    }
            ]
    }
    
    1);

    Hãy nhớ rằng các phần tử của

    {
            "country_name" : "Australien",
            "posting_locations" : [
                    {
                            "city_name" : "Bondi Beach (Sydney)"
                    },
                    {
                            "city_name": "Rushcutters Bay (Sydney)"
                    },
                    {
                            "city_name": "Wolly Creek (Sydney)"
                    }
            ]
    }
    
    3 được trả về bởi hàm
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    8 từ phương thức
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    6. Ở bước này, hàm
    // MongoDB version 5.0.8
    
    > db.nested.aggregate({
            $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
    }).pretty()
    
    9 rút gọn mảng
    {
            "country_name" : "Australien",
            "posting_locations" : [
                    {
                            "city_name" : "Bondi Beach (Sydney)"
                    },
                    {
                            "city_name": "Rushcutters Bay (Sydney)"
                    },
                    {
                            "city_name": "Wolly Creek (Sydney)"
                    }
            ]
    }
    
    3 thành tổng các giá trị (phần tử) của nó