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.
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 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 |
# File 'lib/parse/query/constraints.rb', line 1368 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.
1365 |
# File 'lib/parse/query/constraints.rb', line 1365 register :first |