[
{
"trip": [
{
"destination": "Tokyo",
"time": 8.56,
"price": 80
},
{
"destination": "Paris",
"time": 2.36,
"price": 9
},
{
"destination": "Goa",
"time": 4.56,
"price": 30
}
]
},
{
"trip": [
{
"destination": "Tokyo",
"time": 6.26,
"price": 23
},
{
"destination": "Paris",
"time": 7.46,
"price": 45
},
{
"destination": "Goa",
"time": 8.98,
"price": 39
}
]
}
]
我想对上述文档进行排序,该文档存储在MongoDB中,我使用PHP。
场景:在前端,我有一个下拉菜单,我显示行程(即东京,巴黎,果阿),我有一个标题为"价格"的按钮,显示最低到最高的价格行程。
:
当我选择,假设,巴黎,我点击价格按钮,上面的文档应该按价格排序(从最低到最高)。
Sort应该应用于目的地为Paris的对象
这个文档可以达到25/28mb(这里我只是展示了我的文档的一个小预览)
What I tried:
db.testCollection.find({
'trip': {
$elemMatch: {
'destination':'Paris'
}
}
}).sort({
"trip.price":1
})
和更多
任何小小的帮助都非常感谢,谢谢。
试试这个:
$collection = $db->createCollection("emp_details");
$sort = array('col_name' => 1); // you can change this array on the behalf of some condition like:
if(something)
{
$sort = array('col_name' => 1); // asc
}
else
{
$sort = array('col_name' => -1); // desc
}
$resultSet = $collection->find()->sort($sort);