我想使用聚合將一些數(shù)據(jù)與 mongo-go-driver 進(jìn)行分組,但是 json 結(jié)果讓我感到困惑,因?yàn)樾墟I替換為“Key”,并且真正的鍵成為“Key”的值var result primitive.Apipeline := mongo.Pipeline{{{"$group", bson.D{{"_id", "$nis"}}}}}cursor, err := db.NilaiUH.Aggregate(context.TODO(), pipeline)cursor.All(context.Background(), &result)json.NewEncoder(w).Encode(&result)這是結(jié)果[ [ { "Key": "_id", "Value": 15100 } ], [ { "Key": "_id", "Value": 15101 } ]]
1 回答
明月笑刀無情
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
新版本的 MongoDB 驅(qū)動程序?qū)⒖战涌谝暈殒I和值的數(shù)組,而不是字符串和空接口之間的映射。我已使用以下代碼糾正了該問題:
// register custom codec registry to handle empty interfaces
rb := bson.NewRegistryBuilder()
rb.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.M{}))
client, err := mongo.NewClient(options.Client().ApplyURI(url).SetRegistry(rb.Build()))
- 1 回答
- 0 關(guān)注
- 150 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
