Class: Parse::Constraint::ArrayEmptyConstraint
- Inherits:
-
Constraint
- Object
- 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 Method Summary collapse
-
#arr_empty ⇒ ArrayEmptyConstraint
A registered method on a symbol to create the constraint.
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
Instance Method Details
#arr_empty ⇒ ArrayEmptyConstraint
A registered method on a symbol to create the constraint.
1389 |
# File 'lib/parse/query/constraints.rb', line 1389 register :arr_empty |
#build ⇒ Hash
Returns the compiled constraint using aggregation pipeline.
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 |
# File 'lib/parse/query/constraints.rb', line 1392 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 |