Class: Parse::Constraint::ArrayEmptyConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::ArrayEmptyConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
This uses the arr_empty name to avoid conflict with the existing empty constraint which checks if the first array element exists.
The true case uses equality which can leverage MongoDB indexes. The false case still requires $size which cannot use indexes.
Array empty constraint - matches arrays with no elements. Uses direct equality for the true case (index-friendly) rather than $size.
q.where :tags.arr_empty => true # arrays with 0 elements (uses { field: [] })
q.where :tags.arr_empty => false # arrays with 1+ elements (uses $size > 0)
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
-
#arr_empty ⇒ ArrayEmptyConstraint
A registered method on a symbol to create the constraint.
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
Methods inherited from Parse::Constraint
#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s
Constructor Details
This class inherits a constructor from Parse::Constraint
Instance Method Details
#arr_empty ⇒ ArrayEmptyConstraint
A registered method on a symbol to create the constraint.
622 |
# File 'lib/parse/query/constraints.rb', line 622 register :arr_empty |
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/parse/query/constraints.rb', line 625 def build value = formatted_value unless value == true || value == false raise ArgumentError, "#{self.class}: Value must be true or false" end field_name = Parse::Query.format_field(@operation.operand) if value # Use direct equality for empty array (can use MongoDB index) pipeline = [{ "$match" => { field_name => [] } }] else # For non-empty, use $ne [] which is index-friendly # Note: This matches arrays with elements but also matches nil/missing # Use not_empty constraint if you need to exclude nil/missing pipeline = [{ "$match" => { field_name => { "$ne" => [] } } }] end { "__aggregation_pipeline" => pipeline } end |