mongodb - PHP Mongo - find last object in collection -
how full php block of code return last inserted object in collection? function doesn't return proper object.
# create connection instance $this->mongo = new mongoclient($connection_string); # select database $this->database = $this->mongo->selectdb($database_name); public function get_last($given_collection){ $collection = $this->database->selectcollection($given_collection); $data_object = $collection->find()->sort(array("id"=>1)); return $data_object; }
try:
$collection->find()->sort(array("_id"=>-1))->limit(1);
this mongodb query working fine show data in descending order.
db.collection.find().sort({"_id":-1}).limit(1);
Comments
Post a Comment