Class: Parse::Constraint::ArrayNotEmptyOrNilConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/parse/query/constraints.rb

Overview

Note:

Uses index-friendly operations where possible.

Array not empty constraint - matches arrays that have at least one element. This is the opposite of empty_or_nil: it matches only non-empty arrays.

q.where :tags.not_empty => true # matches non-empty arrays only q.where :tags.not_empty => false # matches [] or nil/missing

Instance Method Summary collapse

Instance Method Details

#build ⇒ Hash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash) —

    the compiled constraint using aggregation pipeline.



1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
# File 'lib/parse/query/constraints.rb', line 1537

def build
  value = formatted_value
  unless value == true || value == false
    raise ArgumentError, "#{self.class}: Value must be true or false"
  end

  # Use formatted field name for proper Parse field mapping
  field_name = Parse::Query.format_field(@operation.operand)

  if value
    # Match non-empty arrays (must exist, not nil, and not empty)
    # Use $and to combine multiple conditions without duplicate keys
    pipeline = [
      {
        "$match" => {
          "$and" => [
            { field_name => { "$exists" => true } },
            { field_name => { "$ne" => nil } },
            { field_name => { "$ne" => [] } },
          ],
        },
      },
    ]
  else
    # Match empty array OR nil/missing field
    # Use explicit $eq for empty array check (more reliable through Parse Server)
    pipeline = [
      {
        "$match" => {
          "$or" => [
            { field_name => { "$exists" => true, "$eq" => [] } },
            { field_name => { "$exists" => false } },
            { field_name => { "$eq" => nil } },
          ],
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#not_empty ⇒ ArrayNotEmptyOrNilConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :field.not_empty => true

Returns:



1534
# File 'lib/parse/query/constraints.rb', line 1534

register :not_empty