Hướng dẫn can we use group inside project mongodb? - chúng ta có thể sử dụng nhóm bên trong dự án mongodb không?

Tôi đang cố gắng để có được số từ khóa cùng với

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
7,
{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
8 và
{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
9. DB của tôi là

Show
{
    "_id" : ObjectId("5673f5b1e4b0822f6f0a5b89"),
    "keyword" : "electronic content management system",
    "llcId" : "CL1K9B",
    "categoryId" : "CL1K8V",
    "parentId" : "CL1K8V",

}

Tôi đã thử

{
$group:
{
_id: , // Group key
: { : },
...
}
}
0 với
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])

Và nó cho tôi một kết quả như

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }

Nhưng tôi cũng cần dữ liệu thực tế trong kết quả, ví dụ:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
9,
{
$group:
{
_id: , // Group key
: { : },
...
}
}
3,
{
$group:
{
_id: , // Group key
: { : },
...
}
}
4,
{
$group:
{
_id: , // Group key
: { : },
...
}
}
5. Tôi đã cố gắng hiển thị
{
$group:
{
_id: , // Group key
: { : },
...
}
}
6 và từ khóa bằng cách sử dụng
{
$group:
{
_id: , // Group key
: { : },
...
}
}
0 nhưng nó chỉ hiển thị
{
$group:
{
_id: , // Group key
: { : },
...
}
}
8 và tổng số. Những gì tôi đang thiếu?

Giai đoạn đầu tiên:MongoDB Manual

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 sử dụng biến hệ thống
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
49 để nhóm toàn bộ tài liệu của các tác giả. Giai đoạn này chuyển các tài liệu sau đây cho giai đoạn tiếp theo:

The

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 stage separates documents into groups according to a "group key". The output is one document for each unique group key.

Giai đoạn thứ hai:usage examples.

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
50 thêm một trường vào đầu ra chứa tổng số bản sao của sách cho mỗi tác giả.

Ghi chúaccumulator expressions.

Hoạt động tổng hợp sau đây xoay vòng dữ liệu trong bộ sưu tập db.keyword.aggregate([ { $group: { _id: "$llcId", total: {$sum: 1}, } }, { $project: { categoryId: 1, total: 1 } } ]) 45 để có các tiêu đề được nhóm bởi các tác giả.

Hoạt động trả về các tài liệu sau: does not order its output documents.

Các tài liệu hoạt động tổng hợp sau đây bởi

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
47:
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage has the following prototype form:

{
$group:
{
_id: , // Group key
: { : },
...
}
}

Giai đoạn đầu tiên:

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 sử dụng biến hệ thống
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
49 để nhóm toàn bộ tài liệu của các tác giả. Giai đoạn này chuyển các tài liệu sau đây cho giai đoạn tiếp theo:

{
$group:
{
_id: , // Group key
: { : },
...
}
}
8

Giai đoạn thứ hai:See the Group by Null example.

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
1

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
50 thêm một trường vào đầu ra chứa tổng số bản sao của sách cho mỗi tác giả.accumulator operators.

Ghi chúaccumulator operators can accept any valid

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
3. For more information on expressions, see Expressions.

Toán tử

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
4 phải là một trong các toán tử tích lũy sau:

Đã thay đổi trong phiên bản 5.0.

Tên

Sự mô tả

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
5

Trả về kết quả của chức năng tích lũy do người dùng xác định.

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
6

Trả về một mảng các giá trị biểu thức duy nhất cho mỗi nhóm. Thứ tự của các phần tử mảng không được xác định.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
8

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
0

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

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

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 and
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7 stages.

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
3

Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

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

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 and
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7 stages.

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
7

Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

Trả về một tập hợp của các trường

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
4 dưới cùng trong một nhóm, theo thứ tự sắp xếp được chỉ định.

Trả về số lượng tài liệu trong một nhóm.

{ "_id" : null, "count" : 8 }

1

Khác biệt với giai đoạn đường ống

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
7.

Mới trong phiên bản 5.0: Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

{ "_id" : null, "count" : 8 }

4

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

{ "_id" : null, "count" : 8 }

9

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

Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

SELECT COUNT(*) AS count FROM sales

2

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

SELECT COUNT(*) AS count FROM sales

7

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

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

SELECT COUNT(*) AS count FROM sales

9

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

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

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1,
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7 and as an expression.

db.sales.aggregate( [ { $group : { _id : "$item" } } ] )

4

Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.sales.aggregate( [ { $group : { _id : "$item" } } ] )

5

Trả về một tập hợp của các trường

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
4 dưới cùng trong một nhóm, theo thứ tự sắp xếp được chỉ định.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.sales.aggregate( [ { $group : { _id : "$item" } } ] )

7

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.sales.aggregate( [ { $group : { _id : "$item" } } ] )

9

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
01

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
03

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
05

Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

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

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 and
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7 stages.

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
08

Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.

Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định.

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

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 and
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7 stages.

Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit,
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
returns an error. To allow more space for stage processing, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.

Trả về một tập hợp của các trường db.sales.aggregate( [ { $group: { _id: null, count: { $count: { } } } }] )4 dưới cùng trong một nhóm, theo thứ tự sắp xếp được chỉ định.

Trả về số lượng tài liệu trong một nhóm.

Khác biệt với giai đoạn đường ống

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
7.
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
. There are optimizations that you can make manually and optimizations MongoDB makes internally.

Mới trong phiên bản 5.0: Có sẵn trong các giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 và
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
7.
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
16
by the same field and the
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage only uses the

{ "_id" : null, "count" : 8 }

1 accumulator operator, consider adding an index on the grouped field which matches the sort order. In some cases, the
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage can use the index to quickly find the first document of each group.

Trả về một giá trị từ tài liệu đầu tiên cho mỗi nhóm. Đặt hàng chỉ được xác định nếu các tài liệu được sắp xếp.

Khác biệt với toán tử mảng

{ "_id" : null, "count" : 8 }

1.

db.foo.aggregate([
{
$sort:{ x : 1, y : 1 }
},
{
$group: {
_id: { x : "$x" },
y: { $first : "$y" }
}
}
])

Trả về một tập hợp của các yếu tố

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )
4 đầu tiên trong một nhóm. Chỉ có ý nghĩa khi các tài liệu theo thứ tự xác định. Khác biệt với toán tử mảng

{ "_id" : null, "count" : 8 }

