首页 > 解决方案 > Doctrine MongoDB - 使用 Doctrine MongoDB Aggregation Builder 在构面内水合结果

问题描述

我有以下查询,我希望第一个结果是水合的。这是我尝试过的:

$builder = $dm->createAggregationBuilder(\Documents\Jobs::class);
$builder
    ->facet()
        ->field('carpenterJobs')
        ->pipeline(
            $dm->createAggregationBuilder(\Documents\Jobs::class)
                ->hydrate($this->getEntityName(\Documents\Jobs::class))
                ->match()
                ->field('name')
                ->equals('carpenter');
        )
        ->field('cleanerJobs')
        ->pipeline(
            $dm->createAggregationBuilder(\Documents\Jobs::class)
                ->match()
                ->field('name')
                ->equals('cleaner');
        )
;

但是当我执行这个时,结果没有水合。下面是 Jobs 实体:

/**
 * @QueryResultDocument
 * @Document(repositoryClass="...")
 */
class Jobs
{
    /**
     * @Id
     */
    private $id;

    /**
     * @Field(type="string")
     * @Index(unique=true, order="asc")
     */
    private $name;

    ...
}

我也尝试过水合,$builder但它返回一个错误说Undefined index: _id。自己执行 'carpenterJobs' 查询,无需在水龙头内,我可以返回水合结果。

标签: phpmongodbdoctrine-odmdoctrine-mongodb

解决方案


推荐阅读