@@ -168,58 +168,45 @@ name, as follows:
168
168
# class: Band
169
169
# embedded: false>
170
170
171
- Embedded Documents
172
- ==================
173
-
174
- To match values of fields of embedded documents, use the dot notation:
175
-
176
- .. code-block:: ruby
177
-
178
- Band.where('manager.name' => 'Smith')
179
- # => #<Mongoid::Criteria
180
- # selector: {"manager.name"=>"Smith"}
181
- # options: {}
182
- # class: Band
183
- # embedded: false>
184
-
185
- Band.where(:'manager.name'.ne => 'Smith')
186
- # => #<Mongoid::Criteria
187
- # selector: {"manager.name"=>{"$ne"=>"Smith"}}
188
- # options: {}
189
- # class: Band
190
- # embedded: false>
191
171
192
- .. note::
172
+ Fields
173
+ ======
193
174
194
- Queries always return top-level model instances, even if all of the
195
- conditions are referencing embedded documents.
196
-
197
- Field Types
198
- ===========
175
+ Querying on Defined Fields
176
+ --------------------------
199
177
200
178
In order to query on a field, it is not necessary to add the field to
201
179
:ref:`the model class definition <fields>`. However, if a field is defined in
202
- the model class, the type of the field is taken into account when constructing
203
- the query:
180
+ the model class, Mongoid will coerce query values to match defined field types
181
+ when constructing the query:
204
182
205
183
.. code-block:: ruby
206
184
207
- Band.where(name: 2020)
185
+ Band.where(name: 2020, founded: "2020" )
208
186
# => #<Mongoid::Criteria
209
- # selector: {"name"=>"2020"}
187
+ # selector: {"name"=>"2020", "founded"=>2020 }
210
188
# options: {}
211
189
# class: Band
212
190
# embedded: false>
213
191
214
- Band.where(founded: 2020)
192
+ Querying for Raw Values
193
+ -----------------------
194
+
195
+ If you'd like to bypass Mongoid's query type coercion behavior and query
196
+ directly for the raw-typed value in the database, wrap the query value in
197
+ ``Mongoid::RawValue`` class. This can be useful when working with legacy data.
198
+
199
+ .. code-block:: ruby
200
+
201
+ Band.where(founded: Mongoid::RawValue("2020"))
215
202
# => #<Mongoid::Criteria
216
- # selector: {"founded"=>2020}
203
+ # selector: {"founded"=>" 2020" }
217
204
# options: {}
218
205
# class: Band
219
206
# embedded: false>
220
207
221
- Aliases
222
- =======
208
+ Field Aliases
209
+ -------------
223
210
224
211
Queries take into account :ref:`storage field names <storage-field-names>`
225
212
and :ref:`field aliases <field-aliases>`:
@@ -245,6 +232,33 @@ Since `id` and `_id` fields are aliases, either one can be used for queries:
245
232
# embedded: false>
246
233
247
234
235
+ Embedded Documents
236
+ ==================
237
+
238
+ To match values of fields of embedded documents, use the dot notation:
239
+
240
+ .. code-block:: ruby
241
+
242
+ Band.where('manager.name' => 'Smith')
243
+ # => #<Mongoid::Criteria
244
+ # selector: {"manager.name"=>"Smith"}
245
+ # options: {}
246
+ # class: Band
247
+ # embedded: false>
248
+
249
+ Band.where(:'manager.name'.ne => 'Smith')
250
+ # => #<Mongoid::Criteria
251
+ # selector: {"manager.name"=>{"$ne"=>"Smith"}}
252
+ # options: {}
253
+ # class: Band
254
+ # embedded: false>
255
+
256
+ .. note::
257
+
258
+ Queries always return top-level model instances, even if all of the
259
+ conditions are referencing embedded documents.
260
+
261
+
248
262
.. _logical-operations:
249
263
250
264
Logical Operations
0 commit comments