Skip to content

Commit

Permalink
feat: mongo db - create/drop indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Mar 9, 2024
1 parent cf9b3cb commit 16fff60
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
64 changes: 64 additions & 0 deletions database/mongoIndex.go
Original file line number Diff line number Diff line change
@@ -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)
}
38 changes: 38 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 16fff60

Please sign in to comment.