mongoid 7.1.0.rc0 → 7.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +6 -6
- data/LICENSE +1 -1
- data/README.md +5 -5
- data/Rakefile +14 -0
- data/lib/config/locales/en.yml +5 -5
- data/lib/mongoid.rb +3 -2
- data/lib/mongoid/association/accessors.rb +37 -2
- data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
- data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
- data/lib/mongoid/association/many.rb +3 -2
- data/lib/mongoid/association/proxy.rb +6 -4
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
- data/lib/mongoid/association/referenced/eager.rb +29 -9
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -22
- data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
- data/lib/mongoid/atomic.rb +13 -3
- data/lib/mongoid/attributes.rb +28 -20
- data/lib/mongoid/clients/factory.rb +2 -2
- data/lib/mongoid/clients/options.rb +8 -8
- data/lib/mongoid/clients/sessions.rb +20 -4
- data/lib/mongoid/clients/storage_options.rb +5 -5
- data/lib/mongoid/config.rb +42 -12
- data/lib/mongoid/config/options.rb +5 -2
- data/lib/mongoid/contextual.rb +5 -4
- data/lib/mongoid/contextual/geo_near.rb +3 -2
- data/lib/mongoid/contextual/map_reduce.rb +3 -2
- data/lib/mongoid/contextual/mongo.rb +2 -1
- data/lib/mongoid/criteria.rb +23 -4
- data/lib/mongoid/criteria/modifiable.rb +2 -1
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
- data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
- data/lib/mongoid/criteria/queryable/selectable.rb +120 -13
- data/lib/mongoid/criteria/queryable/storable.rb +104 -99
- data/lib/mongoid/errors/eager_load.rb +2 -0
- data/lib/mongoid/errors/no_client_config.rb +2 -2
- data/lib/mongoid/errors/no_default_client.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +4 -2
- data/lib/mongoid/extensions/regexp.rb +1 -1
- data/lib/mongoid/fields.rb +2 -1
- data/lib/mongoid/fields/standard.rb +2 -1
- data/lib/mongoid/fields/validators/macro.rb +4 -1
- data/lib/mongoid/findable.rb +5 -4
- data/lib/mongoid/interceptable.rb +5 -1
- data/lib/mongoid/matchable/regexp.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +11 -2
- data/lib/mongoid/persistence_context.rb +6 -6
- data/lib/mongoid/query_cache.rb +61 -18
- data/lib/mongoid/railties/database.rake +7 -0
- data/lib/mongoid/serializable.rb +10 -2
- data/lib/mongoid/shardable.rb +56 -4
- data/lib/mongoid/tasks/database.rake +10 -5
- data/lib/mongoid/tasks/database.rb +83 -0
- data/lib/mongoid/timestamps/timeless.rb +3 -1
- data/lib/mongoid/validatable/uniqueness.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
- data/spec/app/models/coding.rb +4 -0
- data/spec/app/models/coding/pull_request.rb +12 -0
- data/spec/app/models/delegating_patient.rb +16 -0
- data/spec/app/models/passport.rb +1 -0
- data/spec/app/models/phone.rb +1 -0
- data/spec/app/models/publication.rb +5 -0
- data/spec/app/models/publication/encyclopedia.rb +12 -0
- data/spec/app/models/publication/review.rb +14 -0
- data/spec/integration/app_spec.rb +254 -0
- data/spec/integration/associations/embedded_spec.rb +54 -0
- data/spec/integration/associations/has_many_spec.rb +34 -0
- data/spec/integration/associations/has_one_spec.rb +34 -0
- data/spec/integration/bson_regexp_raw_spec.rb +20 -0
- data/spec/integration/criteria/date_field_spec.rb +41 -0
- data/spec/integration/criteria/logical_spec.rb +13 -0
- data/spec/integration/document_spec.rb +22 -0
- data/spec/integration/shardable_spec.rb +149 -0
- data/spec/lite_spec_helper.rb +15 -4
- data/spec/mongoid/association/accessors_spec.rb +238 -63
- data/spec/mongoid/association/embedded/embeds_many_models.rb +19 -0
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
- data/spec/mongoid/attributes_spec.rb +19 -7
- data/spec/mongoid/changeable_spec.rb +23 -0
- data/spec/mongoid/clients/factory_spec.rb +8 -8
- data/spec/mongoid/clients/options_spec.rb +11 -11
- data/spec/mongoid/clients/sessions_spec.rb +8 -4
- data/spec/mongoid/clients/transactions_spec.rb +20 -8
- data/spec/mongoid/clients_spec.rb +2 -2
- data/spec/mongoid/contextual/atomic_spec.rb +22 -11
- data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
- data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
- data/spec/mongoid/contextual/mongo_spec.rb +76 -53
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +52 -0
- data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
- data/spec/mongoid/criteria_spec.rb +36 -2
- data/spec/mongoid/document_fields_spec.rb +29 -0
- data/spec/mongoid/document_persistence_context_spec.rb +33 -0
- data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
- data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
- data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
- data/spec/mongoid/fields_spec.rb +24 -1
- data/spec/mongoid/indexable_spec.rb +6 -4
- data/spec/mongoid/interceptable_spec.rb +62 -0
- data/spec/mongoid/interceptable_spec_models.rb +76 -0
- data/spec/mongoid/matchable/default_spec.rb +1 -1
- data/spec/mongoid/matchable/regexp_spec.rb +2 -2
- data/spec/mongoid/matchable_spec.rb +2 -2
- data/spec/mongoid/persistable/pushable_spec.rb +55 -1
- data/spec/mongoid/query_cache_spec.rb +77 -9
- data/spec/mongoid/relations/proxy_spec.rb +1 -1
- data/spec/mongoid/scopable_spec.rb +2 -1
- data/spec/mongoid/serializable_spec.rb +129 -18
- data/spec/mongoid/shardable_models.rb +61 -0
- data/spec/mongoid/shardable_spec.rb +69 -16
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -31
- data/spec/support/child_process_helper.rb +76 -0
- data/spec/support/cluster_config.rb +3 -3
- data/spec/support/constraints.rb +26 -10
- data/spec/support/expectations.rb +3 -1
- data/spec/support/helpers.rb +11 -0
- data/spec/support/lite_constraints.rb +22 -0
- data/spec/support/session_registry.rb +50 -0
- data/spec/support/spec_config.rb +12 -4
- metadata +518 -480
- metadata.gz.sig +0 -0
@@ -60,7 +60,7 @@ module Mongoid
|
|
60
60
|
#
|
61
61
|
# @since 1.0.0
|
62
62
|
def __numeric__(object)
|
63
|
-
object.to_s =~ /(
|
63
|
+
object.to_s =~ /(\A[-+]?[0-9]+\z)|(\.0+\z)|(\.\z)/ ? object.to_i : Float(object)
|
64
64
|
end
|
65
65
|
|
66
66
|
# Evolve the object to an integer.
|
@@ -12,7 +12,7 @@ module Mongoid
|
|
12
12
|
# Is the object a regexp?
|
13
13
|
#
|
14
14
|
# @example Is the object a regex?
|
15
|
-
#
|
15
|
+
# /\A[123]/.regexp?
|
16
16
|
#
|
17
17
|
# @return [ true ] Always true.
|
18
18
|
#
|
@@ -24,7 +24,7 @@ module Mongoid
|
|
24
24
|
# Evolve the object into a regex.
|
25
25
|
#
|
26
26
|
# @example Evolve the object to a regex.
|
27
|
-
# Regexp.evolve("
|
27
|
+
# Regexp.evolve("\A[123]")
|
28
28
|
#
|
29
29
|
# @param [ Regexp, String ] object The object to evolve.
|
30
30
|
#
|
@@ -38,7 +38,7 @@ module Mongoid
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
module
|
41
|
+
module Raw_
|
42
42
|
|
43
43
|
# Is the object a regexp?
|
44
44
|
#
|
@@ -55,7 +55,7 @@ module Mongoid
|
|
55
55
|
# Evolve the object into a raw bson regex.
|
56
56
|
#
|
57
57
|
# @example Evolve the object to a regex.
|
58
|
-
# BSON::Regexp::Raw.evolve("
|
58
|
+
# BSON::Regexp::Raw.evolve("\\A[123]")
|
59
59
|
#
|
60
60
|
# @param [ BSON::Regexp::Raw, String ] object The object to evolve.
|
61
61
|
#
|
@@ -77,5 +77,5 @@ end
|
|
77
77
|
|
78
78
|
::Regexp.__send__(:include,Mongoid::Criteria::Queryable::Extensions::Regexp)
|
79
79
|
::Regexp.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Regexp::ClassMethods)
|
80
|
-
BSON::Regexp::Raw.__send__(:include,Mongoid::Criteria::Queryable::Extensions::Regexp::
|
81
|
-
BSON::Regexp::Raw.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Regexp::
|
80
|
+
BSON::Regexp::Raw.__send__(:include,Mongoid::Criteria::Queryable::Extensions::Regexp::Raw_)
|
81
|
+
BSON::Regexp::Raw.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Regexp::Raw_::ClassMethods)
|
@@ -9,6 +9,18 @@ module Mongoid
|
|
9
9
|
# This module contains additional time with zone behavior.
|
10
10
|
module TimeWithZone
|
11
11
|
|
12
|
+
# Evolve the time as a date, UTC midnight.
|
13
|
+
#
|
14
|
+
# @example Evolve the time to a date query format.
|
15
|
+
# time.__evolve_date__
|
16
|
+
#
|
17
|
+
# @return [ Time ] The date at midnight UTC.
|
18
|
+
#
|
19
|
+
# @since 1.0.0
|
20
|
+
def __evolve_date__
|
21
|
+
::Time.utc(year, month, day, 0, 0, 0, 0)
|
22
|
+
end
|
23
|
+
|
12
24
|
# Evolve the time into a utc time.
|
13
25
|
#
|
14
26
|
# @example Evolve the time.
|
@@ -163,7 +163,7 @@ module Mongoid
|
|
163
163
|
if expr.is_a?(Selectable)
|
164
164
|
expr = expr.selector
|
165
165
|
end
|
166
|
-
normalized =
|
166
|
+
normalized = _mongoid_expand_keys(expr)
|
167
167
|
sel.store(operator, result_criteria.push(normalized))
|
168
168
|
end
|
169
169
|
end
|
@@ -190,9 +190,9 @@ module Mongoid
|
|
190
190
|
sel = query.selector
|
191
191
|
_mongoid_flatten_arrays(criteria).each do |criterion|
|
192
192
|
if criterion.is_a?(Selectable)
|
193
|
-
expr =
|
193
|
+
expr = _mongoid_expand_keys(criterion.selector)
|
194
194
|
else
|
195
|
-
expr = criterion
|
195
|
+
expr = _mongoid_expand_keys(criterion)
|
196
196
|
end
|
197
197
|
if sel.empty?
|
198
198
|
sel.store(operator, [expr])
|
@@ -212,7 +212,7 @@ module Mongoid
|
|
212
212
|
# explicitly only expands Array objects and Array subclasses.
|
213
213
|
private def _mongoid_flatten_arrays(array)
|
214
214
|
out = []
|
215
|
-
pending = array
|
215
|
+
pending = array.dup
|
216
216
|
until pending.empty?
|
217
217
|
item = pending.shift
|
218
218
|
if item.nil?
|
@@ -226,11 +226,78 @@ module Mongoid
|
|
226
226
|
out
|
227
227
|
end
|
228
228
|
|
229
|
-
#
|
230
|
-
|
231
|
-
|
232
|
-
|
229
|
+
# Takes a criteria hash and expands Key objects into hashes containing
|
230
|
+
# MQL corresponding to said key objects.
|
231
|
+
#
|
232
|
+
# Ruby does not permit multiple symbol operators. For example,
|
233
|
+
# {:foo.gt => 1, :foo.gt => 2} is collapsed to {:foo.gt => 2} by the
|
234
|
+
# language. Therefore this method never has to deal with multiple
|
235
|
+
# identical operators.
|
236
|
+
#
|
237
|
+
# Similarly, this method should never need to expand a literal value
|
238
|
+
# and an operator at the same time.
|
239
|
+
#
|
240
|
+
# @param [ Hash ] Criteria including Key instances.
|
241
|
+
#
|
242
|
+
# @return [ Hash ] Expanded criteria.
|
243
|
+
private def _mongoid_expand_keys(expr)
|
244
|
+
unless expr.is_a?(Hash)
|
245
|
+
raise ArgumentError, 'Argument must be a Hash'
|
246
|
+
end
|
247
|
+
|
248
|
+
result = {}
|
249
|
+
expr.each do |field, value|
|
250
|
+
field.__expr_part__(value.__expand_complex__).each do |k, v|
|
251
|
+
if result[k]
|
252
|
+
if result[k].is_a?(Hash)
|
253
|
+
# Existing value is an operator.
|
254
|
+
# If new value is also an operator, ensure there are no
|
255
|
+
# conflicts and add
|
256
|
+
if v.is_a?(Hash)
|
257
|
+
# The new value is also an operator.
|
258
|
+
# If there are no conflicts, combine the hashes, otherwise
|
259
|
+
# add new conditions to top level with $and.
|
260
|
+
if (v.keys & result[k].keys).empty?
|
261
|
+
result[k].update(v)
|
262
|
+
else
|
263
|
+
raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
|
264
|
+
result['$and'] ||= []
|
265
|
+
result['$and'] << {k => v}
|
266
|
+
end
|
267
|
+
else
|
268
|
+
# The new value is a simple value.
|
269
|
+
# If there isn't an $eq operator already in the query,
|
270
|
+
# transform the new value into an $eq operator and add it
|
271
|
+
# to the existing hash. Otherwise add the new condition
|
272
|
+
# with $and to the top level.
|
273
|
+
if result[k].key?('$eq')
|
274
|
+
raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
|
275
|
+
result['$and'] ||= []
|
276
|
+
result['$and'] << {k => v}
|
277
|
+
else
|
278
|
+
result[k].update('$eq' => v)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
else
|
282
|
+
# Existing value is a simple value.
|
283
|
+
# If we are adding an operator, and the operator is not $eq,
|
284
|
+
# convert existing value into $eq and add the new operator
|
285
|
+
# to the same hash. Otherwise add the new condition with $and
|
286
|
+
# to the top level.
|
287
|
+
if v.is_a?(Hash) && !v.key?('$eq')
|
288
|
+
result[k] = {'$eq' => result[k]}.update(v)
|
289
|
+
else
|
290
|
+
raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
|
291
|
+
result['$and'] ||= []
|
292
|
+
result['$and'] << {k => v}
|
293
|
+
end
|
294
|
+
end
|
295
|
+
else
|
296
|
+
result[k] = v
|
297
|
+
end
|
298
|
+
end
|
233
299
|
end
|
300
|
+
result
|
234
301
|
end
|
235
302
|
|
236
303
|
# Adds the criterion to the existing selection.
|
@@ -33,7 +33,7 @@ module Mongoid
|
|
33
33
|
# Add a group operation to the aggregation pipeline.
|
34
34
|
#
|
35
35
|
# @example Add a group operation.
|
36
|
-
# pipeline.group(:count.sum => 1, :max.max => "likes")
|
36
|
+
# pipeline.group(:_id => "foo", :count.sum => 1, :max.max => "likes")
|
37
37
|
#
|
38
38
|
# @param [ Hash ] entry The group entry.
|
39
39
|
#
|
@@ -76,7 +76,8 @@ module Mongoid
|
|
76
76
|
# pipeline.unwind(:field)
|
77
77
|
# pipeline.unwind(document)
|
78
78
|
#
|
79
|
-
# @param [ String, Symbol, Hash ] field_or_doc
|
79
|
+
# @param [ String, Symbol, Hash ] field_or_doc A field name or a
|
80
|
+
# document.
|
80
81
|
#
|
81
82
|
# @return [ Pipeline ] The pipeline.
|
82
83
|
#
|
@@ -89,11 +89,27 @@ module Mongoid
|
|
89
89
|
if new_s.is_a?(Selectable)
|
90
90
|
new_s = new_s.selector
|
91
91
|
end
|
92
|
-
normalized =
|
92
|
+
normalized = _mongoid_expand_keys(new_s)
|
93
93
|
normalized.each do |k, v|
|
94
94
|
k = k.to_s
|
95
|
-
if c.selector[k]
|
96
|
-
|
95
|
+
if c.selector[k]
|
96
|
+
# There is already a condition on k.
|
97
|
+
# If v is an operator, and all existing conditions are
|
98
|
+
# also operators, and v isn't present in existing conditions,
|
99
|
+
# we can add to existing conditions.
|
100
|
+
# Otherwise use $and.
|
101
|
+
if v.is_a?(Hash) &&
|
102
|
+
v.length == 1 &&
|
103
|
+
(new_k = v.keys.first).start_with?('$') &&
|
104
|
+
(existing_kv = c.selector[k]).is_a?(Hash) &&
|
105
|
+
!existing_kv.key?(new_k) &&
|
106
|
+
existing_kv.keys.all? { |sub_k| sub_k.start_with?('$') }
|
107
|
+
then
|
108
|
+
merged_v = c.selector[k].merge(v)
|
109
|
+
c.selector.store(k, merged_v)
|
110
|
+
else
|
111
|
+
c = c.send(:__multi__, [k => v], '$and')
|
112
|
+
end
|
97
113
|
else
|
98
114
|
c.selector.store(k, v)
|
99
115
|
end
|
@@ -567,17 +583,21 @@ module Mongoid
|
|
567
583
|
if new_s.is_a?(Selectable)
|
568
584
|
new_s = new_s.selector
|
569
585
|
end
|
570
|
-
new_s.each do |k, v|
|
586
|
+
_mongoid_expand_keys(new_s).each do |k, v|
|
571
587
|
k = k.to_s
|
572
588
|
if c.selector[k] || k[0] == ?$
|
573
589
|
c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
|
574
590
|
else
|
575
|
-
if v.is_a?(
|
576
|
-
|
591
|
+
if v.is_a?(Hash)
|
592
|
+
c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
|
577
593
|
else
|
578
|
-
|
594
|
+
if v.is_a?(Regexp)
|
595
|
+
negated_operator = '$not'
|
596
|
+
else
|
597
|
+
negated_operator = '$ne'
|
598
|
+
end
|
599
|
+
c = c.send(:__override__, {k => v}, negated_operator)
|
579
600
|
end
|
580
|
-
c = c.send(:__override__, {k => v}, negated_operator)
|
581
601
|
end
|
582
602
|
end
|
583
603
|
c
|
@@ -586,13 +606,34 @@ module Mongoid
|
|
586
606
|
end
|
587
607
|
key :not, :override, "$not"
|
588
608
|
|
589
|
-
#
|
609
|
+
# Creates a disjunction using $or from the existing criteria in the
|
610
|
+
# receiver and the provided arguments.
|
611
|
+
#
|
612
|
+
# This behavior (receiver becoming one of the disjunction operands)
|
613
|
+
# matches ActiveRecord's +or+ behavior.
|
614
|
+
#
|
615
|
+
# Use +any_of+ to add a disjunction of the arguments as an additional
|
616
|
+
# constraint to the criteria already existing in the receiver.
|
617
|
+
#
|
618
|
+
# Each argument can be a Hash, a Criteria object, an array of
|
619
|
+
# Hash or Criteria objects, or a nested array. Nested arrays will be
|
620
|
+
# flattened and can be of any depth. Passing arrays is deprecated.
|
590
621
|
#
|
591
|
-
# @example Add the $or selection.
|
622
|
+
# @example Add the $or selection where both fields must have the specified values.
|
592
623
|
# selectable.or(field: 1, field: 2)
|
593
624
|
#
|
594
|
-
# @
|
595
|
-
#
|
625
|
+
# @example Add the $or selection where either value match is sufficient.
|
626
|
+
# selectable.or({field: 1}, {field: 2})
|
627
|
+
#
|
628
|
+
# @example Same as previous example but using the deprecated array wrap.
|
629
|
+
# selectable.or([{field: 1}, {field: 2}])
|
630
|
+
#
|
631
|
+
# @example Same as previous example, also deprecated.
|
632
|
+
# selectable.or([{field: 1}], [{field: 2}])
|
633
|
+
#
|
634
|
+
# @param [ Hash | Criteria | Array<Hash | Criteria>, ... ] criteria
|
635
|
+
# Multiple key/value pair matches or Criteria objects, or arrays
|
636
|
+
# thereof. Passing arrays is deprecated.
|
596
637
|
#
|
597
638
|
# @return [ Selectable ] The new selectable.
|
598
639
|
#
|
@@ -600,7 +641,73 @@ module Mongoid
|
|
600
641
|
def or(*criteria)
|
601
642
|
_mongoid_add_top_level_operation('$or', criteria)
|
602
643
|
end
|
603
|
-
|
644
|
+
|
645
|
+
# Adds a disjunction of the arguments as an additional constraint
|
646
|
+
# to the criteria already existing in the receiver.
|
647
|
+
#
|
648
|
+
# Use +or+ to make the receiver one of the disjunction operands.
|
649
|
+
#
|
650
|
+
# Each argument can be a Hash, a Criteria object, an array of
|
651
|
+
# Hash or Criteria objects, or a nested array. Nested arrays will be
|
652
|
+
# flattened and can be of any depth. Passing arrays is deprecated.
|
653
|
+
#
|
654
|
+
# @example Add the $or selection where both fields must have the specified values.
|
655
|
+
# selectable.any_of(field: 1, field: 2)
|
656
|
+
#
|
657
|
+
# @example Add the $or selection where either value match is sufficient.
|
658
|
+
# selectable.any_of({field: 1}, {field: 2})
|
659
|
+
#
|
660
|
+
# @example Same as previous example but using the deprecated array wrap.
|
661
|
+
# selectable.any_of([{field: 1}, {field: 2}])
|
662
|
+
#
|
663
|
+
# @example Same as previous example, also deprecated.
|
664
|
+
# selectable.any_of([{field: 1}], [{field: 2}])
|
665
|
+
#
|
666
|
+
# @param [ Hash | Criteria | Array<Hash | Criteria>, ... ] criteria
|
667
|
+
# Multiple key/value pair matches or Criteria objects, or arrays
|
668
|
+
# thereof. Passing arrays is deprecated.
|
669
|
+
#
|
670
|
+
# @return [ Selectable ] The new selectable.
|
671
|
+
#
|
672
|
+
# @since 1.0.0
|
673
|
+
def any_of(*criteria)
|
674
|
+
criteria = _mongoid_flatten_arrays(criteria)
|
675
|
+
case criteria.length
|
676
|
+
when 0
|
677
|
+
clone
|
678
|
+
when 1
|
679
|
+
# When we have a single criteria, any_of behaves like and.
|
680
|
+
# Note: criteria can be a Query object, which #where method does
|
681
|
+
# not support.
|
682
|
+
self.and(*criteria)
|
683
|
+
else
|
684
|
+
# When we have multiple criteria, combine them all with $or
|
685
|
+
# and add the result to self.
|
686
|
+
exprs = criteria.map do |criterion|
|
687
|
+
if criterion.is_a?(Selectable)
|
688
|
+
_mongoid_expand_keys(criterion.selector)
|
689
|
+
else
|
690
|
+
Hash[criterion.map do |k, v|
|
691
|
+
if k.is_a?(Symbol)
|
692
|
+
[k.to_s, v]
|
693
|
+
else
|
694
|
+
[k, v]
|
695
|
+
end
|
696
|
+
end]
|
697
|
+
end
|
698
|
+
end
|
699
|
+
# Should be able to do:
|
700
|
+
#where('$or' => exprs)
|
701
|
+
# But since that is broken do instead:
|
702
|
+
clone.tap do |query|
|
703
|
+
if query.selector['$or']
|
704
|
+
query.selector.store('$or', query.selector['$or'] + exprs)
|
705
|
+
else
|
706
|
+
query.selector.store('$or', exprs)
|
707
|
+
end
|
708
|
+
end
|
709
|
+
end
|
710
|
+
end
|
604
711
|
|
605
712
|
# Add a $size selection for array fields.
|
606
713
|
#
|
@@ -17,67 +17,49 @@ module Mongoid
|
|
17
17
|
# @api private
|
18
18
|
module Storable
|
19
19
|
|
20
|
-
# Adds
|
21
|
-
#
|
22
|
-
# This method takes the operator and the operator value expression
|
23
|
-
# separately for callers' convenience. It can be considered to
|
24
|
-
# handle storing the hash `{operator => op_expr}`.
|
25
|
-
#
|
26
|
-
# If the selector already has the specified operator in it (on the
|
27
|
-
# top level), the new condition given in op_expr is added to the
|
28
|
-
# existing conditions for the specified operator. This is
|
29
|
-
# straightforward for $and; for other logical operators, the behavior
|
30
|
-
# of this method is to add the new conditions to the existing operator.
|
31
|
-
# For example, if the selector is currently:
|
32
|
-
#
|
33
|
-
# {'foo' => 'bar', '$or' => [{'hello' => 'world'}]}
|
34
|
-
#
|
35
|
-
# ... and operator is '$or' and op_expr is `{'test' => 123'}`,
|
36
|
-
# the resulting selector will be:
|
37
|
-
#
|
38
|
-
# {'foo' => 'bar', '$or' => [{'hello' => 'world'}, {'test' => 123}]}
|
20
|
+
# Adds a field expression to the query.
|
39
21
|
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# assumption that the existing selector is the correct left hand side
|
44
|
-
# of the operation already.
|
22
|
+
# +field+ must be a field name, and it must be a string. The upstream
|
23
|
+
# code must have converted other field/key types to the simple string
|
24
|
+
# form by the time this method is invoked.
|
45
25
|
#
|
46
|
-
#
|
47
|
-
# there already is a top-level operator with the same name, the
|
48
|
-
# op_expr is added to the selector via a top-level $and operator,
|
49
|
-
# thus producing a selector having both operator values.
|
26
|
+
# +value+ can be of any type, it is written into the selector unchanged.
|
50
27
|
#
|
51
|
-
# This method
|
52
|
-
# currently empty and operator is $and, op_expr is written to the
|
53
|
-
# selector with $and even if the $and can in principle be elided).
|
28
|
+
# This method performs no processing on the provided field value.
|
54
29
|
#
|
55
|
-
#
|
30
|
+
# Mutates the receiver.
|
56
31
|
#
|
57
|
-
# @param [ String ]
|
58
|
-
# @param [
|
32
|
+
# @param [ String ] field The field name.
|
33
|
+
# @param [ Object ] value The field value.
|
59
34
|
#
|
60
35
|
# @return [ Storable ] self.
|
61
|
-
def
|
62
|
-
unless
|
63
|
-
raise ArgumentError, "
|
64
|
-
end
|
65
|
-
|
66
|
-
unless operator[0] == ?$
|
67
|
-
raise ArgumentError, "Operator must begin with $: #{operator}"
|
36
|
+
def add_field_expression(field, value)
|
37
|
+
unless field.is_a?(String)
|
38
|
+
raise ArgumentError, "Field must be a string: #{field}"
|
68
39
|
end
|
69
40
|
|
70
|
-
if
|
71
|
-
|
41
|
+
if field[0] == ?$
|
42
|
+
raise ArgumentError, "Field cannot be an operator (i.e. begin with $): #{field}"
|
72
43
|
end
|
73
44
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
45
|
+
if selector[field]
|
46
|
+
# We already have a restriction by the field we are trying
|
47
|
+
# to restrict, combine the restrictions.
|
48
|
+
if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
|
49
|
+
value.keys.all? { |key|
|
50
|
+
key_s = key.to_s
|
51
|
+
key_s[0] == ?$ && !selector[field].key?(key_s)
|
52
|
+
}
|
53
|
+
then
|
54
|
+
# Multiple operators can be combined on the same field by
|
55
|
+
# adding them to the existing hash.
|
56
|
+
new_value = selector[field].merge(value)
|
57
|
+
selector.store(field, new_value)
|
58
|
+
else
|
59
|
+
add_operator_expression('$and', [{field => value}])
|
60
|
+
end
|
79
61
|
else
|
80
|
-
selector.store(
|
62
|
+
selector.store(field, value)
|
81
63
|
end
|
82
64
|
|
83
65
|
self
|
@@ -87,41 +69,40 @@ module Mongoid
|
|
87
69
|
#
|
88
70
|
# This method only handles logical operators ($and, $nor and $or).
|
89
71
|
# It raises ArgumentError if called with another operator. Note that
|
90
|
-
# in
|
91
|
-
# $not is not handled by this method
|
72
|
+
# in MQL, $not is a field-level operator and not a query-level one,
|
73
|
+
# and therefore $not is not handled by this method.
|
92
74
|
#
|
93
75
|
# This method takes the operator and the operator value expression
|
94
76
|
# separately for callers' convenience. It can be considered to
|
95
77
|
# handle storing the hash `{operator => op_expr}`.
|
96
78
|
#
|
97
|
-
# If the selector
|
98
|
-
# top level), the new condition given in op_expr is
|
99
|
-
# existing conditions for the specified operator.
|
100
|
-
# straightforward for $and; for other logical operators, the behavior
|
101
|
-
# of this method is to add the new conditions to the existing operator.
|
79
|
+
# If the selector consists of a single condition which is the specified
|
80
|
+
# operator (on the top level), the new condition given in op_expr is
|
81
|
+
# added to the existing conditions for the specified operator.
|
102
82
|
# For example, if the selector is currently:
|
103
83
|
#
|
104
|
-
# {'
|
84
|
+
# {'$or' => [{'hello' => 'world'}]}
|
105
85
|
#
|
106
|
-
# ... and operator is '$or' and op_expr is `{'test' => 123'}`,
|
86
|
+
# ... and operator is '$or' and op_expr is `[{'test' => 123'}]`,
|
107
87
|
# the resulting selector will be:
|
108
88
|
#
|
109
|
-
# {'
|
89
|
+
# {'$or' => [{'hello' => 'world'}, {'test' => 123}]}
|
110
90
|
#
|
111
|
-
# This
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
# of the operation already.
|
91
|
+
# This method always adds the new conditions as additional requirements;
|
92
|
+
# in other words, it does not implement the ActiveRecord or/nor behavior
|
93
|
+
# where the receiver becomes one of the operands. It is expected that
|
94
|
+
# code upstream of this method implements such behavior.
|
116
95
|
#
|
117
96
|
# This method does not simplify values (i.e. if the selector is
|
118
97
|
# currently empty and operator is $and, op_expr is written to the
|
119
98
|
# selector with $and even if the $and can in principle be elided).
|
99
|
+
# Such simplification is also expected to have already been performed
|
100
|
+
# by the upstream code.
|
120
101
|
#
|
121
102
|
# This method mutates the receiver.
|
122
103
|
#
|
123
104
|
# @param [ String ] operator The operator to add.
|
124
|
-
# @param [ Hash ] op_expr Operator value to add.
|
105
|
+
# @param [ Array<Hash> ] op_expr Operator value to add.
|
125
106
|
#
|
126
107
|
# @return [ Storable ] self.
|
127
108
|
def add_logical_operator_expression(operator, op_expr)
|
@@ -145,56 +126,80 @@ module Mongoid
|
|
145
126
|
# with whatever other conditions exist.
|
146
127
|
selector.store(operator, op_expr)
|
147
128
|
else
|
148
|
-
# Other operators need to
|
149
|
-
|
150
|
-
|
151
|
-
|
129
|
+
# Other operators need to be added separately
|
130
|
+
if selector[operator]
|
131
|
+
add_logical_operator_expression('$and', [operator => op_expr])
|
132
|
+
else
|
133
|
+
selector.store(operator, op_expr)
|
134
|
+
end
|
152
135
|
end
|
153
136
|
|
154
137
|
self
|
155
138
|
end
|
156
139
|
|
157
|
-
# Adds
|
140
|
+
# Adds an operator expression to the selector.
|
158
141
|
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
142
|
+
# This method takes the operator and the operator value expression
|
143
|
+
# separately for callers' convenience. It can be considered to
|
144
|
+
# handle storing the hash `{operator => op_expr}`.
|
162
145
|
#
|
163
|
-
#
|
146
|
+
# The operator value can be of any type.
|
164
147
|
#
|
165
|
-
#
|
148
|
+
# If the selector already has the specified operator in it (on the
|
149
|
+
# top level), the new condition given in op_expr is added to the
|
150
|
+
# existing conditions for the specified operator. This is
|
151
|
+
# straightforward for $and; for other logical operators, the behavior
|
152
|
+
# of this method is to add the new conditions to the existing operator.
|
153
|
+
# For example, if the selector is currently:
|
166
154
|
#
|
167
|
-
#
|
168
|
-
#
|
155
|
+
# {'foo' => 'bar', '$or' => [{'hello' => 'world'}]}
|
156
|
+
#
|
157
|
+
# ... and operator is '$or' and op_expr is `{'test' => 123'}`,
|
158
|
+
# the resulting selector will be:
|
159
|
+
#
|
160
|
+
# {'foo' => 'bar', '$or' => [{'hello' => 'world'}, {'test' => 123}]}
|
161
|
+
#
|
162
|
+
# This does not implement an OR between the existing selector and the
|
163
|
+
# new operator expression - handling this is the job of upstream
|
164
|
+
# methods. This method simply stores op_expr into the selector on the
|
165
|
+
# assumption that the existing selector is the correct left hand side
|
166
|
+
# of the operation already.
|
167
|
+
#
|
168
|
+
# For non-logical query-level operators like $where and $text, if
|
169
|
+
# there already is a top-level operator with the same name, the
|
170
|
+
# op_expr is added to the selector via a top-level $and operator,
|
171
|
+
# thus producing a selector having both operator values.
|
172
|
+
#
|
173
|
+
# This method does not simplify values (i.e. if the selector is
|
174
|
+
# currently empty and operator is $and, op_expr is written to the
|
175
|
+
# selector with $and even if the $and can in principle be elided).
|
176
|
+
#
|
177
|
+
# This method mutates the receiver.
|
178
|
+
#
|
179
|
+
# @param [ String ] operator The operator to add.
|
180
|
+
# @param [ Object ] op_expr Operator value to add.
|
169
181
|
#
|
170
182
|
# @return [ Storable ] self.
|
171
|
-
def
|
172
|
-
unless
|
173
|
-
raise ArgumentError, "
|
183
|
+
def add_operator_expression(operator, op_expr)
|
184
|
+
unless operator.is_a?(String)
|
185
|
+
raise ArgumentError, "Operator must be a string: #{operator}"
|
174
186
|
end
|
175
187
|
|
176
|
-
|
177
|
-
raise ArgumentError, "
|
188
|
+
unless operator[0] == ?$
|
189
|
+
raise ArgumentError, "Operator must begin with $: #{operator}"
|
178
190
|
end
|
179
191
|
|
180
|
-
if
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
# Multiple operators can be combined on the same field by
|
190
|
-
# adding them to the existing hash.
|
191
|
-
new_value = selector[field].merge(value)
|
192
|
-
selector.store(field, new_value)
|
193
|
-
else
|
194
|
-
add_operator_expression('$and', [{field => value}])
|
195
|
-
end
|
192
|
+
if %w($and $nor $or).include?(operator)
|
193
|
+
return add_logical_operator_expression(operator, op_expr)
|
194
|
+
end
|
195
|
+
|
196
|
+
# For other operators, if the operator already exists in the
|
197
|
+
# query, add the new condition with $and, otherwise add the
|
198
|
+
# new condition to the top level.
|
199
|
+
if selector[operator]
|
200
|
+
add_logical_operator_expression('$and', [{operator => op_expr}])
|
196
201
|
else
|
197
|
-
selector.store(
|
202
|
+
selector.store(operator, op_expr)
|
198
203
|
end
|
199
204
|
|
200
205
|
self
|