Storing array with Doctrine and MongoDB

后端 未结 3 1083
长发绾君心
长发绾君心 2021-01-05 08:44

How can I store an array with Doctrine and Mongo DB?

I do not want reference document, only array.

Example:

Type[ 
     Type1,
     Type2,
           


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 09:38

    You can use mongo types hash or collection as your need.

    Hash : Stores and retrieves the value as associative array.

    Collection : Stores and retrieves the value as numeric indexed array.

    For example:

    use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
    
    class Category
    {
        /**
         * @MongoDB\Field(name="tags", type="collection")
         */
        private $tags;
    
        /**
         * @MongoDB\Field(name="country_wise_total_count", type="hash")
         */
        private $country_wise_total_count;
    }
    

    The data is stored such as :

    "tags": [
        "man",
        "boy",
        "male",
        "sandal",
        "cloth",
        "army boots",
        "boots",
        "sport shoes",
        "school",
        "casual",
        "office"
      ],
    
    "country_wise_total_count": {
         "NP": NumberInt(7),
         "US" : NumberInt(10)
      }
    

提交回复
热议问题