Class: Parse::Constraint::ArraySubsetOfConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Parse::Constraint::ArraySubsetOfConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
This constraint uses MongoDB aggregation pipeline with $setIsSubset. While $expr expressions cannot utilize field indexes, aggregation enables set operations not available in standard Parse queries.
Subset constraint - array only contains elements from the given set. Uses MongoDB aggregation with $setIsSubset.
# Find items where tags only contain elements from the allowed list q.where :tags.subset_of => ["rock", "pop", "jazz"]
# This will match: # ["rock"] - yes (subset) # ["rock", "pop"] - yes (subset) # ["rock", "classical"] - no ("classical" not in allowed set)
Instance Method Summary collapse
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
-
#subset_of ⇒ ArraySubsetOfConstraint
A registered method on a symbol to create the constraint.
Instance Method Details
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 |
# File 'lib/parse/query/constraints.rb', line 2043 def build val = list_value field_name = Parse::Query.format_field(@operation.operand) # Check if values are pointers is_pointer_array = val.any? do |item| item.respond_to?(:pointer) || item.is_a?(Parse::Pointer) end if is_pointer_array # Extract objectIds from pointers target_ids = val.map do |item| if item.respond_to?(:id) item.id elsif item.is_a?(Parse::Pointer) item.id else item end end # Validate all IDs are present (unsaved objects have nil IDs) if target_ids.any?(&:nil?) raise ArgumentError, "#{self.class.name}: Cannot use unsaved objects (missing ID) in array constraint" end # $ifNull coerces a missing/null field to [] so $map (and $setIsSubset) # don't raise type errors on legacy documents that lack the field. # The empty set is a subset of every set, so a missing field matches # any subset_of target — consistent with treating missing as []. pipeline = [ { "$match" => { "$expr" => { "$setIsSubset" => [ { "$map" => { "input" => { "$ifNull" => ["$#{field_name}", []] }, "as" => "p", "in" => "$$p.objectId", } }, target_ids, ], }, }, }, ] else # $ifNull coerces a missing/null field to [] so $setIsSubset doesn't # raise a type error on legacy documents that lack the field. pipeline = [ { "$match" => { "$expr" => { "$setIsSubset" => [ { "$ifNull" => ["$#{field_name}", []] }, val, ], }, }, }, ] end { "__aggregation_pipeline" => pipeline } end |
#subset_of ⇒ ArraySubsetOfConstraint
A registered method on a symbol to create the constraint.
2040 |
# File 'lib/parse/query/constraints.rb', line 2040 register :subset_of |