improve mongo storage

This commit is contained in:
Wei Lun
2020-11-05 21:52:34 +08:00
parent 930aae7ea7
commit f242d1f466
3 changed files with 14 additions and 19 deletions

View File

@@ -37,7 +37,7 @@ func New(config ...Config) *Storage {
cfg := configDefault(config...)
// Create data source name
var dsn string = "mongodb://"
var dsn = "mongodb://"
if cfg.Username != "" {
dsn += url.QueryEscape(cfg.Username)
}
@@ -65,10 +65,21 @@ func New(config ...Config) *Storage {
panic(err)
}
// verify that the client can connect
if err = client.Ping(context.Background(), nil); err != nil {
panic(err)
}
// Get collection from database
db := client.Database(cfg.Database)
col := db.Collection(cfg.Collection)
if cfg.Clear {
if err = col.Drop(context.Background()); err != nil {
panic(err)
}
}
// expired data may exist for some time beyond the 60 second period between runs of the background task.
// more on https://docs.mongodb.com/manual/core/index-ttl/
indexModel := mongo.IndexModel{