Provides helpers on top of mongo-go-driver
Version 2, for the official mongo-go-driver¶
note: version 1 (mgo based) is deprecated
Install and update -
go get -u github.com/go-pkgz/mongo/v2
Connect- Connects with mongo url and return mongo client. Supports extra url params to pass a set of custom values in the url, for example
mongo.Clientas well as the map with all extra key/values. After connect call it also tries to ping the mongo server.
BufferedWriterimplements buffered writer to mongo. Write method caching internally till it reached buffer size. Flush methods can be called manually at any time.
WithCollectionsets collection name to write to
WithAutoFlushsets auto flush duration
PrepSort- prepares sort object
bson.Dfrom strings like
PrepIndex- prepares index object
driver.IndexModelfrom strings like
mongo.Collectionfor url defined in env
MONGO_TEST. If not defined
mongodb://mongo:27017used. By default it will use random connection with prefix
mongo.Collectionwith a custom collection name.
Both methods return
mongo.Collection which can be used for testing. It also returns
teardown func to clean mongo after the test.