Class: Parse::Constraint::ArraySizeConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Parse::Constraint::ArraySizeConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
This constraint uses aggregation pipeline because Parse Server does not support the $size query operator natively.
This constraint uses MongoDB aggregation pipeline. While $expr expressions cannot utilize field indexes, aggregation is efficient for array size operations that would otherwise require client-side filtering.
Array size constraint using MongoDB aggregation. Parse Server does not natively support $size query constraint, so we use MongoDB aggregation pipeline with $expr and $size to check array length.
# Exact size match q.where :field.array_size => 2 q.where :tags.array_size => 5
# Comparison operators via hash q.where :tags.array_size => { gt: 3 } # size > 3 q.where :tags.array_size => { gte: 2 } # size >= 2 q.where :tags.array_size => { lt: 5 } # size < 5 q.where :tags.array_size => { lte: 4 } # size <= 4 q.where :tags.array_size => { ne: 0 } # size != 0
# Combine for range q.where :tags.array_size => { gte: 2, lt: 10 } # 2 <= size < 10
Constant Summary collapse
- COMPARISON_OPERATORS =
Mapping of constraint keys to MongoDB comparison operators
{ gt: "$gt", gte: "$gte", lt: "$lt", lte: "$lte", ne: "$ne", eq: "$eq", }.freeze
Instance Method Summary collapse
-
#array_size ⇒ ArraySizeConstraint
A registered method on a symbol to create the constraint.
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
Instance Method Details
#array_size ⇒ ArraySizeConstraint
A registered method on a symbol to create the constraint. The
operator is registered as :size and installed on Symbol as
array_size.
1303 |
# File 'lib/parse/query/constraints.rb', line 1303 register :size |
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 |
# File 'lib/parse/query/constraints.rb', line 1316 def build value = formatted_value field_name = Parse::Query.format_field(@operation.operand) size_expr = { "$size" => { "$ifNull" => ["$#{field_name}", []] } } if value.is_a?(Integer) # Simple exact match raise ArgumentError, "#{self.class}: Size value must be non-negative" if value < 0 pipeline = [ { "$match" => { "$expr" => { "$eq" => [size_expr, value], }, }, }, ] elsif value.is_a?(Hash) # Hash with comparison operators conditions = [] value.each do |op, val| op_sym = op.to_sym unless COMPARISON_OPERATORS.key?(op_sym) raise ArgumentError, "#{self.class}: Unknown operator '#{op}'. Valid operators: #{COMPARISON_OPERATORS.keys.join(", ")}" end unless val.is_a?(Integer) && val >= 0 raise ArgumentError, "#{self.class}: Value for '#{op}' must be a non-negative integer" end mongo_op = COMPARISON_OPERATORS[op_sym] conditions << { mongo_op => [size_expr, val] } end # Combine multiple conditions with $and expr = conditions.length == 1 ? conditions.first : { "$and" => conditions } pipeline = [ { "$match" => { "$expr" => expr, }, }, ] else raise ArgumentError, "#{self.class}: Value must be an integer or hash with comparison operators (gt, gte, lt, lte, ne, eq)" end { "__aggregation_pipeline" => pipeline } end |