Skip to content

Aggregation framework: get related object  #1677

@NikitaKharkov

Description

@NikitaKharkov

At first my code and examples of data:

event: {
    "_id" : 3,
    "name": "someTestName",
    ...,
    "seoBlock" : {
        "$ref" : "event_seo_block",
        "$id" : 1004
    }
}
event_seo_block: {
    "_id": '1004',
    "text": "someText",
    "image": "http://lorempixel.com/640/480/?34447"
}

Fetch code:

$builder =  $this->getDocumentManager()->getDocumentCollection('AppBundle:Event')->createAggregationBuilder();
$builder
            ->match()
                ->field('status')->equals(Event::STATUS_ONLINE)
                ->field('leadingTradeFair')->equals(true)
                ->field('eventTo')->gte($this->getStartOfToday())
                ->field('seoBlock.$id')->in($validSeoBlocksIds)
            ->lookup('event_seo_block')
                ->localField('seoBlock.$id')
                ->foreignField('_id')
                ->alias('seoBlock')
            ->group()
                ->field('_id')->expression('$fairId')
                ->field('events')->push('$$ROOT')
            ->project()
                ->excludeIdField()
                ->field('seo')->expression('$events.seoBlock')
                ->field('events')->expression('$events')
                ->field('order')->add(null, null)
            ->sort('events.fairId', 1)

Explanation of problem:

I have event table with relation event_seo_block table as 1:1 . So when I made all the operations I have to get all info from related object, but I can't do it, because I can't point in expression seoBlock.$id (->localField('seoBlock.$id') string in code), I have an error:

FieldPath field names may not start with '$'.

I read about this problem, here I found about problem: jira.mongodb.org. But ODM don't support objectToArray and I can't solve problem in this way. Other links says the same.

I want to know is there a way how can I get this objects by aggregation framework?

What you do advise to do? Thanks

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions