Hisham Huneidie
Hisham Huneidie

Reputation: 1

How can I add conditions in Doctrine for MongoDB

I have the Document CompetitionsDocument like this...

Name Position
Carl Center
Carl First
Carl Last
Carl Last
Mariah Center
Mariah Center
Mariah Last
Mariah Last

I would like to get a grouped table like this...

Name First Center Last
Carl 1 1 2
Mariah 0 2 2

Used tools:

My code is like this...

$builder->group()
    ->field('id')
    ->expression(
        $builder->expr()->field('name')->expression('$name')
    )
    ->field('first')
    ->sum(
        $builder->redact()->cond(
            $builder->expr()->gte('$position', 'First'), 1, 0
        )
    )
    ->field('center')
    ->sum(
        $builder->redact()->cond(
            $builder->expr()->gte('$position', 'Center'), 1, 0
        )
    )
    ->field('last')
    ->sum(
        $builder->redact()->cond(
            $builder->expr()->gte('$position', 'Last'), 1, 0
        )
    );

But I don't know how to add conditions that add 1 or 0 depending on the value.

Note: The function gte() returns error. I can't use 0 or 1 in the second and third parameter. I should use '$$PRUNE' or '$$DESCEND'

Upvotes: 0

Views: 98

Answers (0)

Related Questions