Hi,
basically you just need to skip the encapsulation and add the annotations directly. In other words, just add @ODM\Index and @ODM\UniqueIndex annotations directly without surrounding them by @ODM\Indexes. No need to change anything else in your logic, because it is already correct. I just verified that this works for multi-field indexes, too:
/**
* @ODM\EmbeddedDocument
* @ODM\Indexes({
* @ODM\Index(name="QuantityIndex", keys={"quantity":"asc"}),
* @ODM\UniqueIndex(name="ean", background=false, keys={"ean":"asc"}),
* @ODM\Index(name="size", background=true, keys={"size":"asc"}),
* @ODM\Index(name="variantid", background=false, keys={"variantId":"asc"}),
* @ODM\UniqueIndex(name="sizevariant_id", background=false, sparse=true, keys={"sizeVariantId":"asc"}),
* @ODM\UniqueIndex(name="multifield", keys={"ean":"asc","size":"asc"})
* })
*/
class Variant
{
...
- I manually removed the encapsulation (i.e. remove the two lines "* @ODM\Indexes({" and "* })" and removed all the commas at the end of the other lines), so it looks like this:
/**
* @ODM\EmbeddedDocument
* @ODM\Index(name="QuantityIndex", keys={"quantity":"asc"})
* @ODM\UniqueIndex(name="ean", background=false, keys={"ean":"asc"})
* @ODM\Index(name="size", background=true, keys={"size":"asc"})
* @ODM\Index(name="variantid", background=false, keys={"variantId":"asc"})
* @ODM\UniqueIndex(name="sizevariant_id", background=false, sparse=true, keys={"sizeVariantId":"asc"})
* @ODM\UniqueIndex(name="multifield", keys={"ean":"asc","size":"asc"})
*/
class Variant
{
...
- Run
bin/console doctrine:mongodb:schema:update
in console
Result: A multi-field index was correctly added to mongodb.