Class: Parse::Constraint::ArrayNeqConstraint

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

Overview

Note:

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

Array not-equal constraint using MongoDB aggregation with $ne. Matches arrays that are NOT exactly equal (including element order). This is order-dependent: [A, B] does NOT match [A, B] but DOES match [B, A].

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

Instance Method Summary collapse

Instance Method Details

#build ⇒ Hash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash) —

    the compiled constraint using aggregation pipeline.



1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
# File 'lib/parse/query/constraints.rb', line 1788

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 $ne (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" => {
            "$ne" => [
              { "$map" => {
                "input" => { "$ifNull" => ["$#{field_name}", []] },
                "as" => "p",
                "in" => "$$p.objectId",
              } },
              target_ids,
            ],
          },
        },
      },
    ]
  else
    # For simple value arrays, direct $ne 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" => {
            "$ne" => [
              { "$ifNull" => ["$#{field_name}", []] },
              val,
            ],
          },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#neq ⇒ ArrayNeqConstraint

A registered method on a symbol to create the constraint.

Examples:

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

Returns:



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

register :neq