Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(mongodb): update gomongo library and handle BSON marshaling (#19148)

h3n4l28fd2099b3f9
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.go
index 08a08fc6ea..6e47ffcccc 100644
--- a/backend/plugin/db/mongodb/mongodb.go
+++ b/backend/plugin/db/mongodb/mongodb.go
@@ -278,10 +278,15 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
func (*Driver) convertGomongoResult(res *gomongo.Result, statement string, startTime time.Time) []*v1pb.QueryResult {
rows := []*v1pb.QueryRow{}
- for _, r := range res.Rows {
+ for _, v := range res.Value {
+ str, err := marshalValueToExtJSON(v)
+ if err != nil {
+ slog.Error("failed to marshal gomongo result value", log.BBError(err))
+ str = fmt.Sprintf("%v", v)
+ }
rows = append(rows, &v1pb.QueryRow{
Values: []*v1pb.RowValue{
- {Kind: &v1pb.RowValue_StringValue{StringValue: r}},
+ {Kind: &v1pb.RowValue_StringValue{StringValue: str}},
},
})
}
@@ -290,12 +295,31 @@ func (*Driver) convertGomongoResult(res *gomongo.Result, statement string, start
ColumnNames: []string{"result"},
ColumnTypeNames: []string{"TEXT"},
Rows: rows,
- RowsCount: int64(res.RowCount),
+ RowsCount: int64(len(res.Value)),
Latency: durationpb.New(time.Since(startTime)),
Statement: statement,
}}
}
+// marshalValueToExtJSON marshals a value to Extended JSON (relaxed) format.
+// The frontend expects each row to be a JSON object with key-value pairs,
+// so primitive values are wrapped in an object with a "value" key.
+func marshalValueToExtJSON(v any) (string, error) {
+ var toMarshal any
+ switch val := v.(type) {
+ case string, int64, bool:
+ // For primitive results, wrap as JSON object
+ toMarshal = bson.M{"value": val}
+ default:
+ toMarshal = val
+ }
+ jsonBytes, err := bson.MarshalExtJSON(toMarshal, false, false)
+ if err != nil {
+ return "", err
+ }
+ return string(jsonBytes), nil
+}
+
func (d *Driver) queryConnWithMongosh(ctx context.Context, statement string, queryContext db.QueryContext, startTime time.Time) ([]*v1pb.QueryResult, error) {
simpleStatement := isMongoStatement(statement)
connectionURI := getBasicMongoDBConnectionURI(d.connCfg)
diff --git a/go.mod b/go.mod
index a65f27fca6..23a98be713 100644
--- a/go.mod
+++ b/go.mod
@@ -32,7 +32,7 @@ require (
github.com/beltran/gohive/v2 v2.0.0
github.com/blang/semver/v4 v4.0.0
github.com/bmatcuk/doublestar/v4 v4.9.2
- github.com/bytebase/gomongo v0.0.0-20260123064557-5e3ae906bb2c
+ github.com/bytebase/gomongo v0.0.0-20260127074220-ca70aee87419
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
github.com/caarlos0/env/v11 v11.3.1
github.com/cenkalti/backoff/v5 v5.0.3
diff --git a/go.sum b/go.sum
index 079ed2bfb3..9d5c8b0a9f 100644
--- a/go.sum
+++ b/go.sum
@@ -857,8 +857,8 @@ github.com/bytebase/gh-ost2 v1.1.7-0.20251002210738-35e5dddaad7c h1:scJ4ycKuUAaA
github.com/bytebase/gh-ost2 v1.1.7-0.20251002210738-35e5dddaad7c/go.mod h1:X0CGYwIZUnjfrv02/nBlftxqSBMm4mYJzFwiyn2D4d8=
github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898 h1:0ggqA9XL4yvtePPvRDRYMed3jtdNk4s0o8AMW8uJPSA=
github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898/go.mod h1:2KhUz4GSLcdJGXSJMXAf2BjmakcgoOkNyPpHdGAxRpY=
-github.com/bytebase/gomongo v0.0.0-20260123064557-5e3ae906bb2c h1:e1yWka4+s62d7rInxzkJZXmeu5ntH66TqLilqOuqqUE=
-github.com/bytebase/gomongo v0.0.0-20260123064557-5e3ae906bb2c/go.mod h1:tmnMdRvFw6R4jYLWnPCtGMsCh2McnCXBLW4ZonyShsE=
+github.com/bytebase/gomongo v0.0.0-20260127074220-ca70aee87419 h1:1c35jlaOeYDpok/IUHKsYnd3UVXN8BMXJO0jAy9VvF0=
+github.com/bytebase/gomongo v0.0.0-20260127074220-ca70aee87419/go.mod h1:tmnMdRvFw6R4jYLWnPCtGMsCh2McnCXBLW4ZonyShsE=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=
github.com/bytebase/parser v0.0.0-20260123082732-e9723d1469a0 h1:a8xGDaGm0w1AgPgZSO5eqJcuSjeut8jDMrVfyaVLyII=