2013-12-09 76 views
-1

我使用find()函數在MongoDB中並在下面的格式獲取最後一行MongoDB中

Array 
(
    [_id] => MongoId Object 
     (
      [$id] => 52a561ea78e9288b568b4567 
     ) 

    [friendID] => 1 
    [name] => Shobhit Srivastav 
    [senderID] => 2 
    [receiverID] => 1386570218 
    [receiverType] => TW 
    [receiverUserID] => 3 
    [status] => 0 
) 

Array 
(
    [_id] => MongoId Object 
     (
      [$id] => 52a5623178e928d8568b4567 
     ) 

    [friendID] => 2 
    [name] => Sachin Tendulkar 
    [senderID] => 2 
    [receiverID] => 1386570289 
    [receiverType] => TW 
    [receiverUserID] => 3 
    [status] => 0 
) 

得到了一個紀錄,但我想這是在表中插入最後一行的記錄。我怎麼找到?

在此先感謝!

+1

顯示正常mondodb輸出,而不是你的php的print_r的 –

回答

1

如果要通過​​降序得到插入表中的最後一條記錄,然後進行排序:

上存儲的ObjectId值的_id字段排序大致 等同於創建時間排序。

,並得到第一個記錄:

db.collection.find().sort({ _id : -1 }).limit(1); 

用PHP驅動它看起來像

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