Implement list and fix test
This commit is contained in:
31
mongo.go
31
mongo.go
@@ -55,7 +55,36 @@ func NewMongo(addr string, auth ...string) (*Mongo, error) {
|
||||
}
|
||||
|
||||
func (mg *Mongo) List(ns []string, limits ...string) ([]string, error) {
|
||||
return nil, errors.New("not impl")
|
||||
namespace := resolveNamespace(ns)
|
||||
limits = resolveLimits(limits)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
collection := mg.db.Database(DefaultNamespace).Collection(namespace)
|
||||
filter := bson.M{"_id": bson.M{
|
||||
"$gte": limits[0],
|
||||
"$lte": limits[1],
|
||||
}}
|
||||
projection := bson.M{"_id": 1}
|
||||
cursor, err := collection.Find(ctx, filter, options.Find().SetProjection(projection))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
keys := []string{}
|
||||
for cursor.Next(ctx) {
|
||||
var elem bson.Raw
|
||||
if err := cursor.Decode(&elem); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if raw, err := elem.LookupErr("_id"); err != nil {
|
||||
return nil, err
|
||||
} else if s, ok := raw.StringValueOK(); !ok {
|
||||
return nil, errors.New("_id is not a string")
|
||||
} else {
|
||||
keys = append(keys, s)
|
||||
}
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (mg *Mongo) Get(key string, ns ...string) ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user