Grup dengan menghitung di mongodb

Penghitungan agregat dan penyisihan grup bersama-sama mengembalikan jumlah dokumen dalam grup. Kami akan melihat contoh untuk topik ini dalam panduan ini sehingga Anda bisa mendapatkan ide yang lebih baik tentang cara memilih jumlah dan pengelompokan

Sintaks Jumlah & Grup MongoDB

Mari kita lihat sintaks tahap penghitungan dan agregat grup di MongoDB

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

Menggunakan Hitungan Agregat & Tahapan Grup di MongoDB

Mari kita mulai dengan contoh-contoh yang dibahas dalam panduan ini untuk tahap penghitungan dan agregat grup. Di bawah ini adalah koleksi yang akan saya gunakan

> 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
        ]
}
_

Menghitung Dokumen Menggunakan $group dengan Melakukan Kueri di Top-Level Field

Dalam contoh ini, saya akan memberikan 3 contoh berbeda untuk menghitung dokumen koleksi dan mengelompokkannya berdasarkan beberapa kriteria yang diberikan ke tahap $group pipeline

Contoh 1

Menghitung dokumen dengan mengelompokkannya berdasarkan harganya

> db.drones.aggregate([
..  {"$group":{_id:"$price",counter:{$sum:1}}}])
{ "_id" : 33900, "counter" : 1 }
{ "_id" : 99500, "counter" : 1 }
{ "_id" : 120500, "counter" : 1 }
{ "_id" : 15000, "counter" : 1 }
{ "_id" : 55000, "counter" : 1 }
{ "_id" : 77500, "counter" : 1 }
{ "_id" : 23500, "counter" : 1 }
{ "_id" : 67800, "counter" : 1 }
{ "_id" : 110000, "counter" : 1 }
{ "_id" : 45600, "counter" : 1 }
{ "_id" : 87600, "counter" : 1 }

Contoh 2

Menghitung dokumen dengan mengelompokkannya berdasarkan namanya

> 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 }

Contoh 2

Menghitung dokumen dengan mengelompokkannya berdasarkan field onSale untuk melihat berapa yang salah dan berapa yang benar

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

Menghitung Dokumen Menggunakan $group dengan Melakukan Query di Nested Field

Dalam contoh ini, saya akan memberi Anda contoh menghitung dokumen dan mengelompokkannya dengan mengoperasikan bidang bersarang. Saya akan menggunakan notasi titik untuk mengakses dokumen atau bidang bersarang

Bagaimana cara menggunakan hitungan dalam grup di MongoDB?

Kita dapat menggunakan kode berikut untuk mengelompokkan berdasarkan bidang 'posisi' dan menghitung kemunculan setiap posisi. .
Posisi 'Maju' terjadi 1 kali
Posisi 'Guard' terjadi 3 kali
Posisi 'Center' terjadi 1 kali

Bagaimana cara menggunakan hitungan di MongoDB?

Metode count() menghitung jumlah dokumen yang cocok dengan kriteria pemilihan . Ini mengembalikan jumlah dokumen yang cocok dengan kriteria seleksi. Dibutuhkan dua argumen, yang pertama adalah kriteria pemilihan dan yang lainnya opsional. Metode ini setara dengan db.

Bagaimana cara menghitung dalam agregasi MongoDB?

Kueri $hitung gabungan MongoDB . Di sini, string adalah nama bidang keluaran yang memiliki hitungan sebagai nilainya. Dan, string harus berupa string yang tidak kosong, tidak dimulai dengan '$' dan tidak mengandung '. 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 '.

Bagaimana cara menulis kueri penghitungan di MongoDB?

koleksi. count() — Panduan MongoDB. .
kueri dapat menggunakan indeks,
kueri hanya berisi kondisi pada kunci indeks, dan
predikat kueri mengakses satu rentang kunci indeks yang berdekatan