Class: Parse::Constraint::DoesNotEqualLinkedPointerConstraint
- Inherits:
-
Constraint
- Object
- 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 Method Summary collapse
-
#build ⇒ Hash
Builds the MongoDB aggregation pipeline for the does-not-equal-linked-pointer constraint.
Instance Method Details
#build ⇒ Hash
Builds the MongoDB aggregation pipeline for the does-not-equal-linked-pointer constraint
3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 |
# File 'lib/parse/query/constraints.rb', line 3899 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] # Determine the collection name for the lookup (Rails pluralization) through_class_name = through_field.to_s.classify lookup_collection = through_class_name # Convert field names to Parse format with _p_ prefix for pointers. # The local and through fields are columns of the queried class; the # target field is a column of the linked class, so it uses that # class's names. 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 = Parse::Query.with_field_aliases(lookup_collection) do format_field_name(target_field, is_pointer: true) end # 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 |