Số lượng nhóm mongodb

Nhóm toán tử $ rất quan trọng trong MongoDB, vì nó giúp thực hiện các biến đổi dữ liệu khác nhau cho phép. Toán tử $ nhóm dữ liệu nhóm tương tự theo một số biểu thức cụ thể và nhóm tài liệu cho từng nhóm riêng biệt

Các bài viết liên quan

  • Unit testing trong Dart
  • Các gói phần mềm trong Pytorch
  • Gradient Descent trong PyTorch
  • nút mô-đun. js tiền điện tử
  • Bộ sưu tập Shell trong MongoDB

Giả sử có 50 sinh viên trong một cơ sở dữ liệu và tất cả họ đều yêu thích môn cricket. Nếu chúng ta muốn đếm tất cả các sinh viên yêu thích môn cricket, thì toán tử $ group là một giải pháp hữu ích cho một nhiệm vụ như vậy

cú pháp

{  
 $group:  
     {  
          _id: , // Group By Expression  
          : {  :  },  
           ..  
      }  
}  

Trọng điểm quan trọng

  • _Tôi. This field is started to group. Nếu bạn chỉ định giá trị của trường _id là null hoặc một số không đổi, thì toán tử $ group sẽ tính tổng các giá trị tích lũy cho tất cả các tài liệu đầu vào
  • Field: Đây là trường tùy chọn và được tính bằng các toán tử .

Ví dụ

Trong các ví dụ sau, chúng tôi đang làm việc với

  • Cơ sở dữ liệu. thử nghiệmDB
  • Bộ sưu tập. đơn hóa

Tài liệu. Mười tài liệu chứa các chi tiết của hóa đơn

{
        "_id" : A1,
        "item_name" : "Blue box",
        "price" : 10,
        "qty" : 15,
        "date_of_bill" : "13/04/2015"
}
{
        "_id" : A2,
        "item_name" : "Light Red box",
        "price" : 15,
        "qty" : 20,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : null,
        "item_name" : "Green box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "17/12/2014"
}
{
        "_id" : A3,
        "item_name" : "White box",
        "price" : 8,
        "qty" : 25,
        "date_of_bill" : "07/02/2014"
}
{
        "_id" : A4,
        "item_name" : "Blue box",
        "price" : 15,
        "qty" : 20,
        "date_of_bill" : "13/04/2015"
}
{
        "_id" : A5,
        "item_name" : "Red box",
        "price" : 12,
        "qty" : 10,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : A6,
        "item_name" : "Black box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "22/04/2020"
}
{
        "_id" : A7,
        "item_name" : "Red box",
        "price" : 8,
        "qty" : 15,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : A8,
        "item_name" : "Green box",
        "price" : 20,
        "qty" : 10,
        "date_of_bill" : "17/12/2014"
}
{
        "_id" : A9,
        "item_name" : "Green box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "17/12/2014"
}

Ví dụ 1. $ nhóm

Trong ví dụ này, chúng tôi sẽ nhóm theo các trường Ngày hóa đơn và Tên mặt hàng và hiển thị các trường này (Tổng giá, Số lượng trung bình và Đếm số lượng hóa đơn trong cùng một ngày) cho các chứng từ có ngày

Chuyển tài liệu sang giai đoạn tiếp theo có chứa số lượng tài liệu được nhập vào giai đoạn

Ghi chú

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
2 has a nguyên mẫu sau. có dạng nguyên mẫu như sau.

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
4 là tên của trường đầu tiên có số lượng là giá trị của nó.
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
4 phải là một chuỗi không trống, không được bắt đầu với

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
0 và không được chứa các ký tự_______21

Giai đoạn

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
2 tương đương với các trình tự

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
3 +______24 sau. Giai đoạn ______22 tương đương với chuỗi
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
3 +
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
4 sau đây.
{
        "_id" : A1,
        "item_name" : "Blue box",
        "price" : 10,
        "qty" : 15,
        "date_of_bill" : "13/04/2015"
}
{
        "_id" : A2,
        "item_name" : "Light Red box",
        "price" : 15,
        "qty" : 20,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : null,
        "item_name" : "Green box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "17/12/2014"
}
{
        "_id" : A3,
        "item_name" : "White box",
        "price" : 8,
        "qty" : 25,
        "date_of_bill" : "07/02/2014"
}
{
        "_id" : A4,
        "item_name" : "Blue box",
        "price" : 15,
        "qty" : 20,
        "date_of_bill" : "13/04/2015"
}
{
        "_id" : A5,
        "item_name" : "Red box",
        "price" : 12,
        "qty" : 10,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : A6,
        "item_name" : "Black box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "22/04/2020"
}
{
        "_id" : A7,
        "item_name" : "Red box",
        "price" : 8,
        "qty" : 15,
        "date_of_bill" : "05/12/2014"
}
{
        "_id" : A8,
        "item_name" : "Green box",
        "price" : 20,
        "qty" : 10,
        "date_of_bill" : "17/12/2014"
}
{
        "_id" : A9,
        "item_name" : "Green box",
        "price" : 10,
        "qty" : 30,
        "date_of_bill" : "17/12/2014"
}
3

