Class: Parse::Constraint::ArrayNotSetEqualsConstraint

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

Overview

Note:

This constraint uses aggregation pipeline with MongoDB $not and $setEquals.

Not-set-equals constraint using MongoDB aggregation with $not and $setEquals. Matches arrays that do NOT contain exactly the same elements (regardless of order). This is order-independent: [A, B, C] does NOT match [A, B] but [C, B, A] DOES match.

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



1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
# File 'lib/parse/query/constraints.rb', line 1877

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, use $not with $setEquals on mapped objectIds.
    # $ifNull coerces a missing/null field to [] so $map doesn't raise a type
    # error on legacy documents that lack the field. A missing field is
    # treated the same as [] for set-equality purposes.
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$not" => {
              "$setEquals" => [
                { "$map" => {
                  "input" => { "$ifNull" => ["$#{field_name}", []] },
                  "as" => "p",
                  "in" => "$$p.objectId",
                } },
                target_ids,
              ],
            },
          },
        },
      },
    ]
  else
    # For simple value arrays, use $not with $setEquals.
    # $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" => {
            "$not" => {
              "$setEquals" => [
                { "$ifNull" => ["$#{field_name}", []] },
                val,
              ],
            },
          },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#not_set_equals ⇒ ArrayNotSetEqualsConstraint

A registered method on a symbol to create the constraint.

Examples:

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

Returns:



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

register :not_set_equals