diff --git a/setup.go b/setup.go index 662fa1a..5a638af 100644 --- a/setup.go +++ b/setup.go @@ -9,10 +9,22 @@ import ( ) // EnsureIndex ensured mongodb index reflecting model struct index tag -func (db *MgoDatabase) EnsureIndex(m ModelInterface) error { +func (db *MgoDatabase) EnsureIndex(m ModelInterface, index ...Index) error { colName := GetCollectionName(m) col := db.C(colName) + if len(index) > 0 { + // only ensure given index + for _, i := range index { + err := col.EnsureIndex(i) + if err != nil { + return err + } + } + return nil + } + + // ensure index by struct fields mType := reflect.TypeOf(m) textFields := []string{} diff --git a/types.go b/types.go index 7b6a8b2..9096720 100644 --- a/types.go +++ b/types.go @@ -22,9 +22,9 @@ type Session interface { type Database interface { Session() Session - C(name string) Collection + // C(name string) Collection Name() string - EnsureIndex(m ModelInterface) error + EnsureIndex(m ModelInterface, index ...Index) error ValidateObject(m ModelInterface, changes bson.M) error ReadDocument(m ModelInterface, selector bson.M) error CreateDocument(m ModelInterface) error