Class: Parse::Constraint::DoesNotEqualLinkedPointerConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::DoesNotEqualLinkedPointerConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
Constraint for comparing pointer fields where they do NOT equal through linked objects. Uses MongoDB's $lookup to join collections and $expr with $ne to compare fields.
Usage: Document.where(:project.does_not_equal_linked_pointer => { through: :post, field: :project })
This generates a MongoDB aggregation pipeline that:
- Uses $lookup to join the linked collection
- Uses $match with $expr and $ne to find records where fields do NOT match
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
-
#build ⇒ Hash
Builds the MongoDB aggregation pipeline for the does-not-equal-linked-pointer constraint.
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
#build ⇒ Hash
Builds the MongoDB aggregation pipeline for the does-not-equal-linked-pointer constraint
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 |
# File 'lib/parse/query/constraints.rb', line 2925 def build # Validate that value is a hash with required keys unless @value.is_a?(Hash) && @value[:through] && @value[:field] raise ArgumentError, "DoesNotEqualLinkedPointerConstraint requires a hash with :through and :field keys" end through_field = @value[:through] target_field = @value[:field] # Convert field names to Parse format (snake_case to camelCase) with _p_ prefix for pointers local_field_name = format_field_name(@operation.operand, is_pointer: true) through_field_name = format_field_name(through_field, is_pointer: true) target_field_name = format_field_name(target_field, is_pointer: true) # Determine the collection name for the lookup (Rails pluralization) through_class_name = through_field.to_s.classify lookup_collection = through_class_name # Generate unique alias name for the joined data (use clean name without _p_ prefix) lookup_alias = "#{through_field.to_s.camelize(:lower)}_data" # Build the MongoDB aggregation pipeline pipeline = [] # Parse stores pointers as "ClassName$objectId" strings # We need to extract just the objectId part after the $ # Stage 1: Add field with extracted objectId add_fields_stage = { "$addFields" => { "#{through_field_name}_id" => { "$substr" => [ "$#{through_field_name}", lookup_collection.length + 1, # Skip "ClassName$" -1, # Rest of string ], }, }, } pipeline << add_fields_stage # Stage 2: $lookup to join the linked collection lookup_stage = { "$lookup" => { "from" => lookup_collection, "localField" => through_field_name, "foreignField" => "_id", "as" => lookup_alias, }, } pipeline << lookup_stage # Stage 2: $match with $expr to compare the fields using $ne (not equal) match_stage = { "$match" => { "$expr" => { "$ne" => [ { "$arrayElemAt" => ["$#{lookup_alias}.#{target_field_name}", 0] }, "$#{local_field_name}", ], }, }, } pipeline << match_stage # Return a special marker that indicates this needs aggregation pipeline processing { "__aggregation_pipeline" => pipeline } end |