How to sort nested MongoDB collection using PHP -
[ { "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 } ] } ]
i want sort above document, stored in mongodb, , using php.
scenario: in frontend, have drop-down showing trips (i.e. tokyo, paris, goa) , have button title of "price" show lowest highest price trip.
question:
when select, let's say, paris , click on price button, above document should sorted price (lowest highest).
sort should applied object destination paris.
this document can go 25/28 mb (here showing small preview of document).
what tried:
db.testcollection.find({ 'trip': { $elemmatch: { 'destination':'paris' } } }).sort({ "trip.price":1 })
and many more.
any small appreciated, thank you.
try this:
$collection = $db->createcollection("emp_details"); $sort = array('col_name' => 1); // can change array on behalf of condition like: if(something) { $sort = array('col_name' => 1); // asc } else { $sort = array('col_name' => -1); // desc } $resultset = $collection->find()->sort($sort);
this works me.
Comments
Post a Comment