Skip to content

Commit 454da02

Browse files
feliixxdomodwyer
authored andcommitted
add DropAllIndexes() method (#25)
Create a new method to drop all the indexes of a collection in a single call
1 parent 1519fd3 commit 454da02

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

session.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,29 @@ func (c *Collection) DropIndexName(name string) error {
14991499
return nil
15001500
}
15011501

1502+
// DropAllIndexes drops all the indexes from the c collection
1503+
func (c *Collection) DropAllIndexes() error {
1504+
session := c.Database.Session
1505+
session.ResetIndexCache()
1506+
1507+
session = session.Clone()
1508+
defer session.Close()
1509+
1510+
db := c.Database.With(session)
1511+
result := struct {
1512+
ErrMsg string
1513+
Ok bool
1514+
}{}
1515+
err := db.Run(bson.D{{"dropIndexes", c.Name}, {"index", "*"}}, &result)
1516+
if err != nil {
1517+
return err
1518+
}
1519+
if !result.Ok {
1520+
return errors.New(result.ErrMsg)
1521+
}
1522+
return nil
1523+
}
1524+
15021525
// nonEventual returns a clone of session and ensures it is not Eventual.
15031526
// This guarantees that the server that is used for queries may be reused
15041527
// afterwards when a cursor is received.
@@ -1512,19 +1535,6 @@ func (session *Session) nonEventual() *Session {
15121535

15131536
// Indexes returns a list of all indexes for the collection.
15141537
//
1515-
// For example, this snippet would drop all available indexes:
1516-
//
1517-
// indexes, err := collection.Indexes()
1518-
// if err != nil {
1519-
// return err
1520-
// }
1521-
// for _, index := range indexes {
1522-
// err = collection.DropIndex(index.Key...)
1523-
// if err != nil {
1524-
// return err
1525-
// }
1526-
// }
1527-
//
15281538
// See the EnsureIndex method for more details on indexes.
15291539
func (c *Collection) Indexes() (indexes []Index, err error) {
15301540
cloned := c.Database.Session.nonEventual()

session_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,6 +3478,31 @@ func (s *S) TestEnsureIndexDropIndexName(c *C) {
34783478
c.Assert(err, ErrorMatches, "index not found.*")
34793479
}
34803480

3481+
func (s *S) TestEnsureIndexDropAllIndexes(c *C) {
3482+
session, err := mgo.Dial("localhost:40001")
3483+
c.Assert(err, IsNil)
3484+
defer session.Close()
3485+
3486+
coll := session.DB("mydb").C("mycoll")
3487+
3488+
err = coll.EnsureIndexKey("a")
3489+
c.Assert(err, IsNil)
3490+
3491+
err = coll.EnsureIndexKey("b")
3492+
c.Assert(err, IsNil)
3493+
3494+
err = coll.DropAllIndexes()
3495+
c.Assert(err, IsNil)
3496+
3497+
sysidx := session.DB("mydb").C("system.indexes")
3498+
3499+
err = sysidx.Find(M{"name": "a_1"}).One(nil)
3500+
c.Assert(err, Equals, mgo.ErrNotFound)
3501+
3502+
err = sysidx.Find(M{"name": "b_1"}).One(nil)
3503+
c.Assert(err, Equals, mgo.ErrNotFound)
3504+
}
3505+
34813506
func (s *S) TestEnsureIndexCaching(c *C) {
34823507
session, err := mgo.Dial("localhost:40001")
34833508
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)