Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL order by in mongodb findOne()

Tags:

php

mongodb

In my mongo collection, I have several records with timestamps. I want to use findOne() and return the oldest record with a where parameter.

If it's not possible to use findOne(), it's alright. I just need to return the oldest record with a where parameter.

How can this be done in MongoDB?

like image 967
kapeels Avatar asked Jan 14 '11 06:01

kapeels


1 Answers

If you need an oldest record, use

db.collection.find().sort({ created: *1* }).limit(1)
like image 139
user105991 Avatar answered Oct 14 '22 13:10

user105991