Class: Parse::Constraint::ArrayEqConstraint

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

Overview

Note:

This constraint uses aggregation pipeline with MongoDB $eq on arrays.

Exact array equality constraint using MongoDB aggregation with $eq. Matches arrays that are exactly equal, including element order. This is order-dependent matching: [A, B] does NOT match [B, A].

q.where :field.eq_array => ["rock", "pop"] q.where :tags.eq_array => [category1, category2] # for pointers

For pointer arrays (has_many relations), pass Parse objects or pointers. The constraint will automatically extract objectIds for comparison.

Instance Method Summary collapse

Instance Method Details

#build ⇒ Hash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash) —

    the compiled constraint using aggregation pipeline.



1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
# File 'lib/parse/query/constraints.rb', line 1699

def build
  val = list_value

  field_name = Parse::Query.format_field(@operation.operand)

  # Check if values are pointers (Parse objects or pointer objects)
  is_pointer_array = val.any? do |item|
    item.respond_to?(:pointer) || item.is_a?(Parse::Pointer)
  end

  if is_pointer_array
    # Extract objectIds from pointers for comparison
    target_ids = val.map do |item|
      if item.respond_to?(:id)
        item.id
      elsif item.is_a?(Parse::Pointer)
        item.id
      else
        item
      end
    end

    # Validate all IDs are present (unsaved objects have nil IDs)
    if target_ids.any?(&:nil?)
      raise ArgumentError, "#{self.class.name}: Cannot use unsaved objects (missing ID) in array constraint"
    end

    # For pointer arrays, compare mapped objectIds with exact equality (order matters).
    # $ifNull coerces a missing/null field to [] so $map doesn't raise a type
    # error on legacy documents that lack the field.
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$eq" => [
              { "$map" => {
                "input" => { "$ifNull" => ["$#{field_name}", []] },
                "as" => "p",
                "in" => "$$p.objectId",
              } },
              target_ids,
            ],
          },
        },
      },
    ]
  else
    # For simple value arrays, direct $eq comparison (order matters).
    # $ifNull coerces a missing/null field to [] so a doc lacking the field
    # is treated the same as one with [], consistent with set_equals/arr_empty.
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$eq" => [
              { "$ifNull" => ["$#{field_name}", []] },
              val,
            ],
          },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#eq_array ⇒ ArrayEqConstraint

Note:

Use :eq_array for explicit array equality matching. Simple :eq is handled by the base Constraint class for scalar equality.

A registered method on a symbol to create the constraint.

Examples:

q.where :field.eq_array => ["value1", "value2"]
q.where :categories.eq_array => [cat1, cat2]

Returns:



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

register :eq_array