Skip to content

Instantly share code, notes, and snippets.

@divjotarora
Created November 15, 2021 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divjotarora/f0b5b82433deefbbf6ead8f7e57e6b43 to your computer and use it in GitHub Desktop.
Save divjotarora/f0b5b82433deefbbf6ead8f7e57e6b43 to your computer and use it in GitHub Desktop.
Go Driver bsoncore.Array pipeline repro
package main
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
const (
uri = "mongodb://localhost:27017/?serverSelectionTimeoutMS=1000"
)
var (
ctx = context.TODO()
)
func main() {
client := setupClient()
defer client.Disconnect(ctx)
coll := client.Database("foo").Collection("bar")
if err := coll.Drop(ctx); err != nil {
panic(err)
}
// {"$match": {}}
stage := bsoncore.NewDocumentBuilder().
StartDocument("$match").
FinishDocument().
Build()
pipeline := bsoncore.NewArrayBuilder().AppendDocument(stage).Build()
if _, err := coll.Aggregate(ctx, pipeline); err != nil {
panic(err)
}
}
func setupClient() *mongo.Client {
opts := options.Client().ApplyURI(uri)
client, err := mongo.Connect(ctx, opts)
if err != nil {
panic(err)
}
return client
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment