Hướng dẫn what is accumulator in mongodb? - bộ tích lũy trong mongodb là gì?

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

Mới trong phiên bản 5.0.

$count return số lượng tài liệu trong một nhóm.
Hướng dẫn what is accumulator in mongodb? - bộ tích lũy trong mongodb là gì?

Returns the number of documents in a group.

$count có sẵn trong các giai đoạn này: is available in these stages:

  • $bucket

  • $bucketAuto

  • $group

  • db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    0 (có sẵn bắt đầu từ MongoDB 5.0)

Ghi chú

Định hướng

$count Cú pháp: syntax:

$count không chấp nhận bất kỳ tham số nào. does not accept any parameters.

$count có chức năng tương đương với việc sử dụng

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
4 trong giai đoạn $group. is functionally equivalent to using
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
4 within the $group stage.

Mẹo

Xem thêm:

Tạo một bộ sưu tập

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
6 có chứa doanh số bán bánh ở các bang California (
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
7) và Washington (
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
8):

db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )

Bộ sưu tập

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
6 được sử dụng trong các ví dụ sau.

Ví dụ này sử dụng $count trong giai đoạn $group để đếm số lượng tài liệu trong bộ sưu tập

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
6 cho mỗi
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
3:$count in the $group stage to count the number of documents in the
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
6 collection for each
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
3:

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )

Trong ví dụ:

  • { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    4 Nhóm các tài liệu theo giá trị trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    3. Có các nhóm cho
    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    7 và
    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    8.

  • { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    8 đặt trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    9 thành số lượng tài liệu có cùng giá trị trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    3.

Trong đầu ra này, số lượng tài liệu cho

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
7 và
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
8 được hiển thị trong trường
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
9:

{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }

Ví dụ này sử dụng $count trong giai đoạn

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
0 để đếm số lượng tài liệu trong bộ sưu tập
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
6 cho mỗi
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
3 được xác định trong cửa sổ:$count in the
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
0 stage to count the number of documents in the
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
6 collection for each
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
3 defined in the window:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
countNumberOfDocumentsForState: {
$count: {},
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

Trong ví dụ:

  • { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    4 Nhóm các tài liệu theo giá trị trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    3. Có các nhóm cho
    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    7 và
    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    8.

  • { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    8 đặt trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    9 thành số lượng tài liệu có cùng giá trị trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    3.

  • Trong đầu ra này, số lượng tài liệu cho

    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    7 và
    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    8 được hiển thị trong trường
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    9:$count that is run in a documents window.

    Ví dụ này sử dụng $count trong giai đoạn

    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    0 để đếm số lượng tài liệu trong bộ sưu tập
    db.cakeSales.aggregate( [
    {
    $group: {
    _id: "$state",
    countNumberOfDocumentsForState: {
    $count: {}
    }
    }
    }
    ] )
    6 cho mỗi
    { "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
    { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
    3 được xác định trong cửa sổ:$count returns the number of documents between the beginning of the partition and the current document.

Trong đầu ra này, số lượng tài liệu cho

db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
7 và
db.cakeSales.aggregate( [
{
$group: {
_id: "$state",
countNumberOfDocumentsForState: {
$count: {}
}
}
}
] )
8 được hiển thị trong trường
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 }
{ "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
9:

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "countNumberOfDocumentsForState" : 1 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "countNumberOfDocumentsForState" : 2 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "countNumberOfDocumentsForState" : 3 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "countNumberOfDocumentsForState" : 1 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "countNumberOfDocumentsForState" : 2 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "countNumberOfDocumentsForState" : 3 }

Tổng hợp có nghĩa là gì trong MongoDB?

Tổng hợp trong MongoDB là gì? Tập hợp là một cách xử lý một số lượng lớn các tài liệu trong một bộ sưu tập bằng cách truyền chúng qua các giai đoạn khác nhau. Các giai đoạn tạo nên những gì được gọi là đường ống. Các giai đoạn trong đường ống có thể lọc, sắp xếp, nhóm, định hình lại và sửa đổi các tài liệu đi qua đường ống.a way of processing a large number of documents in a collection by means of passing them through different stages. The stages make up what is known as a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify documents that pass through the pipeline.

Nhóm $ trong MongoDB là gì?

Nhóm $ nhóm nhóm các tài liệu theo ngày và tính tổng số lượng bán, số lượng trung bình và tổng số lượng tài liệu trong mỗi nhóm. Giai đoạn thứ ba: Giai đoạn $ sắp xếp sắp xếp kết quả theo tổng số tiền bán cho mỗi nhóm theo thứ tự giảm dần.groups the documents by date and calculates the total sale amount, average quantity, and total count of the documents in each group. Third Stage: The $sort stage sorts the results by the total sale amount for each group in descending order.

Root $$ là gì?

$$ Root.Biến gốc $$ chứa các tài liệu nguồn cho nhóm.Nếu bạn muốn chuyển chúng qua không sửa đổi, bạn có thể thực hiện điều này bằng cách $ đẩy root $$ vào đầu ra từ nhóm.contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.

$$ trong MongoDB là gì?

Để truy cập giá trị của biến, tiền tố tên biến với các dấu hiệu đô la kép ($$);tức là "$$".Nếu biến tham chiếu một đối tượng, để truy cập một trường cụ thể trong đối tượng, hãy sử dụng ký hiệu dấu chấm;tức là "$$., prefix the variable name with double dollar signs ( $$ ); i.e. "$$" . If the variable references an object, to access a specific field in the object, use the dot notation; i.e. "$$.