Class: Parse::Constraint::ArrayFirstConstraint
- Inherits:
-
Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::ArrayFirstConstraint
- 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.
First element constraint - match based on the first element of an array. Uses MongoDB aggregation with $arrayElemAt.
q.where :tags.first => "rock" # first element equals "rock"
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
-
#build ⇒ Hash
The compiled constraint using aggregation pipeline.
-
#first ⇒ ArrayFirstConstraint
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.
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 |
# File 'lib/parse/query/constraints.rb', line 1397 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", } }, 0] }, 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", } }, 0] }, compare_val, ], }, }, }, ] else pipeline = [ { "$match" => { "$expr" => { "$eq" => [ { "$arrayElemAt" => [{ "$ifNull" => ["$#{field_name}", []] }, 0] }, val, ], }, }, }, ] end { "__aggregation_pipeline" => pipeline } end |
#first ⇒ ArrayFirstConstraint
A registered method on a symbol to create the constraint.
1394 |
# File 'lib/parse/query/constraints.rb', line 1394 register :first |