in which

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
5 sẽ là trường đầu tiên chứa số lượng. You can just a other name for the first field

mẹo

Xem thêm

Một bộ sưu tập có tên

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
6 has the following document.
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }

Tổng hợp hoạt động sau đây có hai giai đoạn

  1. giai đoạn

    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    7 không bao gồm các tài liệu có giá trị
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    8 nhỏ hơn hoặc bằng
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    9 để chuyển các tài liệu với
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    8 lớn hơn
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    9 cho giai đoạn tiếp theo

  2. Giai đoạn

    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    2 trả về số lượng tài liệu còn lại trong đường ống tổng hợp và gán giá trị cho một trường gọi là

    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    53
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
5

Hoạt động trả lại kết quả sau

  • Tham khảo> >
  • Toán tử> >
  • Các giai đoạn đường ống tổng hợp> >
  • $count (tổng hợp)

Hành vi¶

giai đoạn

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
54 tương đương với trình tự
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
55 +
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
56 after.
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
0

in which

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
57 sẽ là trường đầu tiên chứa số lượng. You can just a other name for the first field

Thí dụ¶

Một bộ sưu tập có tên

{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
58 has the following document.
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
3

Tổng hợp hoạt động sau đây có hai giai đoạn

  1. Giai đoạn
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    59 không bao gồm các tài liệu có giá trị
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    00 nhỏ hơn hoặc bằng
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    01 để chuyển tài liệu với
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    00 lớn hơn
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    01 cho giai đoạn tiếp theo
  2. Giai đoạn
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    54 trả về số lượng tài liệu còn lại trong đường ống tổng hợp và gán giá trị cho một trường gọi là
    { "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
    05
{ "_id" : 1, "subject" : "History", "score" : 88 }{ "_id" : 2, "subject" : "History", "score" : 92 }{ "_id" : 3, "subject" : "History", "score" : 97 }{ "_id" : 4, "subject" : "History", "score" : 71 }{ "_id" : 5, "subject" : "History", "score" : 79 }{ "_id" : 6, "subject" : "History", "score" : 83 }
1

Hoạt động trả lại kết quả sau

Làm thế nào để bạn sử dụng bộ đếm trong tập hợp?

Trả về như một số lượng lớn các hàng trong mỗi nhóm nơi biểu thức không phải là null. Nếu truy vấn không có nhóm theo mệnh đề, trả về số lượng hàng bảng. Hàm tổng hợp đếm khác với hàm phân tích, trả về số trên một nhóm hàng trong cửa sổ. . Nếu truy vấn không có mệnh đề GROUP BY, COUNT trả về số hàng của bảng. Hàm tổng hợp COUNT khác với hàm phân tích COUNT, trả về số trên một nhóm hàng trong cửa sổ.

Làm cách nào để đếm tất cả các tài liệu trong tập hợp MongoDB?

Giai đoạn đếm $ trả về số lượng tài liệu còn lại trong đường ống tổng hợp và gán giá trị cho một trường gọi là pass_scores. $count stage trả về số lượng tài liệu còn lại trong quy trình tổng hợp và gán giá trị cho trường có tên là pass_scores.

Làm cách nào để đếm dữ liệu trong MongoDB?

Phương thức đếm () của MongoDB - db. Bộ sưu tập. Đếm () Phương thức đếm () Đếm số lượng tài liệu phù hợp với tiêu chí lựa chọn. db. Bộ sưu tập. count() Phương thức count() đếm số lượng tài liệu phù hợp với tiêu chí lựa chọn.

Làm cách nào để tính các nhóm trong MongoDB?

Chúng ta có thể sử dụng mã sau để nhóm theo trường 'Vị trí' và đếm số lần xuất hiện của từng vị trí