簡體   English   中英

如何使用Mongoid 7.x和MongoDB 3.x重命名集合

[英]How do I rename a collection with Mongoid 7.x and MongoDB 3.x

我有一個使用Ruby 2.2.2的測試應用程序,並且在MongoDB 3.6.2中使用了Mongoid 7.0.0和Moped 1.5.3-我們正在使用Mongoid 4.x和MongoDB 2.6升級一個古老的代碼庫,發現在一路走來的API

最嚴重的是我們曾經能夠做SomeModel.collection.rename,但是(據我所知)此API方法現在已不存在,並給出了未定義的錯誤

我也嘗試了以下方法:

Mongoid.default_client.command({ renameCollection: "test.some_collection", to: "test.some_collection2", dropTarget: true })

但是,這返回

Mongo::Error::OperationFailure: renameCollection may only be run against the admin database. (13)

但是,從命令外殼程序,我可以發出:

db.some_collection.renameCollection("some_collection2") 

這行得通-從我所看到的看來這似乎是我唯一的最后求助方法,我將如何將其發布為輕便摩托車命令? (我不太熟悉語法方案)

另外,Mongoid顯然沒有公開這種看似簡單的操作的原因嗎?

因此找到了一種方法-我的一些收藏品存儲在不同的數據庫中

# returns a hash of client config from mongoid.yml eg ["secondary_db", { "database" => "myDB", hosts => [xxx, xxx, xxx] }]
cfg = Mongoid.clients.select { |k,v| Mongoid.clients[k][:database] == MyModel.storage_options[:database] }.first 

# now create a new Client instance using the right database
client = Mongo::Client.new(cfg[1]["hosts"], { database: cfg[1]["database"]})

client.command("$eval" => "db.my_collection.renameCollection('new_col_name')")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM