Số lượng nhóm tổng hợp MongoDB

Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng MongoDB $count để trả về số lượng tài liệu trong một nhóm

Giới thiệu về MongoDB $count

MongoDB $count trả về số lượng tài liệu trong một nhóm. Đây là cú pháp của $count

{ $count: {} }

Code language: PHP (php)

Lưu ý rằng $count không chấp nhận bất kỳ tham số nào

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

{ $sum: 1 }

Code language: PHP (php)
2 sau đây trong giai đoạn

{ $sum: 1 }

Code language: PHP (php)
3

{ $sum: 1 }

Code language: PHP (php)

MongoDB $count ví dụ

Chúng tôi sẽ sử dụng bộ sưu tập

{ $sum: 1 }

Code language: PHP (php)
4 sau đây để chứng minh $count

db.sales.insertMany([ { "_id" : 1, "item" : "Americanos", "price" : 5, "size": "Short", "quantity" : 22, "date" : ISODate("2022-01-15T08:00:00Z") }, { "_id" : 2, "item" : "Cappuccino", "price" : 6, "size": "Short","quantity" : 12, "date" : ISODate("2022-01-16T09:00:00Z") }, { "_id" : 3, "item" : "Lattes", "price" : 15, "size": "Grande","quantity" : 25, "date" : ISODate("2022-01-16T09:05:00Z") }, { "_id" : 4, "item" : "Mochas", "price" : 25,"size": "Tall", "quantity" : 11, "date" : ISODate("2022-02-17T08:00:00Z") }, { "_id" : 5, "item" : "Americanos", "price" : 10, "size": "Grande","quantity" : 12, "date" : ISODate("2022-02-18T21:06:00Z") }, { "_id" : 6, "item" : "Cappuccino", "price" : 7, "size": "Tall","quantity" : 20, "date" : ISODate("2022-02-20T10:07:00Z") }, { "_id" : 7, "item" : "Lattes", "price" : 25,"size": "Tall", "quantity" : 30, "date" : ISODate("2022-02-21T10:08:00Z") }, { "_id" : 8, "item" : "Americanos", "price" : 10, "size": "Grande","quantity" : 21, "date" : ISODate("2022-02-22T14:09:00Z") }, { "_id" : 9, "item" : "Cappuccino", "price" : 10, "size": "Grande","quantity" : 17, "date" : ISODate("2022-02-23T14:09:00Z") }, { "_id" : 10, "item" : "Americanos", "price" : 8, "size": "Tall","quantity" : 15, "date" : ISODate("2022-02-25T14:09:00Z")} ]);

Code language: JavaScript (javascript)

1) Sử dụng MongoDB $count để đếm số lượng tài liệu cho mỗi nhóm ví dụ

Ví dụ sau sử dụng $count để tính số lượng tài liệu trên mỗi mục và trả về mục có số lượng lớn hơn hai

Tổng số và giai đoạn nhóm cùng nhau trả về số lượng tài liệu trong một nhóm. Chúng ta sẽ xem một ví dụ cho chủ đề này trong hướng dẫn này để bạn có thể hiểu rõ hơn về cách chọn số lượng và nhóm theo

Số lượng MongoDB & Cú pháp nhóm

Chúng ta hãy xem cú pháp giai đoạn tổng hợp đếm và nhóm trong MongoDB

db.drones.aggregate([
 {"$group":{fieldName, countFieldName:{$sum:1}}}])

Sử dụng Số lượng tổng hợp & Giai đoạn nhóm trong MongoDB

Hãy để chúng tôi bắt đầu với các ví dụ để trình bày trong hướng dẫn này về các giai đoạn tổng hợp đếm và nhóm. Dưới đây là bộ sưu tập tôi sẽ sử dụng

> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3ae"),
        "onSale" : true,
        "name" : "Caminara Frucha FCZ-89",
        "price" : 67800,
        "weight" : "35 kilograms",
        "additionalDetails" : {
                "material" : "glass fibre",
                "moreUses" : [ ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3af"),
        "onSale" : true,
        "name" : "Azehsra Chelske AZC-HYU56",
        "price" : 45600,
        "weight" : "32 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture"
                ]
        },
        "utility" : null
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3b0"),
        "onSale" : true,
        "name" : "Hazela Gunce QY999",
        "price" : 33900,
        "weight" : "20 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Videography",
                        "Wildlife Watching",
                        "Photography"
                ]
        },
        "utility" : [
                undefined
        ]
}

