Hướng dẫn can we use count with aggregate function in mongodb? - chúng ta có thể sử dụng count với hàm tổng hợp trong mongodb không?

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

$count thông qua một tài liệu đến giai đoạn tiếp theo có chứa số lượng tài liệu nhập vào giai đoạn.
Hướng dẫn can we use count with aggregate function in mongodb? - chúng ta có thể sử dụng count với hàm tổng hợp trong mongodb không?

Passes a document to the next stage that contains a count of the number of documents input to the stage.

Ghi chú

$count có mẫu nguyên mẫu sau: has the following prototype form:

là tên của trường đầu ra có số lượng là giá trị của nó. 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 ký 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 }
1.

Giai đoạn $count 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 }
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:$count stage is equivalent to the following
{ "_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 sequence:

db.collection.aggregate( [
{ $group: { _id: null, myCount: { $sum: 1 } } },
{ $project: { _id: 0 } }
] )

trong đó

{ "_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 ra chứa số lượng. Bạn có thể chỉ định một tên khác cho trường đầu ra.

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 có các tài liệu 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 }

Hoạt động tổng hợp 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 $count trả về số lượng các 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à

    db.scores.aggregate(
    [
    {
    $match: {
    score: {
    $gt: 80
    }
    }
    },
    {
    $count: "passing_scores"
    }
    ]
    )
    3.

db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)

Hoạt động trả về các kết quả sau:

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

Hành vi¶

Giai đoạn

db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)
4 tương đương với trình tự
db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)
5 +
db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)
6 sau:

db.collection.aggregate( [
   { $group: { _id: null, myCount: { $sum: 1 } } },
   { $project: { _id: 0 } }
] )

trong đó

db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)
7 sẽ là trường đầu ra chứa số lượng. Bạn có thể chỉ định một tên khác cho trường đầu ra.

Thí dụ¶

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

db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)
8 có các tài liệu 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 }

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

  1. Giai đoạn
    db.scores.aggregate(
    [
    {
    $match: {
    score: {
    $gt: 80
    }
    }
    },
    {
    $count: "passing_scores"
    }
    ]
    )
    9 không bao gồm các tài liệu có giá trị
    db.collection.aggregate( [
       { $group: { _id: null, myCount: { $sum: 1 } } },
       { $project: { _id: 0 } }
    ] )
    
    0 nhỏ hơn hoặc bằng
    db.collection.aggregate( [
       { $group: { _id: null, myCount: { $sum: 1 } } },
       { $project: { _id: 0 } }
    ] )
    
    1 để chuyển các tài liệu với
    db.collection.aggregate( [
       { $group: { _id: null, myCount: { $sum: 1 } } },
       { $project: { _id: 0 } }
    ] )
    
    0 lớn hơn
    db.collection.aggregate( [
       { $group: { _id: null, myCount: { $sum: 1 } } },
       { $project: { _id: 0 } }
    ] )
    
    1 cho giai đoạn tiếp theo.
  2. Giai đoạn
    db.scores.aggregate(
    [
    {
    $match: {
    score: {
    $gt: 80
    }
    }
    },
    {
    $count: "passing_scores"
    }
    ]
    )
    4 trả về số lượng các 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à
    db.collection.aggregate( [
       { $group: { _id: null, myCount: { $sum: 1 } } },
       { $project: { _id: 0 } }
    ] )
    
    5.

db.scores.aggregate(
  [
    {
      $match: {
        score: {
          $gt: 80
        }
      }
    },
    {
      $count: "passing_scores"
    }
  ]
)

Hoạt động trả về các kết quả sau:

Làm thế nào để bạn sử dụng đế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 đề, đếm 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 đếm, trả về số trên một nhóm các hàng trong cửa sổ. . If the query has no GROUP BY clause, COUNT returns the number of table rows. The COUNT aggregate function differs from the COUNT analytic function, which returns the number over a group of rows within a window.

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 các 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à passing_scores.$count stage returns a count of the remaining documents in the aggregation pipeline and assigns the value to a field called passing_scores .

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

Phương thức đếm () của MongoDB - db.Collection.Count () 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. Collection. count() The count() method counts the number of documents that match the selection criteria.

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 các lần xuất hiện của từng vị trí ...
Vị trí 'chuyển tiếp' xảy ra 1 lần ..
Vị trí 'bảo vệ' xảy ra 3 lần ..
Vị trí 'trung tâm' xảy ra 1 lần ..