4.
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stages if either:

  • Mới trong phiên bản 5.2: Có sẵn trong các giai đoạn

    {
    $group:
    {
    _id: , // Group key
    : { : },
    ...
    }
    }
    1, biểu thức và
    db.sales.insertMany([
    { "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
    { "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
    { "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
    { "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
    { "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
    { "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
    { "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
    { "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
    ])
    7.

  • Trả về một giá trị từ tài liệu cuối cùng cho mỗi nhóm. Đặt hàng chỉ được xác định nếu các tài liệu được sắp xếp.

Khác biệt với toán tử mảng

{ "_id" : null, "count" : 8 }

9.

Trong

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
25, hãy tạo một bộ sưu tập mẫu có tên
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26 với các tài liệu sau:
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
25
, create a sample collection named
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26 with the following documents:

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])

Hoạt động tổng hợp sau đây sử dụng giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 để đếm số lượng tài liệu trong bộ sưu tập
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26:
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage to count the number of documents in the
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26 collection:

db.sales.aggregate( [
{
$group: {
_id: null,
count: { $count: { } }
}
}
] )

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

{ "_id" : null, "count" : 8 }

Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:

SELECT COUNT(*) AS count FROM sales

Mẹo

Hoạt động tổng hợp sau đây sử dụng giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26:
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage to retrieve the distinct item values from the
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26 collection:

db.sales.aggregate( [ { $group : { _id : "$item" } } ] )

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

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
0

Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
1

Mẹo
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage groups the documents by
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31 to retrieve the distinct item values. This stage returns the
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
34 for each item.Second Stage:The
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
35 stage filters the resulting documents to only return items with a
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
34 greater than or equal to 100.

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

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
2

Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
3

Mẹo

Hoạt động tổng hợp sau đây sử dụng giai đoạn { $group: { _id: , // Group key : { : }, ... } }1 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập db.keyword.aggregate([ { $group: { _id: "$llcId", total: {$sum: 1}, } }, { $project: { categoryId: 1, total: 1 } } ]) 26:

Trong

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
25, hãy tạo một bộ sưu tập mẫu có tên
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26 với các tài liệu sau:
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
25
, create a sample collection named
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26 with the following documents:

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])

Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31, tính toán tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
5

Giai đoạn đầu tiên: Giai đoạn
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 nhóm các tài liệu bằng
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31 để truy xuất các giá trị vật phẩm riêng biệt. Giai đoạn này trả về
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
34 cho mỗi mục. Giai đoạn thứ hai: Giai đoạn
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
35 lọc các tài liệu kết quả chỉ trả về các mục với
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
34 lớn hơn hoặc bằng 100.
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
stage 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
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
41 stage sorts the results by the total sale amount for each group in descending order.

Xem thêm:

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
6

Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
7

Mẹo

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
8

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

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
9

Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
0

Mẹo

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
25, create a sample collection named
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
45 with the following documents:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
1

Hoạt động tổng hợp sau đây sử dụng giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
26:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
2

Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31, tính toán tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
3

Giai đoạn đầu tiên: Giai đoạn

{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 nhóm các tài liệu bằng
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31 để truy xuất các giá trị vật phẩm riêng biệt. Giai đoạn này trả về
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
34 cho mỗi mục. Giai đoạn thứ hai: Giai đoạn
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
35 lọc các tài liệu kết quả chỉ trả về các mục với
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
34 lớn hơn hoặc bằng 100.

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
4

Xem thêm:

Đường ống sau đây tính toán tổng số tiền bán hàng, số lượng bán hàng trung bình và số lượng bán hàng cho mỗi ngày trong năm 2014: uses the

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
49 system variable to group the entire documents by authors. This stage passes the following documents to the next stage:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
5

Giai đoạn đầu tiên: Giai đoạn
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
35 lọc các tài liệu chỉ chuyển các tài liệu từ năm 2014 sang giai đoạn tiếp theo. Giai đoạn thứ hai: Giai đoạn
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1 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ố tài liệu trong các tài liệu trong Mỗi nhóm. Giai đoạn thứ hai: Giai đoạn
db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
41 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.

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

Hoạt động tổng hợp sau đây chỉ định một nhóm { $group: { _id: , // Group key : { : }, ... } }8 của db.keyword.aggregate([ { $group: { _id: "$llcId", total: {$sum: 1}, } }, { $project: { categoryId: 1, total: 1 } } ]) 43, tính toán tổng số lượng bán, số lượng trung bình và số lượng của tất cả các tài liệu trong bộ sưu tập.

Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31, tính toán tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:

{ "_id" : "CL1KJQ", "total" : 17 }
{ "_id" : "CL1KKW", "total" : 30 }
6

Mẹo

Hoạt động tổng hợp sau đây sử dụng giai đoạn { $group: { _id: , // Group key : { : }, ... } }1 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập db.keyword.aggregate([ { $group: { _id: "$llcId", total: {$sum: 1}, } }, { $project: { categoryId: 1, total: 1 } } ]) 26:

Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường

db.keyword.aggregate([
    {
        $group: {
            _id: "$llcId",
            total: {$sum: 1},
        }
    },
    {
        $project: {
            categoryId: 1, total: 1
        }
    }
])
31, tính toán tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:
{
$group:
{
_id: , // Group key
: { : },
...
}
}
1
operator in a common use case.

Làm thế nào sử dụng Tập đoàn MongoDB và Dự án?

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

Chúng ta có thể sử dụng nhóm trong MongoDB không?

Nhóm MongoDB được sử dụng để nhóm dữ liệu từ bộ sưu tập, chúng ta có thể đạt được điều khoản theo nhóm bằng cách sử dụng phương pháp chức năng tổng hợp và phương pháp nhóm trong MongoDB. Mặc dù sử dụng chức năng tổng hợp với các hoạt động truy vấn của nhóm theo điều khoản nhanh hơn như truy vấn bình thường, về cơ bản chức năng tổng hợp được sử dụng trong nhiều điều kiện.we can achieve group by clause using aggregate function and group method in MongoDB. While using aggregate function with group by clause query operations is faster as normal query, basically aggregate function is used in multiple condition.

Chúng ta có thể sử dụng nhiều nhóm trong MongoDB không?

Nhóm MongoDB bởi nhiều trường sử dụng hoạt động tổng hợp trước tiên, khóa dựa trên nhóm được chọn và sau đó bộ sưu tập được chia thành các nhóm theo giá trị khóa đã chọn.Sau đó, bạn có thể tạo một tài liệu cuối cùng bằng cách tổng hợp các tài liệu trong mỗi nhóm. First, the key on which the grouping is based is selected and then the collection is divided into groups according to the selected key value. You can then create a final document by aggregating the documents in each group.

Chúng ta có thể sử dụng $ trong dự án MongoDB không?

Để trả lời câu hỏi của bạn: Có, nhưng không như bạn mong đợi.Có thể sử dụng toán tử $ trong tại các giai đoạn $ Project hoặc $ mat của một truy vấn tổng hợp, nhưng việc sử dụng và mục đích không hoàn toàn giống nhau trong mỗi.Có hai loại cực kỳ khác nhau của toán tử "giống nhau" trong toán tử (tạo ra sự nhầm lẫn ngữ nghĩa):yes, but not as you would expect. It is possible to use the $in operator at the $project or $match stages of an aggregation query, but the usage and the purpose aren't quite the same in each. There are two extremely different types of the "same" $in operator (making a semantic confusion):