Class: Parse::Constraint::ArrayNeqConstraint

Inherits:
Parse::Constraint 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 Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash)

    the compiled constraint using aggregation pipeline.



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/parse/query/constraints.rb', line 1052

def build
  val = formatted_value
  val = [val].compact unless val.is_a?(Array)

  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

#neqArrayNeqConstraint

A registered method on a symbol to create the constraint.

Examples:

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

Returns:



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

register :neq