Đếm tài liệu bằng nhóm $ bằng cách truy vấn trên trường cấp cao nhất

Trong ví dụ này, tôi sẽ cung cấp 3 ví dụ khác nhau về đếm tài liệu của một bộ sưu tập và nhóm chúng dựa trên một số tiêu chí được cung cấp cho giai đoạn đường dẫn $group

ví dụ 1

Đếm tài liệu bằng cách nhóm chúng dựa trên giá của chúng

________số 8_______

ví dụ 2

Đếm tài liệu bằng cách nhóm chúng dựa trên tên của chúng

> db.drones.aggregate([
..  {"$group":{_id:"$name",counter:{$sum:1}}}]);
{ "_id" : "PlayGarra 2078-56", "counter" : 1 }
{ "_id" : "Sifinist Croma AX", "counter" : 1 }
{ "_id" : "Drovce Finnifield FR-7", "counter" : 1 }
{ "_id" : "Azehsra Chelske AZC-HYU56", "counter" : 1 }
{ "_id" : "Frinty Gemini 3X70", "counter" : 1 }
{ "_id" : "Rilche Gabbana Flier RG950", "counter" : 1 }
{ "_id" : "Caminara Frucha FCZ-89", "counter" : 1 }
{ "_id" : "X-Strimmer Eye", "counter" : 1 }
{ "_id" : "Nimbari Gryphon Medeta 65", "counter" : 1 }
{ "_id" : "Khai Balemosh Shefqa TRX", "counter" : 1 }
{ "_id" : "Hazela Gunce QY999", "counter" : 1 }

ví dụ 2

Đếm tài liệu bằng cách nhóm chúng dựa trên trường onSale để xem có bao nhiêu tài liệu sai và bao nhiêu tài liệu đúng

> db.drones.aggregate([
..  {"$group":{_id:"$onSale",counter:{$sum:1}}}])
{ "_id" : false, "counter" : 5 }
{ "_id" : true, "counter" : 6 }

Đếm tài liệu bằng nhóm $ bằng cách truy vấn trên trường lồng nhau

Trong ví dụ này, tôi sẽ cung cấp cho bạn một ví dụ về đếm tài liệu và nhóm chúng bằng cách thao tác trên một trường lồng nhau. Tôi sẽ sử dụng ký hiệu dấu chấm để truy cập tài liệu hoặc trường lồng nhau

Làm cách nào để đếm trong tập hợp MongoDB?

Truy vấn số lượng $ tổng hợp MongoDB . Ở đây, chuỗi là tên của trường đầu ra có số đếm là giá trị của nó. Và, chuỗi phải là một chuỗi không trống, không bắt đầu bằng '$' và không chứa '. It transfers a document to the next stage that contains a count of the number of documents input to the stage. Here, the string is the name of the output field which has the count as its value. And, the string must be a non-empty string, not start with '$' and not contain '.

Làm cách nào để sử dụng số lượng trong nhóm trong MongoDB?

Chúng ta có thể sử dụng đoạn mã sau để nhóm theo trường 'vị trí' và đếm số 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í 'Guard' xảy ra 3 lần
Vị trí 'Trung tâm' xuất hiện 1 lần

Chúng ta có thể sử dụng tính năng đếm với hàm tổng hợp trong MongoDB không?

MongoDB $count Aggregation . Có một số điều quan trọng cần lưu ý về cú pháp này. Đầu tiên, chúng tôi gọi toán tử $count và sau đó chỉ định chuỗi. The MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.

Tập hợp nhóm trong MongoDB là gì?

Các thao tác tổng hợp trong MongoDB xử lý các bản ghi/tài liệu dữ liệu và trả về các kết quả được tính toán. Tính năng tổng hợp thu thập các giá trị từ nhiều tài liệu khác nhau, nhóm chúng lại rồi thực hiện các thao tác khác nhau như Tổng, Trung bình, Tối thiểu, Tối đa, v.v. trên dữ liệu được nhóm đó để trả về kết quả được tính toán.