Lĩnh vực nào là bắt buộc trong một hoạt động $group mongodb?

Tổng hợp là quá trình chọn dữ liệu từ một bộ sưu tập trong MongoDB. Nó xử lý nhiều tài liệu và trả về kết quả được tính toán

Sử dụng tính năng tổng hợp để nhóm các giá trị từ nhiều tài liệu hoặc thực hiện các thao tác trên dữ liệu được nhóm để trả về một kết quả duy nhất

Hoạt động tổng hợp có thể được thực hiện theo hai cách

  1. Sử dụng đường ống tổng hợp
  2. Sử dụng các phương pháp tổng hợp mục đích duy nhất. , và

đường ống tổng hợp

Đường dẫn tổng hợp là một mảng gồm một hoặc nhiều giai đoạn được truyền trong phương thức

db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
6 hoặc
db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
7

db.collection.aggregate([ {stage1}, {stage2}, {stage3}...])

Khung tổng hợp xử lý đường dẫn các giai đoạn trên dữ liệu thu thập và cung cấp cho bạn đầu ra ở dạng bạn cần

Mỗi giai đoạn nhận đầu ra của giai đoạn trước, xử lý thêm dữ liệu và gửi nó đến giai đoạn tiếp theo dưới dạng dữ liệu đầu vào. Đường dẫn tổng hợp thực thi trên máy chủ có thể tận dụng các chỉ mục. Xem danh sách các giai đoạn

Lĩnh vực nào là bắt buộc trong một hoạt động $group mongodb?

Hãy xem cách sử dụng các giai đoạn khác nhau trong bộ sưu tập

db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
8 sau đây

Dữ liệu mẫu

Sao chép

db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])

Giai đoạn $match

Giai đoạn

db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
9 thường là giai đoạn đầu tiên để chỉ chọn các tài liệu phù hợp từ một bộ sưu tập. Nó tương đương với phương thức Find(). Ví dụ sau minh họa một quy trình tổng hợp với một giai đoạn
db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
9 duy nhất

Ví dụ. Giai đoạn $match

Sao chép

db.employees.aggregate([ {$match:{ gender: 'female'}} ])

Trong ví dụ trên, giai đoạn

db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
9 trả về tất cả các nhân viên nam và chuyển nó sang giai đoạn tiếp theo
db.employees.aggregate([ 
    { $group:{ _id:'$department.name'} }
])
8. Biểu thức
db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
01 sắp xếp các tài liệu đầu vào theo trường
db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        gender:'male',
        email: "[email protected]",
        salary: 5000,
        department: { 
                    "name":"HR" 
                }
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        gender:'male',
        email: "[email protected]",
        salary: 8000,
        department: { 
                    "name":"Finance" 
                }
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        gender:'male',
        email: "[email protected]",
        salary: 7500,
        department: { 
                    "name":"Marketing" 
                }
    },
    { 
        _id:4,
        firstName: "Rosy",
        lastName: "Brown",
        gender:'female',
        email: "[email protected]",
        salary: 5000, 
        department: { 
                    "name":"HR" 
                }

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        gender:'male',
        email: "[email protected]",
        salary: 4500,
        department: { 
                    "name":"Finance" 
                }

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        gender:'male',
        email: "[email protected]",
        salary: 7000,
        department: { 
                    "name":"Marketing" 
                }
    }
])
02 theo thứ tự tăng dần. 1 biểu thị thứ tự tăng dần và -1 biểu thị thứ tự giảm dần

Sự khác biệt giữa $Group và $project trong MongoDB là gì?

$group được sử dụng để nhóm các tài liệu đầu vào theo biểu thức _id đã chỉ định và đối với mỗi nhóm riêng biệt, xuất ra một tài liệu. $project được sử dụng để chuyển các tài liệu có các trường được yêu cầu sang giai đoạn tiếp theo trong quy trình

Trường nào luôn được bao gồm theo mặc định trong phép chiếu trong MongoDB?

Trường _id luôn được bao gồm theo mặc định. Để loại trừ trường _id khỏi tài liệu đầu ra của giai đoạn $project, hãy chỉ định loại trừ trường _id bằng cách đặt nó thành 0 trong tài liệu trình chiếu.

$root trong MongoDB là gì?

Tài nguyên gốc là điểm bắt đầu cho API Trình quản lý đám mây . Từ đây, bạn có thể duyệt qua các liên kết để truy cập tất cả các tài nguyên API khác.

Tên toán tử tổng hợp cho khái niệm tham gia trong MongoDB là gì?

Toán tử $expr cho phép sử dụng các biểu thức tổng hợp bên trong cú pháp $match. Bắt đầu từ MongoDB 5. 0, các toán tử so sánh $eq , $lt , $lte , $gt và $gte được đặt trong toán tử $expr có thể sử dụng chỉ mục trên tập hợp from được tham chiếu trong giai đoạn tra cứu $.