Search code examples
mongodbgobsonmgo

Rename mongo collection in Go using mgo or bson?


I'd like to rename a mongo collection in my Go app. I'm using the mgo driver and it doesn't have a defined method to do this. Does anyone know what the query would be using bson? This is the command I'd like to implement in Go: http://docs.mongodb.org/manual/reference/command/renameCollection/


Solution

  • I haven't used mgo but this looks like exactly what you want to run a raw query.

    http://godoc.org/labix.org/v2/mgo#Session.Run

    In directly to mongo:

    db.adminCommand({renameCollection:'yourdb.yourcollection', to:'yourdb.yournewcollection'})
    

    Using mgo:

    session.Run(bson.D{{"renameCollection", "yourdb.yourcollection"}, {"to", "yourdb.yournewcollection"}})