我一直在寻找对集合中特定的内部数组进行排序,我在 symfony2 中使用了学说-mongodb-bundle。我的收藏 :
"page":
{
"_id": "56rgt46rt54h68rt4h6"
"categories":
[{
"_id": "2g56rt1h65rt165165erg4"
"products":[
{"name":"A", "pos": 2},
{"name":"B", "pos": 7},
{"name":"C", "pos": 1},
{"name":"D", "pos": 5}
]
}]
}
我希望我有:
"page":
{
"_id": "56rgt46rt54h68rt4h6"
"categories":
[{
"_id": "2g56rt1h65rt165165erg4"
"products":[
{"name":"C", "pos": 1},
{"name":"A", "pos": 2},
{"name":"D", "pos": 5},
{"name":"B", "pos": 7}
]
}]
}
我的实体:
/**
* @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;
}
我是 DoctrineMongoDBBundle 的新手,累积内部数组(EmbedMany)可能不是一个好主意,最好有几个集合。所以保存参考。