Hướng dẫn can we use multiple group in mongodb? - chúng ta có thể sử dụng nhiều nhóm trong mongodb không?

Bạn có thể thực hiện điều này trong một truy vấn duy nhất, bạn chỉ cần sáng tạo một chút với việc thao túng các tài liệu và sau đó thực hiện hai giai đoạn, thêm vào ngày trước và sau đó bởi khách hàng.two $group stages, adding by date first and then by the customer.

Vì vậy, trước hết là cho các phiên bản MongoDB hiện tại 2.6 trở lên do một số nhà khai thác được sử dụng:

db.transactions.aggregate([

    // Project an additional array, stands for "order", "delivery"
    { "$project": {
        "_id": 0,
        "customer": 1,
        "order_date": 1,
        "delivery_date": 1,
        "qty": 1,
        "type": { "$literal": ["o","d"] }
    }},

    // Unwind that array, creates two documents by "type"
    { "$unwind": "$type" },

    // Group by "customer", "type" and date
    { "$group": {
        "_id": { 
            "customer": "$customer",
            "type": "$type",
            "year": { 
                "$year": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            },
            "month": { 
                "$month": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            }
        },
        "qty": { "$sum": "$qty" }
    }},

    // Group on the "customer" selecting which array to add to
    { "$group": {
        "_id": "$_id.customer",
        "orders": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "o" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        },
        "deliveries": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "d" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        }
    }},

    // Getting rid of the `false` values in there
    { "$project": {
        "orders": { "$setDifference": [ "$orders", [false] ] },
        "deliveries": { "$setDifference": [ "$deliveries", [false] ] },
    }},

    // But "sets" are not considered ordered, so sort them
    { "$unwind": "$orders" },
    { "$sort": { "orders.year": 1, "orders.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$push": "$orders" },
        "deliveries": { "$first": "$deliveries" }
    }},
    { "$unwind": "$deliveries" },
    { "$sort": { "deliveries.year": 1, "deliveries.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$first": "$orders" },
        "deliveries": { "$push": "$deliveries" }
    }}
)

Chỉ làm điều đó một chút khác nhau cho các phiên bản trước 2.6:

db.transactions.aggregate([

    // Project an additional array, stands for "order", "delivery"
    { "$project": {
        "_id": 0,
        "customer": 1,
        "order_date": 1,
        "delivery_date": 1,
        "qty": 1,
        "type": { "$cond": [ 1, ["o","d"], 0 ] }
    }},

    // Unwind that array, creates two documents by "type"
    { "$unwind": "$type" },

    // Group by "customer", "type" and date
    { "$group": {
        "_id": { 
            "customer": "$customer",
            "type": "$type",
            "year": { 
                "$year": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            },
            "month": { 
                "$month": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            }
        },
        "qty": { "$sum": "$qty" }
    }},

    // Group on the "customer" selecting which array to add to
    { "$group": {
        "_id": "$_id.customer",
        "orders": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "o" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        },
        "deliveries": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "d" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        }
    }},

    // Filter `false` and sort on date
    { "$unwind": "$orders" },
    { "$match": { "orders": { "$ne": false } } },
    { "$sort": { "orders.year": 1, "orders.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$push": "$orders" },
        "deliveries": { "$first": "$deliveries" }
    }},
    { "$unwind": "$deliveries" },
    { "$match": { "deliveries": { "$ne": false } } },
    { "$sort": { "deliveries.year": 1, "deliveries.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$first": "$orders" },
        "deliveries": { "$push": "$deliveries" }
    }}

])

Về cơ bản để tổng hợp cách tiếp cận ở đây, những gì bạn đang làm là sao chép mọi tài liệu và gán một "loại" đại diện cho "thứ tự" hoặc "giao hàng". Sau đó, khi bạn nhóm theo "Khách hàng" và "Ngày" và "Nhập" bạn quyết định có điều kiện "Ngày" chọn dựa trên loại hiện tại và chỉ cần tổng hợp "Qty" dưới phím đó.

Vì kết quả là một mảng của "đơn đặt hàng" và "giao hàng" cho mỗi khách hàng, sau đó bạn có điều kiện $push cho mảng đó hoặc các giá trị tài liệu hoặc false tùy thuộc vào "loại" hiện tại của tài liệu là gì cho mỗi mảng.$push to that array either the document values or false depending on what the current "type" of the document is to each array.

Cuối cùng, vì các mảng này hiện chứa các giá trị cho false cũng như các tài liệu cần thiết, bạn lọc ra các giá trị đó và đảm bảo các mảng của bạn nằm trong đúng thứ tự "ngày" nếu bạn thực sự yêu cầu điều đó.

Có, các danh sách có nhiều hơn hai giai đoạn $group, việc nâng vật nặng thực sự được thực hiện trong hai nhóm với các nhóm khác chỉ để thao tác mảng nếu bạn yêu cầu, nhưng nó cung cấp cho bạn kết quả chính xác và đặt hàng.$group stages, the heavy lifting is actually done in two groupings with the others just there for array manipulation if you require it, but it gives you exact and ordered results.

Vì vậy, đó có thể không phải là cách tiếp cận đầu tiên mà bạn có thể nghĩ đến, nhưng cho thấy một số ý tưởng chuyển đổi thú vị bạn có thể sử dụng với các nhà khai thác tổng hợp khác nhau để giải quyết vấn đề. Cái này làm điều này :)

Chúng ta có thể tham gia hai bộ sưu tập trong MongoDB không?

Có, bạn có thể tham gia 2 bộ sưu tập với Khung tổng hợp và $ Unionwith Giai đoạn. Dưới đây là các tài liệu có cú pháp và ví dụ, vì vậy bạn có thể kiểm tra cách thực hiện.. Here are the docs with syntax and examples, so you can check how to do it.

Chúng ta có thể làm nhóm trong MongoDB không?

Chúng tôi có thể nhóm theo đơn cũng như nhiều trường từ bộ sưu tập, chúng tôi có thể sử dụng nhà điều hành nhóm $ trong MongoDB để nhóm nhóm từ bộ sưu tập và trả về tài liệu mới. Chúng tôi đang sử dụng $ AVG, $ Sum, $ Max, $ Min, $ Push, $ cuối cùng, $ đầu tiên và $ addtoset Toán tử với nhóm trong MongoDB., we can use $group operator in MongoDB to group fields from the collection and returns the new document as result. We are using $avg, $sum, $max, $min, $push, $last, $first and $addToSet operator with group by in MongoDB.

Làm thế nào để nhóm làm việc trong MongoDB?

Giai đoạn nhóm $ phân tách các tài liệu thành các nhóm theo "khóa nhóm".Đầu ra là một tài liệu cho mỗi khóa nhóm duy nhất.Một khóa nhóm thường là một trường, hoặc nhóm các trường.Khóa nhóm cũng có thể là kết quả của một biểu thức.. The output is one document for each unique group key. A group key is often a field, or group of fields. The group key can also be the result of an expression.

Tích lũy trong MongoDB là gì?

Các bộ tích lũy là các nhà khai thác duy trì trạng thái của họ (ví dụ: tổng số, tối đa, tối thiểu và dữ liệu liên quan) khi các tài liệu tiến triển thông qua đường ống.Sử dụng toán tử tích lũy $ để thực hiện các chức năng JavaScript của riêng bạn để thực hiện hành vi không được hỗ trợ bởi ngôn ngữ truy vấn MongoDB.operators that maintain their state (e.g. totals, maximums, minimums, and related data) as documents progress through the pipeline. Use the $accumulator operator to execute your own JavaScript functions to implement behavior not supported by the MongoDB Query Language.