Class: Parse::Constraint::ArrayLastConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::ArrayLastConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
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
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
-
#last ⇒ ArrayLastConstraint
A registered method on a symbol to create the 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
Returns the compiled constraint using aggregation pipeline.
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 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 |
# File 'lib/parse/query/constraints.rb', line 1449 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 |
#last ⇒ ArrayLastConstraint
A registered method on a symbol to create the constraint.
1446 |
# File 'lib/parse/query/constraints.rb', line 1446 register :last |