refactor aggregate again

This commit is contained in:
Florian Baumann 2022-01-06 14:37:47 +01:00
parent c9319ea27a
commit 011f1df1dc
2 changed files with 9 additions and 5 deletions

View File

@ -112,8 +112,12 @@ func Sets() {
{"release", bson.D{{"$last", "$releasedat"}}}, {"release", bson.D{{"$last", "$releasedat"}}},
}}, }},
} }
sortStage := bson.D{
{"$sort", bson.D{
{"release", 1},
}}}
sets, _ := coll.storage_aggregate(bson.D{{"$match", bson.D{{}}}}, groupStage) sets, _ := coll.storage_aggregate(mongo.Pipeline{groupStage, sortStage})
for _, set := range sets { for _, set := range sets {
fmt.Printf("* %s %s (%.2f Eur) %.0f\n", set["release"].(string)[0:4], set["_id"], set["value"], set["count"]) fmt.Printf("* %s %s (%.2f Eur) %.0f\n", set["release"].(string)[0:4], set["_id"], set["value"], set["count"])
} }
@ -151,7 +155,7 @@ func ShowSet(setname string) error {
{"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}}, {"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}},
}}, }},
} }
stats, _ := coll.storage_aggregate(matchStage, groupStage) stats, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
LogMessage(fmt.Sprintf("%s", sets[0].Name), "green") LogMessage(fmt.Sprintf("%s", sets[0].Name), "green")
LogMessage(fmt.Sprintf("Set Cards: %d/%d", len(cards), sets[0].CardCount), "normal") LogMessage(fmt.Sprintf("Set Cards: %d/%d", len(cards), sets[0].CardCount), "normal")
@ -229,7 +233,7 @@ func Stats() {
{"count", bson.D{{"$sum", 1}}}, {"count", bson.D{{"$sum", 1}}},
}}} }}}
sets, _ := coll.storage_aggregate(bson.D{{"$match", bson.D{{}}}}, groupStage) sets, _ := coll.storage_aggregate(mongo.Pipeline{groupStage})
for _, set := range sets { for _, set := range sets {
// TODO fix primitiveA Problem with loop and reflect // TODO fix primitiveA Problem with loop and reflect
fmt.Printf("* %s %d\n", set["_id"], set["count"]) fmt.Printf("* %s %d\n", set["_id"], set["count"])

View File

@ -99,13 +99,13 @@ func (coll Collection) storage_remove(filter bson.M) error {
} }
func (coll Collection) storage_aggregate(matchstage, groupstage bson.D) ([]primitive.M, error) { func (coll Collection) storage_aggregate(pipeline mongo.Pipeline) ([]primitive.M, error) {
// db.cards.aggregate([ {$group: { _id: "$setname", sum: { $sum: "$prices.eur"}}}]) // db.cards.aggregate([ {$group: { _id: "$setname", sum: { $sum: "$prices.eur"}}}])
opts := options.Aggregate().SetMaxTime(2 * time.Second) opts := options.Aggregate().SetMaxTime(2 * time.Second)
cursor, err := coll.Aggregate( cursor, err := coll.Aggregate(
context.TODO(), context.TODO(),
mongo.Pipeline{matchstage, groupstage}, pipeline,
opts) opts)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)