开发者

Mongo - Fetch first created and last created document according to 'ts' (timestamp)

Is it possible to fetch first created and last created document according to 'ts' (timestamp) element of the doc开发者_JAVA技巧ument in single mongo query? If yes then please let me know how to query the mongo using php.


You can't get them in one query, but you can do it in 2 queries.

db.collection.find().limit(1).sort([ts:-1])  --> Find the last timestampe
db.collection.find().limit(1).sort([ts:1])   --> Find the first timestamp 

or in PHP

$cursor = $collection->find()->sort(array('ts'=>-1))->limit(1);
$cursor = $collection=>find()->sort(array('ts'=>1))->limit(1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