|
| 1 | +.. _indexes: |
| 2 | + |
| 3 | +**************** |
| 4 | +Index Management |
| 5 | +**************** |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Specifying Indexes |
| 16 | +================== |
| 17 | + |
| 18 | +You can define indexes on documents using the index macro. Provide the key for |
| 19 | +the index along with a direction. Additional options can be supplied in the |
| 20 | +second options hash parameter: |
| 21 | + |
| 22 | +.. code-block:: ruby |
| 23 | + |
| 24 | + class Person |
| 25 | + include Mongoid::Document |
| 26 | + field :ssn |
| 27 | + |
| 28 | + index({ ssn: 1 }, { unique: true, name: "ssn_index" }) |
| 29 | + end |
| 30 | + |
| 31 | +You can define indexes on embedded document fields as well: |
| 32 | + |
| 33 | +.. code-block:: ruby |
| 34 | + |
| 35 | + class Person |
| 36 | + include Mongoid::Document |
| 37 | + embeds_many :addresses |
| 38 | + index "addresses.street" => 1 |
| 39 | + end |
| 40 | + |
| 41 | +You can index on multiple fields and provide direction: |
| 42 | + |
| 43 | +.. code-block:: ruby |
| 44 | + |
| 45 | + class Person |
| 46 | + include Mongoid::Document |
| 47 | + field :first_name |
| 48 | + field :last_name |
| 49 | + |
| 50 | + index({ first_name: 1, last_name: 1 }, { unique: true }) |
| 51 | + end |
| 52 | + |
| 53 | +Indexes can be sparse: |
| 54 | + |
| 55 | +.. code-block:: ruby |
| 56 | + |
| 57 | + class Person |
| 58 | + include Mongoid::Document |
| 59 | + field :ssn |
| 60 | + |
| 61 | + index({ ssn: -1 }, { sparse: true }) |
| 62 | + end |
| 63 | + |
| 64 | +For geospatial indexes, make sure the field being indexed is of type Array: |
| 65 | + |
| 66 | +.. code-block:: ruby |
| 67 | + |
| 68 | + class Person |
| 69 | + include Mongoid::Document |
| 70 | + field :location, type: Array |
| 71 | + |
| 72 | + index({ location: "2d" }, { min: -200, max: 200 }) |
| 73 | + end |
| 74 | + |
| 75 | +Indexes can be scoped to a specific database: |
| 76 | + |
| 77 | +.. code-block:: ruby |
| 78 | + |
| 79 | + class Person |
| 80 | + include Mongoid::Document |
| 81 | + field :ssn |
| 82 | + index({ ssn: 1 }, { database: "users", unique: true, background: true }) |
| 83 | + end |
| 84 | + |
| 85 | +You may use aliased field names in index definitions. Field aliases |
| 86 | +will also be resolved on the following options: ``partial_filter_expression``, |
| 87 | +``weights``, ``wildcard_projection``. |
| 88 | + |
| 89 | +.. code-block:: ruby |
| 90 | + |
| 91 | + class Person |
| 92 | + include Mongoid::Document |
| 93 | + field :a, as: :age |
| 94 | + index({ age: 1 }, { partial_filter_expression: { age: { '$gte' => 20 } }) |
| 95 | + end |
| 96 | + |
| 97 | +.. note:: |
| 98 | + |
| 99 | + The expansion of field name aliases in index options such as |
| 100 | + ``partial_filter_expression`` is performed according to the behavior of MongoDB |
| 101 | + server 6.0. Future server versions may change how they interpret these options, |
| 102 | + and Mongoid's functionality may not support such changes. |
| 103 | + |
| 104 | +Mongoid can define indexes on "foreign key" fields for associations. |
| 105 | +This only works on the association macro that the foreign key is stored on: |
| 106 | + |
| 107 | +.. code-block:: ruby |
| 108 | + |
| 109 | + class Comment |
| 110 | + include Mongoid::Document |
| 111 | + belongs_to :post, index: true |
| 112 | + has_and_belongs_to_many :preferences, index: true |
| 113 | + end |
| 114 | + |
| 115 | +*Deprecated:* In MongoDB 4.0 and earlier, users could control whether to build indexes |
| 116 | +in the foreground (blocking) or background (non-blocking, but less efficient) using the |
| 117 | +``background`` option. |
| 118 | + |
| 119 | +.. code-block:: ruby |
| 120 | + |
| 121 | + class Person |
| 122 | + include Mongoid::Document |
| 123 | + field :ssn |
| 124 | + index({ ssn: 1 }, { unique: true, background: true }) |
| 125 | + end |
| 126 | + |
| 127 | +The default value of ``background`` is controlled by Mongoid's |
| 128 | +``background_indexing`` :ref:`configuration option <configuration-options>`. |
| 129 | + |
| 130 | +The ``background`` option has `no effect as of MongoDB 4.2 |
| 131 | +<https://www.mongodb.com/docs/manual/core/index-creation/#comparison-to-foreground-and-background-builds>`_. |
| 132 | + |
| 133 | + |
| 134 | +Specifying Search Indexes on MongoDB Atlas |
| 135 | +========================================== |
| 136 | + |
| 137 | +If your application is connected to MongoDB Atlas, you can declare and manage |
| 138 | +search indexes on your models. (This feature is only available on MongoDB |
| 139 | +Atlas.) |
| 140 | + |
| 141 | +To declare a search index, use the ``search_index`` macro in your model: |
| 142 | + |
| 143 | +.. code-block:: ruby |
| 144 | + |
| 145 | + class Message |
| 146 | + include Mongoid::Document |
| 147 | + |
| 148 | + search_index { ... } |
| 149 | + search_index :named_index, { ... } |
| 150 | + end |
| 151 | + |
| 152 | +Search indexes may be given an explicit name; this is necessary if you have |
| 153 | +more than one search index on a model. |
| 154 | + |
| 155 | + |
| 156 | +Index Management Rake Tasks |
| 157 | +=========================== |
| 158 | + |
| 159 | +When you want to create the indexes in the database, use the provided |
| 160 | +``db:mongoid:create_indexes`` Rake task: |
| 161 | + |
| 162 | +.. code-block:: bash |
| 163 | + |
| 164 | + $ rake db:mongoid:create_indexes |
| 165 | + |
| 166 | +Mongoid also provides a Rake task to delete all secondary indexes. |
| 167 | + |
| 168 | +.. code-block:: bash |
| 169 | + |
| 170 | + $ rake db:mongoid:remove_indexes |
| 171 | + |
| 172 | +Note: the output of these Rake tasks goes to the default logger configured |
| 173 | +by Rails. This is usually a file like ``log/development.log`` and not standard |
| 174 | +output. |
| 175 | + |
| 176 | +These create/remove indexes commands also works for just one model by running |
| 177 | +in Rails console: |
| 178 | + |
| 179 | +.. code-block:: ruby |
| 180 | + |
| 181 | + # Create indexes for Model |
| 182 | + Model.create_indexes |
| 183 | + |
| 184 | + # Remove indexes for Model |
| 185 | + Model.remove_indexes |
| 186 | + |
| 187 | +Managing Search Indexes on MongoDB Atlas |
| 188 | +---------------------------------------- |
| 189 | + |
| 190 | +If you have defined search indexes on your model, there are rake tasks available |
| 191 | +for creating and removing those search indexes: |
| 192 | + |
| 193 | +.. code-block:: bash |
| 194 | + |
| 195 | + $ rake db:mongoid:create_search_indexes |
| 196 | + $ rake db:mongoid:remove_search_indexes |
| 197 | + |
| 198 | +By default, creating search indexes will wait for the indexes to be created, |
| 199 | +which can take quite some time. If you want to simply let the database create |
| 200 | +the indexes in the background, you can set the ``WAIT_FOR_SEARCH_INDEXES`` |
| 201 | +environment variable to 0, like this: |
| 202 | + |
| 203 | +.. code-block:: bash |
| 204 | + |
| 205 | + $ rake WAIT_FOR_SEARCH_INDEXES=0 db:mongoid:create_search_indexes |
| 206 | + |
| 207 | +Note that the task for removing search indexes will remove all search indexes |
| 208 | +from all models, and should be used with caution. |
| 209 | + |
| 210 | +You can also add and remove search indexes for a single model by invoking the |
| 211 | +following in a Rails console: |
| 212 | + |
| 213 | +.. code-block:: ruby |
| 214 | + |
| 215 | + # Create all defined search indexes on the model; this will return |
| 216 | + # immediately and the indexes will be created in the background. |
| 217 | + Model.create_search_indexes |
| 218 | + |
| 219 | + # Remove all search indexes from the model |
| 220 | + Model.remove_search_indexes |
| 221 | + |
| 222 | + # Enumerate all search indexes on the model |
| 223 | + Model.search_indexes.each { |index| ... } |
| 224 | + |
| 225 | + |
| 226 | +Telling Mongoid Where to Look For Models |
| 227 | +---------------------------------------- |
| 228 | + |
| 229 | +For non-Rails applications, Mongoid's rake tasks will look for models in |
| 230 | +``./app/models`` and ``./lib/models``. For Rails, Mongoid will look in |
| 231 | +``./app/models`` (or wherever you've configured Rails to look for models). If |
| 232 | +your models are in another location, you will need to tell Mongoid where to |
| 233 | +look for them with ``Mongoid.model_paths=``. You can do this by setting it |
| 234 | +in your application's Rakefile: |
| 235 | + |
| 236 | +.. code-block:: ruby |
| 237 | + |
| 238 | + # Rakefile |
| 239 | + |
| 240 | + # keep the defaults, but add more paths to look for models |
| 241 | + Mongoid.model_paths += [ "./src/models", "./lib/documents" ] |
| 242 | + |
| 243 | + # or, override the defaults entirely |
| 244 | + Mongoid.model_paths = [ "./src/models", "./lib/documents" ] |
| 245 | + |
| 246 | +Make sure that these paths are in your application's load path, as well. For |
| 247 | +example: |
| 248 | + |
| 249 | +.. code-block:: ruby |
| 250 | + |
| 251 | + # Rakefile |
| 252 | + |
| 253 | + $LOAD_PATHS.concat [ "./src/models", "./lib/documents" ] |
| 254 | + |
| 255 | + |
| 256 | +Using Rake Tasks With Non-Rails Applications |
| 257 | +-------------------------------------------- |
| 258 | + |
| 259 | +Mongoid's Rake tasks are automatically loaded in Rails applications using |
| 260 | +Mongoid. When using Mongoid with a non-Rails application, these tasks must |
| 261 | +be loaded manually: |
| 262 | + |
| 263 | +.. code-block:: ruby |
| 264 | + |
| 265 | + # Rakefile |
| 266 | + |
| 267 | + require 'mongoid' |
| 268 | + load 'mongoid/tasks/database.rake' |
| 269 | + |
| 270 | +If your application uses Bundler, you can require ``bundler/setup`` instead of |
| 271 | +explicitly requiring ``mongoid``: |
| 272 | + |
| 273 | +.. code-block:: ruby |
| 274 | + |
| 275 | + # Rakefile |
| 276 | + |
| 277 | + require 'bundler/setup' |
| 278 | + load 'mongoid/tasks/database.rake' |
0 commit comments