From 16fff6065d9b778de1909d908a96fff8cd82af31 Mon Sep 17 00:00:00 2001 From: pilinux Date: Sat, 9 Mar 2024 23:16:11 +0100 Subject: [PATCH] feat: mongo db - create/drop indexes --- database/mongoIndex.go | 64 ++++++++++++++++++++++++++++++++++++++++++ example/main.go | 38 +++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 database/mongoIndex.go diff --git a/database/mongoIndex.go b/database/mongoIndex.go new file mode 100644 index 0000000..5d1db79 --- /dev/null +++ b/database/mongoIndex.go @@ -0,0 +1,64 @@ +package database + +import ( + "context" + "time" + + "github.com/qiniu/qmgo/options" +) + +// MongoCreateIndex - create one index for a mongo collection +func MongoCreateIndex(dbName, collectionName string, index options.IndexModel) error { + client := GetMongo() + db := client.Database(dbName) // set database name + collection := db.Collection(collectionName) // set collection name + + // set max TTL + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + // create index + return collection.CreateOneIndex(ctx, index) +} + +// MongoCreateIndexes - create many indexes for a mongo collection +func MongoCreateIndexes(dbName, collectionName string, indexes []options.IndexModel) error { + client := GetMongo() + db := client.Database(dbName) // set database name + collection := db.Collection(collectionName) // set collection name + + // set max TTL + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + // create indexes + return collection.CreateIndexes(ctx, indexes) +} + +// MongoDropIndex - drop one/multiple indexes from a mongo collection +func MongoDropIndex(dbName, collectionName string, indexes []string) error { + client := GetMongo() + db := client.Database(dbName) // set database name + collection := db.Collection(collectionName) // set collection name + + // set max TTL + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + // drop index + return collection.DropIndex(ctx, indexes) +} + +// MongoDropAllIndexes - drop all indexes from a mongo collection except the index on the _id field +func MongoDropAllIndexes(dbName, collectionName string) error { + client := GetMongo() + db := client.Database(dbName) // set database name + collection := db.Collection(collectionName) // set collection name + + // set max TTL + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + // drop all indexes + return collection.DropAllIndexes(ctx) +} diff --git a/example/main.go b/example/main.go index 71f7bd1..f379e3b 100644 --- a/example/main.go +++ b/example/main.go @@ -6,6 +6,7 @@ import ( gconfig "github.com/pilinux/gorest/config" gdatabase "github.com/pilinux/gorest/database" + "github.com/qiniu/qmgo/options" "github.com/pilinux/gorest/example/database/migrate" "github.com/pilinux/gorest/example/router" @@ -64,6 +65,43 @@ func main() { fmt.Println(err) return } + + // Example of dropping index "countryCode" from collection "geocodes" in database "map" + /* + indexes := []string{"countryCode"} + if err := gdatabase.MongoDropIndex("map", "geocodes", indexes); err != nil { + fmt.Println(err) + return + } + */ + + // Example of dropping all indexes from collection "geocodes" in database "map" + /* + if err := gdatabase.MongoDropAllIndexes("map", "geocodes"); err != nil { + fmt.Println(err) + return + } + */ + + // Create new index for "countryCode" field + index := options.IndexModel{ + Key: []string{"countryCode"}, + } + // Example of creating many indexes + /* + indexes := []options.IndexModel{ + { + Key: []string{"state"}, + }, + { + Key: []string{"countryCode"}, + }, + } + */ + if err := gdatabase.MongoCreateIndex("map", "geocodes", index); err != nil { + fmt.Println(err) + return + } } r, err := router.SetupRouter(configure)