PHP Mongo - 查找集合

时间:2016-07-25 15:44:38

标签: php mongodb

完整的PHP代码块如何在集合中返回最后插入的对象?这是我的功能,但它没有返回正确的对象。

# 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;
    }

1 个答案:

答案 0 :(得分:2)

尝试:

$collection->find()->sort(array("_id"=>-1))->limit(1);

这是mongodb查询按降序运行精细显示数据。

db.collection.find().sort({"_id":-1}).limit(1);
相关问题