Initial import
This commit is contained in:
75
main_index.go
Normal file
75
main_index.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
// CreateIndexes will create indexes for the given collection and definition.
|
||||
func (c *MongoClient) CreateIndexes(collection *mongo.Collection, cdef *CollectionDefinition) {
|
||||
if cdef == nil {
|
||||
log.Printf("No definitions will not create indexes")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// handle indexes
|
||||
indexModels := make([]mongo.IndexModel, 0)
|
||||
|
||||
for _, keyDef := range cdef.IndexSpecs {
|
||||
log.Printf("Key Definition %s", keyDef["name"])
|
||||
kdb, err := bson.Marshal(keyDef)
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal %v", err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
kdRaw := bson.Raw(kdb)
|
||||
if err := kdRaw.Validate(); err != nil {
|
||||
log.Printf("failed to validate bson raw: %v", err)
|
||||
|
||||
continue
|
||||
}
|
||||
//
|
||||
idxModel := mongo.IndexModel{}
|
||||
|
||||
opts := options.Index()
|
||||
|
||||
keysVal := kdRaw.Lookup("keys")
|
||||
|
||||
var keysBson bson.D
|
||||
if err := bson.Unmarshal(keysVal.Value, &keysBson); err != nil {
|
||||
log.Printf("failed to unmarshal keys value %v", err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
idxModel.Keys = keysBson
|
||||
|
||||
nameVal := kdRaw.Lookup("name")
|
||||
if name, ok := nameVal.StringValueOK(); ok {
|
||||
opts = opts.SetName(name)
|
||||
}
|
||||
|
||||
uniqueVal := kdRaw.Lookup("unique")
|
||||
if unique, ok := uniqueVal.BooleanOK(); ok {
|
||||
opts = opts.SetUnique(unique)
|
||||
}
|
||||
|
||||
partialVal := kdRaw.Lookup("partialFilterExpression")
|
||||
if partialFilterExpression, ok := partialVal.BooleanOK(); ok {
|
||||
opts = opts.SetPartialFilterExpression(partialFilterExpression)
|
||||
}
|
||||
|
||||
idxModel.Options = opts
|
||||
|
||||
indexModels = append(indexModels, idxModel)
|
||||
}
|
||||
|
||||
collection.Indexes().CreateMany(context.Background(), indexModels)
|
||||
}
|
||||
Reference in New Issue
Block a user