MongoDB - Beispiel für Aggregat und Gruppe

MongoDB - Beispiel für Aggregate und Gruppen

mongodb-group-example

In diesem Tutorial zeigen wir Ihnen, wie Sie mit der MongoDB-Aggregatfunktion Dokumente (Daten) gruppieren können.

1. Testdaten

Daten im JSON-Format, zeigt den Hosting-Anbieter für die Website.

website.json

{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
{ "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com"}
{ "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" }
{ "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" }
{ "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" }
{ "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" }
{ "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" }
{ "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" }
{ "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" }
{ "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }

Importiert in eine "Website" -Sammlung.

> mongoimport -d testdb -c website --file website.json
connected to: 127.0.0.1
Mon Jan 13 14:30:22.662 imported 10 objects

Note
Wenn die Sammlung vorhanden ist, fügen Sie die Option--upserthinzu, um die Daten zu überschreiben.

> mongoimport -d testdb -c website --file website.json --upsert

2. Gruppierungsbeispiel

Verwendetdb.collection.aggregate und$group, um die Datengruppierung durchzuführen.

2.1 Das folgende Beispiel gruppiert nach dem Feld "Hosting" und zeigt die Gesamtsumme jedes Hostings an.

> db.website.aggregate(
    {
    $group : {_id : "$hosting", total : { $sum : 1 }}
    }
  );

Ausgabe

{
        "result" : [
                {
                        "_id" : "godaddy.com",
                        "total" : 1
                },
                {
                        "_id" : "cloud.google.com",
                        "total" : 2
                },
                {
                        "_id" : "aws.amazon.com",
                        "total" : 4
                },
                {
                        "_id" : "hostgator.com",
                        "total" : 3
                }
        ],
        "ok" : 1
}

Die äquivalente SQL.

SELECT hosting, SUM(hosting) AS total
       FROM website
       GROUP BY hosting

2.2 Sortierung mit$sort hinzufügen.

>  db.website.aggregate(
     {
    $group : {_id : "$hosting", total : { $sum : 1 }}
     },
     {
    $sort : {total : -1}
     }
  );

Ausgabe - Zeigen Sie "Gesamt" in absteigender Reihenfolge an. Verwendet für aufsteigende Reihenfolge$sort : {total : 1}.

{
        "result" : [
                {
                        "_id" : "aws.amazon.com",
                        "total" : 4
                },
                {
                        "_id" : "hostgator.com",
                        "total" : 3
                },
                {
                        "_id" : "cloud.google.com",
                        "total" : 2
                },
                {
                        "_id" : "godaddy.com",
                        "total" : 1
                }
        ],
        "ok" : 1
}

2.3 Fügen Sie$match Bedingung hinzu, gruppieren Sie nach "Hosting" nur für "aws.amazon.com".

> db.website.aggregate(
    {
    $match : {hosting : "aws.amazon.com"}
    },
    {
    $group : { _id : "$hosting", total : { $sum : 1 } }
    }
  );

Ausgabe

{
        "result" : [
                {
                        "_id" : "aws.amazon.com",
                        "total" : 4
                }
        ],
        "ok" : 1
}

More Examples
Weitere Informationen zur Aggregation und Gruppe finden Sie in diesem offiziellenMongoDB Aggregation guide.

3. Exportiert das Gruppierungsergebnis nach CSV oder JSON

In vielen Fällen müssen die Gruppierungsergebnisse im CSV- oder JSON-Format exportiert werden. Um dies zu lösen, fügt die Gruppe Ergebnisse in eine neue Sammlung ein und exportiert die neue Sammlung übermongoexport.

3.1 Setzt die Gruppenergebnisse in eine Variable. In diesem Fall lautet der Variablenname „Gruppendaten“.

> var groupdata = db.website.aggregate(
    {
    $group : {_id : "$hosting", total : { $sum : 1 }}
    },
    {
    $sort : {total : -1}
    }
  );

3.2 Fügtgroupdata.toArray() in eine neue Sammlung ein.

> db.websitegroup.insert(groupdata.toArray());

> db.websitegroup.find().pretty()
{ "_id" : "aws.amazon.com", "total" : 4 }
{ "_id" : "hostgator.com", "total" : 3 }
{ "_id" : "cloud.google.com", "total" : 2 }
{ "_id" : "godaddy.com", "total" : 1 }
>

3.3 Exportiert die Sammlung "websitegroup" in eine CSV-Datei.

c:\> mongoexport -d testdb -c websitegroup -f _id,total -o group.csv --csv
connected to: 127.0.0.1
exported 4 records

group.csv

_id,total
"aws.amazon.com",4.0
"cloud.google.com",2.0
"godaddy.com",1.0
"hostgator.com",3.0

3.4 Exportiert die Sammlung "websitegroup" in eine JSON-Datei.

c:\> mongoexport -d testdb -c websitegroup -o group.json
connected to: 127.0.0.1
exported 4 records

group.json

{ "_id" : "aws.amazon.com", "total" : 4 }
{ "_id" : "cloud.google.com", "total" : 2 }
{ "_id" : "godaddy.com", "total" : 1 }
{ "_id" : "hostgator.com", "total" : 3 }

4. Großsortierung

Changed in version 2.6 - Lesen Sie dieseMemory Restrictions
In MongoDB ist die In-Memory-Sortierung auf 100 MB begrenzt. Um eine große Sortierung durchzuführen, müssen Sie die OptionallowDiskUse aktivieren, um Daten zu schreiben in eine temporäre Datei zum Sortieren.

Aktivieren Sie die OptionallowDiskUse, um den Fehlersort exceeded memory limit zu vermeiden.

db.website.aggregate(
[
    {$group : {_id : "$hosting", total : { $sum : 1 }}},
    {$sort : {total : -1}}
],
    {allowDiskUse: true}
);