【问题标题】:Why can't I store associative arrays in MongoDB document?为什么我不能在 MongoDB 文档中存储关联数组?
【发布时间】:2019-05-20 22:28:07
【问题描述】:

我想将格式为 JJ/MM/YYYY 的日期存储到一个关联数组中,如下所示:

'day' => $day,
'month' => $month,
'year' => $year

我已经很容易做到了,但是一旦我尝试将文档存储在集合中,日期就会像这样存储在文档中:

0 => $day
1 => $month
2 => $year

我在“刷新”之前对对象进行了 PHP 调试,它显示了与值关联的键,但是一旦在集合中,它就被数字键重置了。

我的实体如下所示:

/**
 * @MongoDB\Field(type="collection")
 */
private $date;

public function getDate()
{
    return $this->date;
}

public function setDate($date)
{
    $this->date = $date;

    return $this;
}

还有控制器部分:

$date = $movie->getDate();
$keys = array('day', 'month', 'year');
$values = explode('/', $date);
$dateArray = array_combine($keys, $values);
$movie->setDate($dateArray);

我想确切地说,我在 Symfony 4 上使用 Doctrine ODM for MongoDB。

所以这是我的问题:

1)如何存储带有关联键的数组?

2) MongoDB 文档中的键是否“固定”?我的意思是,我确定密钥 0 将始终与 $day 相关联,而密钥 1 将始终与 $month 相关联吗?

【问题讨论】:

    标签: php arrays mongodb symfony doctrine


    【解决方案1】:

    你需要使用hashtype:

    hash: 关联数组到 MongoDB 对象

    collection: 数字索引数组到 MongoDB 数组

    /**
     * @MongoDB\Field(type="hash")
     */
    private $date;
    

    【讨论】:

    • 谢谢。这确实解决了问题。只是一个问题:在文档中,它说If you are using the hash type, values within the associative array are passed to MongoDB directly, without being prepared. Only formats suitable for the Mongo driver should be used.,当它说“适合Mongo驱动程序的格式”时是什么意思?
    • 据我了解,这意味着只有标量值和数组或 mongo 接受的值(例如 \MongoDate 而不是 \DateTime)
    【解决方案2】:

    您可以使用 Doctrine ODM 提供的 EmbeddedDocument 功能。

    首先,您需要在 AppBundle\Document\DateExample.php 中创建 EmbeddedDocument DateExample:

    <?php
    
    namespace AppBundle\Document;
    
    use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
    
    /**
     * @MongoDB\EmbeddedDocument()
     */
    class DateExample
    {
        /**
         * @MongoDB\Field(type="int")
         */
        protected $day;
    
        /**
         * @MongoDB\Field(type="int")
         */
        protected $month;
    
        /**
         * @MongoDB\Field(type="int")
         */
        protected $year;
    
        // getter and setter
    }
    

    然后,您可以在示例文档中使用 DateExample。所以 Example.php 文件会是这样的:

    <?php
    
    namespace AppBundle\Document;
    
    use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
    
    /**
    * @MongoDB\Document(repositoryClass="AppBundle\Repository\ExampleRepository")
    */
    class Example
    {
    
        /** @MongoDB\EmbedOne(targetDocument="DateExample") */
        private $date;
    
        // ...
    }
    

    还有控制器部分:

    $date_example = new DateExample();
    $date_example->setDay(21);
    $date_example->setMonth(05);
    $date_example->setYear(2019);
    
    $example = new Example();
    $example->setDate($date_example);
    
    // ...
    

    【讨论】:

    • 感谢您的回答。但是,它看起来有点“太多”,这是在 MongoDB Document Doctrine 中保留数组键的唯一解决方案吗?
    猜你喜欢
    • 2011-06-04
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多