Class: Parse::Constraint::ArrayLastConstraint

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

Overview

Note:

This constraint uses MongoDB aggregation pipeline with $arrayElemAt. While $expr expressions cannot utilize field indexes, aggregation enables positional array access not available in standard Parse queries.

Last element constraint - match based on the last element of an array. Uses MongoDB aggregation with $arrayElemAt and index -1.

q.where :tags.last => "pop" # last element equals "pop"

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.



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
# File 'lib/parse/query/constraints.rb', line 1478

def build
  val = formatted_value
  field_name = Parse::Query.format_field(@operation.operand)

  # Handle pointer values. Wrap field reference in $ifNull so a missing
  # field is treated as [] (yielding null from $arrayElemAt) rather than
  # propagating a Missing value through the pipeline.
  if val.respond_to?(:id)
    compare_val = val.id
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$eq" => [
              { "$arrayElemAt" => [{ "$map" => {
                  "input" => { "$ifNull" => ["$#{field_name}", []] },
                  "as" => "p",
                  "in" => "$$p.objectId",
                } }, -1] },
              compare_val,
            ],
          },
        },
      },
    ]
  elsif val.is_a?(Parse::Pointer)
    compare_val = val.id
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$eq" => [
              { "$arrayElemAt" => [{ "$map" => {
                  "input" => { "$ifNull" => ["$#{field_name}", []] },
                  "as" => "p",
                  "in" => "$$p.objectId",
                } }, -1] },
              compare_val,
            ],
          },
        },
      },
    ]
  else
    pipeline = [
      {
        "$match" => {
          "$expr" => {
            "$eq" => [
              { "$arrayElemAt" => [{ "$ifNull" => ["$#{field_name}", []] }, -1] },
              val,
            ],
          },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#lastArrayLastConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :tags.last => "pop"

Returns:



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

register :last