diff --git a/core/stores/mongo/model.go b/core/stores/mongo/model.go index 9088896d..020bf07f 100644 --- a/core/stores/mongo/model.go +++ b/core/stores/mongo/model.go @@ -22,8 +22,8 @@ type ( } ) -func MustNewModel(url, database, collection string, opts ...Option) *Model { - model, err := NewModel(url, database, collection, opts...) +func MustNewModel(url, collection string, opts ...Option) *Model { + model, err := NewModel(url, collection, opts...) if err != nil { log.Fatal(err) } @@ -31,15 +31,16 @@ func MustNewModel(url, database, collection string, opts ...Option) *Model { return model } -func NewModel(url, database, collection string, opts ...Option) (*Model, error) { +func NewModel(url, collection string, opts ...Option) (*Model, error) { session, err := getConcurrentSession(url) if err != nil { return nil, err } return &Model{ - session: session, - db: session.DB(database), + session: session, + // If name is empty, the database name provided in the dialed URL is used instead + db: session.DB(""), collection: collection, opts: opts, }, nil diff --git a/core/stores/mongoc/cachedmodel.go b/core/stores/mongoc/cachedmodel.go index 4e7f8264..c2482f7b 100644 --- a/core/stores/mongoc/cachedmodel.go +++ b/core/stores/mongoc/cachedmodel.go @@ -16,8 +16,8 @@ type Model struct { generateCollection func(*mgo.Session) *cachedCollection } -func MustNewNodeModel(url, database, collection string, rds *redis.Redis, opts ...cache.Option) *Model { - model, err := NewNodeModel(url, database, collection, rds, opts...) +func MustNewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) *Model { + model, err := NewNodeModel(url, collection, rds, opts...) if err != nil { log.Fatal(err) } @@ -25,8 +25,8 @@ func MustNewNodeModel(url, database, collection string, rds *redis.Redis, opts . return model } -func MustNewModel(url, database, collection string, c cache.CacheConf, opts ...cache.Option) *Model { - model, err := NewModel(url, database, collection, c, opts...) +func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Option) *Model { + model, err := NewModel(url, collection, c, opts...) if err != nil { log.Fatal(err) } @@ -34,16 +34,16 @@ func MustNewModel(url, database, collection string, c cache.CacheConf, opts ...c return model } -func NewNodeModel(url, database, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) { +func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) { c := internal.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...) - return createModel(url, database, collection, c, func(collection mongo.Collection) *cachedCollection { + return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection { return newCollection(collection, c) }) } -func NewModel(url, database, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) { +func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) { c := internal.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...) - return createModel(url, database, collection, c, func(collection mongo.Collection) *cachedCollection { + return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection { return newCollection(collection, c) }) } @@ -224,9 +224,9 @@ func (mm *Model) pipe(fn func(c *cachedCollection) mongo.Pipe) (mongo.Pipe, erro return fn(mm.GetCollection(session)), nil } -func createModel(url, database, collection string, c internal.Cache, +func createModel(url, collection string, c internal.Cache, create func(mongo.Collection) *cachedCollection) (*Model, error) { - model, err := mongo.NewModel(url, database, collection) + model, err := mongo.NewModel(url, collection) if err != nil { return nil, err } diff --git a/example/mongo/time.go b/example/mongo/time.go index d82c0e0a..2ba91d88 100644 --- a/example/mongo/time.go +++ b/example/mongo/time.go @@ -19,7 +19,7 @@ type Roster struct { } func main() { - model := mongo.MustNewModel("localhost:27017", "blackboard", "roster") + model := mongo.MustNewModel("localhost:27017/blackboard", "roster") for i := 0; i < 1000; i++ { session, err := model.TakeSession() if err != nil {