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

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -