'Doctrine ODM query by subfield of reference-one [duplicate]
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);
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | malarzm |