MongoDB / Doctrine ODM / SF4.1:总计相当于内部联接

时间:2018-10-22 14:22:47

标签: mongodb symfony4 doctrine-odm

我尝试汇总文档以返回包含多个文档值的数组。

这是我的模型电路:

/**
 * @var string
 *
 * @Mongo\Field(type="string", nullable=false)
 */
protected $name;
/**
 * @var Collection
 *
 * @Mongo\ReferenceMany(targetDocument=Truck::class, inversedBy="circuits")
 */
protected $trucks;
/**
 * @var Stand
 *
 * @Mongo\ReferenceOne(targetDocument=Stand::class, inversedBy="circuits")
 */
protected $stand;

我的卡车型号:

/**
 * @var string
 *
 * @Mongo\Field(type="string", nullable=false)
 */
protected $code;
/**
 * @var string
 *
 * @Mongo\Field(type="string", nullable=false)
 */
protected $name;
/**
 * @var Collection
 *
 * @Mongo\ReferenceMany(targetDocument=Circuit::class, inversedBy="trucks")
 */
protected $circuits;

我的展位模型:

/**
 * @var string
 *
 * @Mongo\Field(type="string", nullable=false)
 */
protected $name;
/**
 * @var ArrayCollection
 *
 * @Mongo\ReferenceMany(targetDocument=Truck::class, inversedBy="stand", cascade={"persist", "remove"})
 */
protected $trucks;

我的问题是我无法返回带有架子和卡车代码的电路:

我当前的输出如下:

{
"_id" : "5bcdd97724083a098f30bdcc",
"nom" : "First Circuit",
"stand" : {
    "$ref" : "stand",
    "$id" : "5bcdd97424083a098f30bd85",
    "$db" : "my_db"
},
"trucks" : [ 
    {
        "$ref" : "truck",
        "$id" : "5bcdda0324083a099745b22e",
        "$db" : "my_db"
    }
],
"createdAt" : ISODate("2018-10-22T14:06:47.541Z"),
"updatedAt" : ISODate("2018-10-22T14:06:47.541Z")

}

但是我想要这样的输出:

    {
"_id" : "5bcdd97724083a098f30bdcc",
"name" : "First Circuit",
"stand" : {
    "name": "Stand name"
},
"trucks" : [ 
    {
        "code": "Truck code 1"
    },
    {
        "code": "Truck code 2"
    }
],
"createdAt" : ISODate("2018-10-22T14:06:47.541Z"),
"updatedAt" : ISODate("2018-10-22T14:06:47.541Z")

}

目前,我已经在电路文件中设置了第一个查询,例如:

$queryBuilder = $this->createQueryBuilder()
        ->find()
        ->select(['name', 'trucks'])
        ->field('stand.id')
            ->equals($id)
        ->field('date')
            ->gte($startOfDay)
            ->lte($endOfDay)
        ->hydrate(false)
        ->eagerCursor(true);

为了更好地理解,我尝试使这个SQL请求等效:

SELECT circuit.name, truck.code, stand.name FROM circuit INNER JOIN truck (I do not set the ON clause, because here is a nested document) INNER JOIN stand WHERE date >= '2018-10-22 00:00:00' AND date <= '2018-10-22 23:59:59';

请您帮我设置正确的mongo /主义查询吗?

先谢谢了

0 个答案:

没有答案