From 697c07533f6aa85e6a871c3fa281c745f5205ffe Mon Sep 17 00:00:00 2001 From: Tobeyw Date: Thu, 10 Oct 2024 17:39:04 +0800 Subject: [PATCH] fix getBlocklist for wss --- neo3fura_ws/lib/cli/src.go | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/neo3fura_ws/lib/cli/src.go b/neo3fura_ws/lib/cli/src.go index 4868b72b..55ad3aac 100644 --- a/neo3fura_ws/lib/cli/src.go +++ b/neo3fura_ws/lib/cli/src.go @@ -4,9 +4,11 @@ import ( "context" "encoding/json" "fmt" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + log2 "neo3fura_ws/lib/log" ) @@ -120,6 +122,59 @@ func (me *T) QueryAll(args struct { return convert, count, nil } +func (me *T) QueryAggregate(args struct { + Collection string + Index string + Sort bson.M + Filter bson.M + Pipeline []bson.M + Query []string +}, ret *json.RawMessage) ([]map[string]interface{}, error) { + + var results []map[string]interface{} + convert := make([]map[string]interface{}, 0) + collection := me.C_online.Database(me.Db_online).Collection(args.Collection) + op := options.AggregateOptions{} + op.SetAllowDiskUse(true) + + cursor, err := collection.Aggregate(context.TODO(), args.Pipeline, &op) + + defer func(cursor *mongo.Cursor, ctx context.Context) { + err := cursor.Close(ctx) + if err != nil { + log2.Fatalf("Closing cursor error %v", err) + } + }(cursor, context.TODO()) + if err == mongo.ErrNoDocuments { + return nil, fmt.Errorf("document not found") + } + if err != nil { + return nil, err + } + if err = cursor.All(context.TODO(), &results); err != nil { + return nil, fmt.Errorf("find documents error:%s", err) + } + + for _, item := range results { + if len(args.Query) == 0 { + convert = append(convert, item) + } else { + temp := make(map[string]interface{}) + for _, v := range args.Query { + temp[v] = item[v] + } + convert = append(convert, temp) + } + } + + r, err := json.Marshal(convert) + if err != nil { + return nil, err + } + *ret = json.RawMessage(r) + return convert, nil +} + func (me *T) GetDistinctCount(args struct { Collection string Index string