How to filter a collection based on array key value in Doctrine ODM

103 views Asked by At

How can I filter a mongodb collection on array type field? Using Doctrine ODM.

Db structure

So I want to filter only documents, that contain translations.locale = 'sk' and translations.translation = 'slovensko'.

I used this code:

$qb = $this->createQueryBuilder();

    foreach ($filters as $keys => $value) {
        $qb = match ($keys) {
            'isActive' => $qb->field('isActive')->equals($value === 'true'),
            'locale' => $qb->field('translations.locale')->equals($value),
            'value' => $qb->field('translations.translation')->equals(new Regex("^{$value}", 'i')),
            default => $qb
        };
    }

    return $qb->getQuery()->execute()->toArray();

That worked when I only had one translation saved. But once I added another, it returned empty array.

0

There are 0 answers