MongoDB match aggregation stage with $expr and $in with Doctrine ODM

21 views Asked by At

Documents in a pipeline stage have fields: string company and array ancestors, I need to filter out just the ones that have company mentioned in ancestors:

# input
{ company: 'A', ancestors: ['B', 'C'] }
{ company: 'X', ancestors: ['B', 'X'] }
{ company: 'A', ancestors: ['A', 'B'] }

# expected result
{ company: 'X', ancestors: ['B', 'X'] }
{ company: 'A', ancestors: ['A', 'B'] }

Aggregation pipeline I built in MongoDB Compass works as expected. Cannot find a way to translate one of the stages to a Doctrine ODM Aggregation Builder syntax. Problem stage has a type $match:

{
  $expr: {
    $in: [ "$company", '$ancestors' ]
  }
}

Tried $ab->match()->expr()->in('$company', '$ancestors'); which fails this ->in() is not an aggregation one, but a plain query "in" that expects a single array argument. Aggregation "in" derives from the aggregation builder itself $ab->expr()->in(...) inside some.. expr?

$ab->addAnd(
  $ab->expr()->in('$company', '$ancestors')
)

Not exactply the logic I am after, addAnd() also requires more expressions.

What syntax of the Doctrine aggregation builder would create the aggregatiop stage?

0

There are 0 answers