gpt4 book ai didi

mongodb - Doctrine/MongoDB : Use key-value pairs instead numeric array

转载 作者:可可西里 更新时间:2023-11-01 10:08:01 27 4
gpt4 key购买 nike

我正在将 Doctrine ODM 与 MongoDB 结合使用。我有一个这样的“产品模型”:

namespace Cms\Model;

/** @Document(collection="products") */
class Product
{
/** @Id */
private $id;

/** @String */
private $title;

/** @String */
private $description;

/** @Date */
private $createdAt;

/** @EmbedMany(targetDocument="Cms\Model\ProductParam") */
private $params;

/** @EmbedOne(targetDocument="Cms\Model\Coordinate") */
private $coordinate;

public function __construct()
{
$this->details = new \Doctrine\Common\Collections\ArrayCollection();
$this->params = new \Doctrine\Common\Collections\ArrayCollection();
}
}

我的 ProductParam 模型是这样的:

namespace Cms\Model;

/** @EmbeddedDocument */
class ProductParam
{
/** @String */
private $type;

/** @String */
private $value;
}

当我插入具有此模式的文档时,结果是这样的:

{
"_id": ObjectId("4d17ac603ffcf6d01300002a"),
"title": "Peugeot 206 2001-X-Reg, 1.4lx Air-con, 12 months mot, Credit Cards Accepted.",
"description": "PEUGEOT 206 1.4LX IMMACULATE THROUGHOUT DRIVES ABSOLUTELY SUPERB",
"params": {
"0": {
"type": "carBrand",
"value": "PEUGEOT"
},
"1": {
"type": "carModel",
"value": "206 LX"
}
}

但是我需要的是这样的:

{
"_id": ObjectId("4d17ac603ffcf6d01300002a"),
"title": "Peugeot 206 2001-X-Reg, 1.4lx Air-con, 12 months mot, Credit Cards Accepted.",
"description": "PEUGEOT 206 1.4LX IMMACULATE THROUGHOUT DRIVES ABSOLUTELY SUPERB",
"params": {
carBrand: "PEUGEOT",
carModel: "206 LX"
}
}

我该怎么做?谢谢。

最佳答案

我建议你不要使用 PostLoad、PrePersist 等,因为它们真的很昂贵,而是使用 strategy="set"例如:

@EmbedMany(targetDocument="Field", strategy="set")

关于mongodb - Doctrine/MongoDB : Use key-value pairs instead numeric array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4535497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com