Implement namespace optional arg
This commit is contained in:
10
mongo.go
10
mongo.go
@@ -53,10 +53,11 @@ func NewMongo(addr string, auth ...string) (*Mongo, error) {
|
||||
return &Mongo{db: db}, nil
|
||||
}
|
||||
|
||||
func (mg *Mongo) Get(key string) ([]byte, error) {
|
||||
func (mg *Mongo) Get(key string, ns ...string) ([]byte, error) {
|
||||
namespace := resolveNamespace(ns)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
collection := mg.db.Database(DefaultNamespace).Collection(DefaultNamespace)
|
||||
collection := mg.db.Database(DefaultNamespace).Collection(namespace)
|
||||
filter := bson.M{"_id": key}
|
||||
cursor, err := collection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
@@ -82,10 +83,11 @@ func (mg *Mongo) Get(key string) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (mg *Mongo) Set(key string, value []byte) error {
|
||||
func (mg *Mongo) Set(key string, value []byte, ns ...string) error {
|
||||
namespace := resolveNamespace(ns)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
collection := mg.db.Database(DefaultNamespace).Collection(DefaultNamespace)
|
||||
collection := mg.db.Database(DefaultNamespace).Collection(namespace)
|
||||
filter := bson.M{"_id": key}
|
||||
document := bson.M{"value": value}
|
||||
_, err := collection.ReplaceOne(ctx, filter, document, options.Replace().SetUpsert(true))
|
||||
|
||||
Reference in New Issue
Block a user