【发布时间】:2012-08-18 07:57:21
【问题描述】:
我有一个名为艺术家的收藏,我想将其重命名为 Artist_lookups。我该怎么做?
【问题讨论】:
我有一个名为艺术家的收藏,我想将其重命名为 Artist_lookups。我该怎么做?
【问题讨论】:
使用 mongoid5 / mongo ruby 驱动程序 2:
# if you need to check whether foo exists
return unless Mongoid.default_client.collections.map(&:name).include?('foo')
# rename to bar
Mongoid.default_client.use(:admin).command(
renameCollection: "#{Mongoid.default_client.database.name}.foo",
to: "#{Mongoid.default_client.database.name}.bar"
)
【讨论】:
db.artists.renameCollection("artist_lookups")
肯定会起作用。
【讨论】:
很简单,在 mongo shell 中,这样做:
db.artists.renameCollection("artist_lookups");
如果您想删除 artist_lookups(如果存在):
db.artists.renameCollection("artist_lookups", true);
你可以得到一些例外。
【讨论】:
来自Mongoid Docs:
class Band
include Mongoid::Document
store_in collection: "artists", database: "music", session: "secondary"
end
在您的模型中使用store_in collection: "artist_lookups"。这将允许您将 Artist 模型存储在 artist_lookups 集合中。
如果您想保留artists集合中的现有数据并对其进行重命名,建议您暂时关闭您的应用,在您的MongoDB服务器上将集合重命名为artist_lookups,然后重新启动应用。
【讨论】: