Doctrine ODM query by subfield of reference-one

436 Views Asked by At

I have an existing MongoDb database:

Foo
{
  "_id": ObjectId(123456789),
  "name": "foo_1",
  "barId": 123,
  "sellerId": 456
}

Bar
{
  "_id": "123",
  "ref": "A1",
  "status": "paid"
}

Here is my mapping:

<document name="Foo">
   <id />
   <field field-name="name" type="string" />
   <field field-name="status" type="string" />
   <reference-one target-document="Bar" field="barId" store-as="id"/>
</document>
   
<document name="Bar">
   <id strategy="NONE" type="string" />
   <field field-name="ref" type="string" />
   <field field-name="status" type="string" />
</document>

I want to query all Foo filtered by sellerId and Bar.status = "paid"

But I can only filter by Bar id and no other fields. How can I do that?

//FooRepository.php

public function getAll(string $sellerId)
{
     $query = $this->createQueryBuilder()
                   ->field('sellerId')->equals($sellerId) // ok
    //             ->field('barId.status')->equals('paid') // ko
                   ->field('barId')->equals('123') // ok
            ;
    
     $query->readOnly();

     return $this->paginate($query);
}
1

There are 1 best solutions below

0
malarzm On

MongoDB's querying does not support "joins" like relational databases do. The closest thing to a LEFT JOIN is aggregation pipeline's $lookup. Please refer to ODM's documentation on how to use aggregation builder and its stages with the library.