I've been looking for sorting a specific inner array in a collection, I use doctrine-mongodb-bundle in symfony2. My collection :
"page":
{
"_id": "56rgt46rt54h68rt4h6"
"categories":
[{
"_id": "2g56rt1h65rt165165erg4"
"products":[
{"name":"A", "pos": 2},
{"name":"B", "pos": 7},
{"name":"C", "pos": 1},
{"name":"D", "pos": 5}
]
}]
}
I wish I had :
"page":
{
"_id": "56rgt46rt54h68rt4h6"
"categories":
[{
"_id": "2g56rt1h65rt165165erg4"
"products":[
{"name":"C", "pos": 1},
{"name":"A", "pos": 2},
{"name":"D", "pos": 5},
{"name":"B", "pos": 7}
]
}]
}
And my entities :
/**
* @MongoDB\EmbeddedDocument
*/
class Category
{
/**
* @MongoDB\Id(strategy="auto")
*/
protected $id;
/**
* @MongoDB\String
*/
protected $name;
/** @MongoDB\EmbedMany(targetDocument="\Document\Product") */
private $products = array();
}
/**
* @MongoDB\EmbeddedDocument
*/
class Product
{
/**
* @MongoDB\Int
*/
protected $pos;
/**
* @MongoDB\String
*/
protected $name;
}
I'm new with DoctrineMongoDBBundle, maybe is not a good idea to cumulate inner array (EmbedMany), and it would be better to have several collections. And so save reference.