使用mgo或bson在Go中重命名Mongo集合?

huangapple go评论85阅读模式
英文:

Rename mongo collection in Go using mgo or bson?

问题

我可以帮你翻译这段内容。以下是翻译结果:

我想在我的Go应用程序中重命名一个Mongo集合。我正在使用mgo驱动程序,但它没有定义的方法来执行此操作。有人知道使用bson的查询应该是什么吗?这是我想在Go中实现的命令:http://docs.mongodb.org/manual/reference/command/renameCollection/

英文:

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/

答案1

得分: 6

我还没有使用过mgo,但这看起来正是你想要运行原始查询的方法。

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

直接在mongo中执行:

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

使用mgo:

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

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"}})

huangapple
  • 本文由 发表于 2015年1月17日 07:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/27994221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定