gpt4 book ai didi

mongodb - 在 Mongoid 3.0 中运行时切换数据库

转载 作者:可可西里 更新时间:2023-11-01 09:58:14 25 4
gpt4 key购买 nike

我有一个在 Unicorn 上运行的 Sinatra 应用程序,它的模型使用 Mongoid。我有几个结构相同但内容不同的 Mongo 数据库,我会在每个用户登录时为他/她选择正确的数据库。我想知道 Mongoid 3.0 是否可以做到这一点。

最佳答案

如果要切换数据库,使用Mongoid.override_database,它是线程安全的。

Mongoid.override_database("client_db_name") # 改变数据库Mongoid.override_database(nil) # 重置数据库

例子:

class ApplicationController < ActionController::Base
before_filter :switch_database
after_filter :reset_database

private

def switch_database
client_ref = params[:client_id]
Mongoid.override_database("my_db_name_#{client_ref}")
end

def reset_database
Mongoid.override_database(nil)
end
end

可以找到文档here .

关于mongodb - 在 Mongoid 3.0 中运行时切换数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14963809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com