From 184d789f939827f31eb62d10e5ab355421bc6ea8 Mon Sep 17 00:00:00 2001 From: Christophe Serafin Date: Wed, 17 Apr 2019 12:51:46 -0700 Subject: [PATCH] new: add method to delete indexes (#65) --- manipmongo/helpers.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/manipmongo/helpers.go b/manipmongo/helpers.go index 4ea67d66..8e979707 100644 --- a/manipmongo/helpers.go +++ b/manipmongo/helpers.go @@ -77,6 +77,28 @@ func CreateIndex(manipulator manipulate.Manipulator, identity elemental.Identity return nil } +// DeleteIndex deletes multiple mgo.Index for the collection. +func DeleteIndex(manipulator manipulate.Manipulator, identity elemental.Identity, indexes ...string) error { + + m, ok := manipulator.(*mongoManipulator) + if !ok { + panic("you can only pass a mongo manipulator to DeleteIndex") + } + + session := m.rootSession.Copy() + defer session.Close() + + collection := session.DB(m.dbName).C(identity.Name) + + for _, index := range indexes { + if err := collection.DropIndexName(index); err != nil { + return err + } + } + + return nil +} + // CreateCollection creates a collection using the given mgo.CollectionInfo. func CreateCollection(manipulator manipulate.Manipulator, identity elemental.Identity, info *mgo.CollectionInfo) error {