-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mongo db - create/drop indexes
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters