Class: Parse::Constraint::ArraySetEqualsConstraint

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

Overview

Note:

This constraint uses aggregation pipeline with MongoDB $setEquals.

Set equality constraint using MongoDB aggregation with $setEquals. Matches arrays that contain exactly the same elements, regardless of order. This is order-independent matching: [A, B] matches [B, A] but not [A, B, C].

q.where :field.set_equals => ["rock", "pop"] q.where :tags.set_equals => [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.



1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
# File 'lib/parse/query/constraints.rb', line 1604

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, we need to map the objectIds from the stored pointers.
    # $ifNull coerces a missing/null field to [] so $map (and $setEquals) don't
    # raise type errors on legacy documents that lack the field.
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$setEquals" => [
              { "$map" => {
                "input" => { "$ifNull" => ["$#{field_name}", []] },
                "as" => "p",
                "in" => "$$p.objectId",
              } },
              target_ids,
            ],
          },
        },
      },
    ]
  else
    # For simple value arrays (strings, numbers, etc.).
    # $ifNull coerces a missing/null field to [] so $setEquals doesn't raise
    # a type error on legacy documents that lack the field.
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$setEquals" => [
              { "$ifNull" => ["$#{field_name}", []] },
              val,
            ],
          },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#set_equals ⇒ ArraySetEqualsConstraint

A registered method on a symbol to create the constraint.

Examples:

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

Returns:



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

register :set_equals