8

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

I do not want reference document, only array.

Example:

Type[ 
     Type1,
     Type2,
     Type3
]

Do I need to create new Doctrine ODM data type?

3 Answers 3

16

If you need to store values not mapped to a document class in an array, you can use the collection field mapping, which maps to a basic array in MongoDB. There is also a hash type, which similarly converts an associative array in PHP to an object in MongoDB without mapping anything within it.

If "Type" in your example is a mapped document class, then you'll want to use an EmbedMany relationship, which will store one or more mapped documents in an array within the parent document. Within MongoDB, this will be represented as an array of objects, which is similar to what you could do yourself with the collection field (storing an array of associative arrays); however, ODM will utilize the EmbedMany mapping to hydrate those objects back to document instances.

Sign up to request clarification or add additional context in comments.

5 Comments

worked for me thanks. Not clear why it's not specified in the docs docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/…
What kind of mapping should we use to update the array to add another key : value element ?
@Sekai: I don't understand the question. The collection type runs the PHP value through array_values() before writing it to the MongoDB field (to reindex the keys numerically). The hash strategy casts the PHP value to an object (i.e. stdClass in PHP) to ensure it's saved as a BSON object. None of the key/value pairs within the collection/hash values are mapped (that's what distinguishes these from embedded documents (EmbedOne and EmbedMany).
I meant if you have an array like 0=>'a',1=>'b' and I want to append 'c' so the array would look like 0=>'a',1=>'b', 2=>'c' In symfony2 I add manually a function to my document class to do it . I'm wondering if there is a mapping that would generate automatically this function
If you have a method in your model that appends an element to an array field, which is mapped as a collection, the behavior should be what you expect. ODM doesn't concern itself with changes within array/object values for hash and collection, so it's just going to $set the new value in the document. This is in contrast to reference- and embed-many relationships, where it has more logic to update individual array/object elements.
7

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)
  }

Comments

4
...
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
...
class MyClass
{
    /**
     * @MongoDB\Hash
     */
    protected $tags = array();
}

Besides, you can check out BSPTagBundle if you want a form type that helps you with that type of variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.