|
| 1 | +============= |
| 2 | +Configuration |
| 3 | +============= |
| 4 | + |
| 5 | +.. _mongoid-configuration: |
| 6 | + |
| 7 | +Mongoid is customarily configured through a ``mongoid.yml`` file that specifies |
| 8 | +options and clients. The simplest configuration is as follows, which configures |
| 9 | +Mongoid to talk to a MongoDB server at "localhost:27017" and use the database |
| 10 | +named "mongoid". |
| 11 | + |
| 12 | +.. code-block:: yaml |
| 13 | + |
| 14 | + development: |
| 15 | + clients: |
| 16 | + default: |
| 17 | + database: mongoid |
| 18 | + hosts: |
| 19 | + - localhost:27017 |
| 20 | + |
| 21 | +The top level key in the configuration file, ``development`` in the above |
| 22 | +example, refers to the environment name which the application is executing in, |
| 23 | +i.e. ``development``, ``test`` or ``production``. The third level key, |
| 24 | +``default`` in the above example, refers to the Mongo client name. |
| 25 | +Most applications will use a single client named ``default``. |
| 26 | + |
| 27 | +Rails Applications |
| 28 | +****************** |
| 29 | + |
| 30 | +If you are using Rails, you can have Mongoid generate a default configuration |
| 31 | +file for you by running the following command: |
| 32 | + |
| 33 | +.. code-block:: bash |
| 34 | + |
| 35 | + $ rails g mongoid:config |
| 36 | + |
| 37 | +The configuration file will be placed in ``config/mongoid.yml``. |
| 38 | + |
| 39 | +Environment Determination |
| 40 | +************************* |
| 41 | + |
| 42 | +When Mongoid loads its configuration, it chooses the environment to use |
| 43 | +in the following order: |
| 44 | + |
| 45 | +- ``Rails.env`` if using Rails. |
| 46 | +- ``Sinatra::Base.environment`` if using Sinatra. |
| 47 | +- ``RACK_ENV`` environment variable. |
| 48 | +- ``MONGOID_ENV`` environment variable. |
| 49 | + |
| 50 | +If you are not building a Rack based application and want to override the |
| 51 | +environment programatically, you can pass a second parameter to ``load!`` |
| 52 | +as follows: |
| 53 | + |
| 54 | +.. code-block:: ruby |
| 55 | + |
| 56 | + Mongoid.load!("path/to/your/mongoid.yml", :production) |
| 57 | + |
| 58 | +Depending on your version of Rails, you may also need to configure the ORM |
| 59 | +to be Mongoid in application.rb like so: |
| 60 | + |
| 61 | +.. code-block:: ruby |
| 62 | + |
| 63 | + config.generators do |g| |
| 64 | + g.orm :mongoid |
| 65 | + end |
| 66 | + |
| 67 | +Anatomy of a Mongoid Config |
| 68 | +*************************** |
| 69 | + |
| 70 | +Let's have a look at a full-blown ``mongoid.yml`` and explain the full power |
| 71 | +of what Mongoid can do. The following configuration has explanation of the |
| 72 | +various options in the comments above each key: |
| 73 | + |
| 74 | +.. code-block:: yaml |
| 75 | + |
| 76 | + development: |
| 77 | + # Configure available database clients. (required) |
| 78 | + clients: |
| 79 | + # Define the default client. (required) |
| 80 | + default: |
| 81 | + # A uri may be defined for a client: |
| 82 | + # uri: 'mongodb://user: [email protected]:27017/my_db' |
| 83 | + # Please see driver documentation for details. Alternatively, you can |
| 84 | + # define the following: |
| 85 | + # |
| 86 | + # Define the name of the default database that Mongoid can connect to. |
| 87 | + # (required). |
| 88 | + database: my_db |
| 89 | + # Provide the hosts the default client can connect to. Must be an array |
| 90 | + # of host:port pairs. (required) |
| 91 | + hosts: |
| 92 | + - myhost1.mydomain.com:27017 |
| 93 | + - myhost2.mydomain.com:27017 |
| 94 | + - myhost3.mydomain.com:27017 |
| 95 | + options: |
| 96 | + # Change the default write concern. (default = { w: 1 }) |
| 97 | + write: |
| 98 | + w: 1 |
| 99 | + |
| 100 | + # Change the default read preference. Valid options for mode are: :secondary, |
| 101 | + # :secondary_preferred, :primary, :primary_preferred, :nearest |
| 102 | + # (default: primary) |
| 103 | + read: |
| 104 | + mode: :secondary_preferred |
| 105 | + tag_sets: |
| 106 | + - use: web |
| 107 | + |
| 108 | + # The name of the user for authentication. |
| 109 | + user: 'user' |
| 110 | + |
| 111 | + # The password of the user for authentication. |
| 112 | + password: 'password' |
| 113 | + |
| 114 | + # The user's database roles. |
| 115 | + roles: |
| 116 | + - 'dbOwner' |
| 117 | + |
| 118 | + # Change the default authentication mechanism. Valid options are: :scram, |
| 119 | + # :mongodb_cr, :mongodb_x509, and :plain. (default on 3.0 is :scram, default |
| 120 | + # on 2.4 and 2.6 is :plain) |
| 121 | + auth_mech: :scram |
| 122 | + |
| 123 | + # The database or source to authenticate the user against. (default: admin) |
| 124 | + auth_source: admin |
| 125 | + |
| 126 | + # Force the driver to connect in a specific way instead of auto- |
| 127 | + # discovering. Can be one of: :direct, :replica_set, :sharded. Set to :direct |
| 128 | + # when connecting to hidden members of a replica set. |
| 129 | + connect: :direct |
| 130 | + |
| 131 | + # Change the default time in seconds the server monitors refresh their status |
| 132 | + # via ismaster commands. (default: 10) |
| 133 | + heartbeat_frequency: 10 |
| 134 | + |
| 135 | + # The time in seconds for selecting servers for a near read preference. (default: 0.015) |
| 136 | + local_threshold: 0.015 |
| 137 | + |
| 138 | + # The timeout in seconds for selecting a server for an operation. (default: 30) |
| 139 | + server_selection_timeout: 30 |
| 140 | + |
| 141 | + # The maximum number of connections in the connection pool. (default: 5) |
| 142 | + max_pool_size: 5 |
| 143 | + |
| 144 | + # The minimum number of connections in the connection pool. (default: 1) |
| 145 | + min_pool_size: 1 |
| 146 | + |
| 147 | + # The time to wait, in seconds, in the connection pool for a connection |
| 148 | + # to be checked in before timing out. (default: 1) |
| 149 | + wait_queue_timeout: 1 |
| 150 | + |
| 151 | + # The time to wait to establish a connection before timing out, in seconds. |
| 152 | + # (default: 10) |
| 153 | + connect_timeout: 10 |
| 154 | + |
| 155 | + # The timeout to wait to execute operations on a socket before raising an error. |
| 156 | + # (default: 5) |
| 157 | + socket_timeout: 5 |
| 158 | + |
| 159 | + # The name of the replica set to connect to. Servers provided as seeds that do |
| 160 | + # not belong to this replica set will be ignored. |
| 161 | + replica_set: my_replica_set |
| 162 | + |
| 163 | + # Whether to connect to the servers via ssl. (default: false) |
| 164 | + ssl: true |
| 165 | + |
| 166 | + # The certificate file used to identify the connection against MongoDB. |
| 167 | + ssl_cert: /path/to/my.cert |
| 168 | + |
| 169 | + # The private keyfile used to identify the connection against MongoDB. |
| 170 | + # Note that even if the key is stored in the same file as the certificate, |
| 171 | + # both need to be explicitly specified. |
| 172 | + ssl_key: /path/to/my.key |
| 173 | + |
| 174 | + # A passphrase for the private key. |
| 175 | + ssl_key_pass_phrase: password |
| 176 | + |
| 177 | + # Whether or not to do peer certification validation. (default: true) |
| 178 | + ssl_verify: true |
| 179 | + |
| 180 | + # The file containing a set of concatenated certification authority certifications |
| 181 | + # used to validate certs passed from the other end of the connection. |
| 182 | + ssl_ca_cert: /path/to/ca.cert |
| 183 | + |
| 184 | + # Configure Mongoid specific options. (optional) |
| 185 | + options: |
| 186 | + # Include the root model name in json serialization. (default: false) |
| 187 | + include_root_in_json: false |
| 188 | + |
| 189 | + # Include the _type field in serialization. (default: false) |
| 190 | + include_type_for_serialization: false |
| 191 | + |
| 192 | + # Preload all models in development, needed when models use |
| 193 | + # inheritance. (default: false) |
| 194 | + preload_models: false |
| 195 | + |
| 196 | + # Raise an error when performing a #find and the document is not found. |
| 197 | + # (default: true) |
| 198 | + raise_not_found_error: true |
| 199 | + |
| 200 | + # Raise an error when defining a scope with the same name as an |
| 201 | + # existing method. (default: false) |
| 202 | + scope_overwrite_exception: false |
| 203 | + |
| 204 | + # Use Active Support's time zone in conversions. (default: true) |
| 205 | + use_activesupport_time_zone: true |
| 206 | + |
| 207 | + # Ensure all times are UTC in the app side. (default: false) |
| 208 | + use_utc: false |
| 209 | + |
| 210 | + # Set the Mongoid and Ruby driver log levels. (default: :info) |
| 211 | + log_level: :info |
| 212 | + |
| 213 | +Inline Configuration |
| 214 | +******************** |
| 215 | + |
| 216 | +It is also possible to configure Mongoid directly in Ruby, without using |
| 217 | +a configuration file. This configuration mechanism does not support defining |
| 218 | +multiple environments, however it does support defining multiple clients. |
| 219 | + |
| 220 | +.. code-block:: ruby |
| 221 | + |
| 222 | + Mongoid.configure do |config| |
| 223 | + config.clients.default = { |
| 224 | + hosts: ['localhost:27017'], |
| 225 | + database: 'my_db', |
| 226 | + } |
| 227 | + |
| 228 | + config.log_level = :warn |
| 229 | + end |
| 230 | + |
| 231 | +Logging |
| 232 | +******* |
| 233 | + |
| 234 | +Mongoid and the MongoDB Ruby driver have separate loggers and log levels. |
| 235 | + |
| 236 | +If Mongoid is loaded in the context of a Rails application, it will |
| 237 | +use Rails' configured logger by default. Otherwise Mongoid will configure |
| 238 | +itself to log to ``STDOUT`` with the default log level of ``INFO``. |
| 239 | + |
| 240 | +The Ruby driver's default log level is ``DEBUG``, and changing Mongoid's |
| 241 | +log level does not affect the Ruby driver's log level. |
| 242 | +However, if Mongoid is configured with a configuration file, the |
| 243 | +log level specified in the configuration file will be applied to both |
| 244 | +the Mongoid and the Ruby driver loggers. |
| 245 | + |
| 246 | +To change the log levels: |
| 247 | + |
| 248 | +.. code-block:: ruby |
| 249 | + |
| 250 | + # Mongoid |
| 251 | + Mongoid.logger.level = Logger::DEBUG |
| 252 | + # Ruby driver |
| 253 | + Mongo::Logger.logger.level = Logger::INFO |
| 254 | + |
| 255 | +*Note:* The Mongo client is a Ruby driver client instance, therefore |
| 256 | +the logger of a Mongo client is the Ruby driver logger, not the Mongoid |
| 257 | +logger. In other words: |
| 258 | + |
| 259 | +.. code-block:: ruby |
| 260 | + |
| 261 | + # Ruby driver logger, not Mongoid logger |
| 262 | + Mongoid.client(:default).logger == Mongo::Logger.logger |
| 263 | + |
| 264 | +Usage with Forking Servers |
| 265 | +************************** |
| 266 | + |
| 267 | +When using Mongoid with a forking web server such as Unicorn, worker processes |
| 268 | +need to recreate driver client instances to ensure that background threads |
| 269 | +monitoring the MongoDB cluster are recreated as they are not transfered into |
| 270 | +child processes through forks, and file descriptors and synchronization |
| 271 | +primitives are not shared between workers. |
| 272 | + |
| 273 | +It is recommended to not perform any operations on Mongoid models in the |
| 274 | +parent process prior to the fork. If the parent process needs to perform |
| 275 | +operations on the MongoDB database, reset all clients in an ``after_fork`` |
| 276 | +handler which is defined in ``unicorn.rb``: |
| 277 | + |
| 278 | +.. code-block:: ruby |
| 279 | + |
| 280 | + after_fork do |server, worker| |
| 281 | + Mongoid.clients.each do |name, client| |
| 282 | + client.close |
| 283 | + client.reconnect |
| 284 | + end |
| 285 | + end |
| 286 | + |
| 287 | +If the parent process does not need to perform operations on the MongoDB |
| 288 | +cluster after child processes are forked, close the client in the parent: |
| 289 | + |
| 290 | +.. code-block:: ruby |
| 291 | + |
| 292 | + before_fork do |server, worker| |
| 293 | + Mongoid.clients.each do |name, client| |
| 294 | + client.close |
| 295 | + end |
| 296 | + end |
| 297 | + |
| 298 | +*Note:* This pattern should be used with Ruby driver version 2.6.2 or higher. |
| 299 | +Previous driver versions did not recreate monitoring threads when reconnecting. |
| 300 | + |
| 301 | +*Note:* If the parent process performs operations on the Mongo client and |
| 302 | +does not close it, the parent process will continue consuming a connection slot |
| 303 | +in the cluster and will continue monitoring the cluster for as long as the |
| 304 | +parent remains alive. |
0 commit comments