Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(mongodb): native driver query with mongosh fallback (#19100)
h3n4la7ecbed0026f
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex 52a8b3cd2a..af31b1fac3 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -14,6 +14,7 @@ import ("strings""time"+ "github.com/bytebase/gomongo""github.com/google/uuid""github.com/pkg/errors""go.mongodb.org/mongo-driver/v2/bson"@@ -250,8 +251,47 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q}statement = strings.Trim(statement, " \t\n\r\f;")- simpleStatement := isMongoStatement(statement)startTime := time.Now()++ var gomongoErr error+ if d.client != nil {+ gmClient := gomongo.NewClient(d.client)+ var gmResult *gomongo.Result+ gmResult, gomongoErr = gmClient.Execute(ctx, d.databaseName, statement)+ if gomongoErr == nil {+ return d.convertGomongoResult(gmResult, statement, startTime), nil+ }+ }++ results, err := d.queryConnWithMongosh(ctx, statement, queryContext, startTime)+ if err == nil && gomongoErr != nil {+ slog.Debug("executed query with mongosh fallback", slog.String("statement", statement), log.BBError(gomongoErr))+ }+ return results, err+}++func (*Driver) convertGomongoResult(res *gomongo.Result, statement string, startTime time.Time) []*v1pb.QueryResult {+ rows := []*v1pb.QueryRow{}+ for _, r := range res.Rows {+ rows = append(rows, &v1pb.QueryRow{+ Values: []*v1pb.RowValue{+ {Kind: &v1pb.RowValue_StringValue{StringValue: r}},+ },+ })+ }++ return []*v1pb.QueryResult{{+ ColumnNames: []string{"result"},+ ColumnTypeNames: []string{"TEXT"},+ Rows: rows,+ RowsCount: int64(res.RowCount),+ Latency: durationpb.New(time.Since(startTime)),+ Statement: statement,+ }}+}++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)// For MongoDB query, we execute the statement in mongosh with flag --eval for the following reasons:// 1. Query always short, so it's safe to execute in the command line.diff --git a/go.mod b/go.modindex 9ea6aa5074..8ee6af5a6f 100644--- a/go.mod+++ b/go.mod@@ -32,6 +32,7 @@ require (github.com/beltran/gohive/v2 v2.0.0github.com/blang/semver/v4 v4.0.0github.com/bmatcuk/doublestar/v4 v4.9.2+ github.com/bytebase/gomongo v0.0.0-20260121095259-4d0ac7950ed8github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0github.com/caarlos0/env/v11 v11.3.1github.com/cenkalti/backoff/v5 v5.0.3@@ -299,7 +300,7 @@ require (github.com/aws/aws-sdk-go-v2/service/sts v1.41.6github.com/aws/smithy-go v1.24.0 // indirectgithub.com/beorn7/perks v1.0.1 // indirect- github.com/bytebase/parser v0.0.0-20260113041207-12c6f17e2d8e+ github.com/bytebase/parser v0.0.0-20260121030202-698704919f24github.com/cespare/xxhash/v2 v2.3.0 // indirectgithub.com/cncf/xds/go v0.0.0-20251022180443-0feb69152e9f // indirectgithub.com/cockroachdb/cockroach-go/v2 v2.4.3diff --git a/go.sum b/go.sumindex a47ce16cf3..e96d7783c7 100644--- a/go.sum+++ b/go.sum@@ -857,10 +857,12 @@ github.com/bytebase/gh-ost2 v1.1.7-0.20251002210738-35e5dddaad7c h1:scJ4ycKuUAaAgithub.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-20260121095259-4d0ac7950ed8 h1:/MFuVj7SR+TQAXtLkt5y72a8JnvKcYITfQOoAhyvbvM=+github.com/bytebase/gomongo v0.0.0-20260121095259-4d0ac7950ed8/go.mod h1:7aVbtrEx9OpYH+qoeyqdxAVXH5NsBJSbTScD2jt+B0o=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-20260113041207-12c6f17e2d8e h1:MtSRG2W59qJujPTbK8jUoQKJX4wLRYXJDWDOgtVa8kY=-github.com/bytebase/parser v0.0.0-20260113041207-12c6f17e2d8e/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=+github.com/bytebase/parser v0.0.0-20260121030202-698704919f24 h1:oonTO26orUa4bYk/hQjALiYO1zII+Kzpjg75OnC3VtU=+github.com/bytebase/parser v0.0.0-20260121030202-698704919f24/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94 h1:6PNsuNmSqCuR8Z616uQLzDIvujWCFsKzeVc3ECmVubk=@@ -1755,6 +1757,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKugithub.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=github.com/testcontainers/testcontainers-go v0.40.0 h1:pSdJYLOVgLE8YdUY2FHQ1Fxu+aMnb6JfVz1mxk7OeMU=github.com/testcontainers/testcontainers-go v0.40.0/go.mod h1:FSXV5KQtX2HAMlm7U3APNyLkkap35zNLxukw9oBi/MY=+github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0 h1:z/1qHeliTLDKNaJ7uOHOx1FjwghbcbYfga4dTFkF0hU=+github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0/go.mod h1:GaunAWwMXLtsMKG3xn2HYIBDbKddGArfcGsF2Aog81E=github.com/the42/cartconvert v0.0.0-20131203171324-aae784c392b8 h1:I4DY8wLxJXCrMYzDM6lKCGc3IQwJX0PlTLsd3nQqI3c=github.com/the42/cartconvert v0.0.0-20131203171324-aae784c392b8/go.mod h1:fWO/msnJVhHqN1yX6OBoxSyfj7TEj1hHiL8bJSQsK30=github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4=