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.
651 |
# File 'lib/parse/query/constraints.rb', line 651 register :arr_empty |
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/parse/query/constraints.rb', line 654 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 |