Skip to content

Commit

Permalink
support list collections and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Koston Zhuang committed Oct 19, 2022
1 parent d076e1b commit 6c3a3f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
47 changes: 47 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,28 @@ func (c *Collection) DropIndex(ctx context.Context, indexes []string) error {
return err
}

// ListIndexes list all indexes in one collection
func (c *Collection) ListIndexes(ctx context.Context) ([]opts.IndexModel, error) {
cursor, err := c.collection.Indexes().List(ctx)
if err != nil {
return nil, err
}
var indexInfos []bson.M
err = cursor.All(ctx, &indexInfos)
if err != nil {
return nil, err
}
res := make([]opts.IndexModel, 0, len(indexInfos))
for _, indexInfo := range indexInfos {
model, err := transIndexInfoToIndexModel(indexInfo)
if err != nil {
return nil, err
}
res = append(res, model)
}
return res, err
}

// generate indexes that store in mongo which may consist more than one index(like []string{"index1","index2"} is stored as "index1_1_index2_1")
func generateDroppedIndex(index []string) string {
var res string
Expand All @@ -539,6 +561,31 @@ func generateDroppedIndex(index []string) string {
return res
}

func transIndexInfoToIndexModel(indexInfo bson.M) (opts.IndexModel, error) {
bytes, err := bson.Marshal(indexInfo)
res := opts.IndexModel{}
if err != nil {
return res, err
}
indexOptions := options.IndexOptions{}
err = bson.Unmarshal(bytes, &indexOptions)
if err != nil {
return res, err
}
keyMap := indexInfo["key"].(bson.M)
keySlice := make([]string, 0)
for k, v := range keyMap {
keyStr := k
if v.(int32) < 0 {
keyStr = fmt.Sprintf("-%s", keyStr)
}
keySlice = append(keySlice, keyStr)
}
res.Key = keySlice
res.IndexOptions = &indexOptions
return res, nil
}

// DropCollection drops collection
// it's safe even collection is not exists
func (c *Collection) DropCollection(ctx context.Context) error {
Expand Down
17 changes: 11 additions & 6 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package qmgo

import (
"context"
"go.mongodb.org/mongo-driver/bson"

opts "github.com/qiniu/qmgo/options"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
Expand Down Expand Up @@ -50,6 +51,10 @@ func (d *Database) DropDatabase(ctx context.Context) error {
return d.database.Drop(ctx)
}

func (d *Database) ListCollectionNames(ctx context.Context) ([]string, error) {
return d.database.ListCollectionNames(ctx, bson.D{})
}

// RunCommand executes the given command against the database.
//
// The runCommand parameter must be a document for the command to be executed. It cannot be nil.
Expand All @@ -72,11 +77,11 @@ func (d *Database) RunCommand(ctx context.Context, runCommand interface{}, opts
// The opts parameter can be used to specify options for the operation (see the options.CreateCollectionOptions
// documentation).
func (db *Database) CreateCollection(ctx context.Context, name string, opts ...opts.CreateCollectionOptions) error {
var option = make([]*options.CreateCollectionOptions,0,len(opts))
for _,opt := range opts{
if opt.CreateCollectionOptions != nil{
option = append(option,opt.CreateCollectionOptions)
var option = make([]*options.CreateCollectionOptions, 0, len(opts))
for _, opt := range opts {
if opt.CreateCollectionOptions != nil {
option = append(option, opt.CreateCollectionOptions)
}
}
return db.database.CreateCollection(ctx,name,option...)
}
return db.database.CreateCollection(ctx, name, option...)
}

0 comments on commit 6c3a3f7

Please sign in to comment.