Mongoid:使用多个数据库? [英] Mongoid: using more than one database?

查看:69
本文介绍了Mongoid:使用多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails应用程序中使用MongoDB和MongoID,某些模型如何成为同一服务器上不同mongo数据库的一部分?我将如何完成类似的任务?我以前遇到过与mysql相同的问题,找不到合理的解决方案.

I'm using MongoDB and MongoID in a rails app, how can some models be a part of a different mongo database on the same server? How would I accomplish something like that? I've run into the same problem with mysql before and couldn't find a reasonable solution.

有什么想法吗?

推荐答案

最新版本的Mongoid支持此功能.请参见文档.

The newest versions of Mongoid support this. See the docs.

摘要:

config/mongoid.yml:

config/mongoid.yml:

defaults: &defaults
  host: localhost
  slaves:
    - host: localhost
      port: 27018
    - host: localhost
      port: 27019
  databases:
    secondary:
      database: secondary_database
      host: localhost
      port: 27020
      slaves:
        - host: localhost
          port: 27021
        - host: localhost
          port: 27022

在您的模型中:

class Business
  include Mongoid::Document
  set_database :secondary
end

这篇关于Mongoid:使用多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