Get the latest entry from the mongodb collection

I want to find out the latest entry in the collection. How to do it?

Note. I know the following prompts on the command line:

1. db.test.find().sort({"idate":-1}).limit(1).forEach(printjson); 2. db.test.find().skip(db.test.count()-1).forEach(printjson) 

where idate has an added timestamp.

The problem is that data collection is the time to return the data, and my collection of "test" is really huge. I need a query with a constant response time.

If there is a better mongodb command line request, let me know.

+74
mongodb mongodb-query
Jun 06 '12 at 19:14
source share
7 answers

This is a revision of the previous answer, but it most likely works on different versions of mongodb.

 db.collection.find().limit(1).sort({$natural:-1}) 
+105
Aug 31 '14 at
source share

This will give you one last document for collection

 db.collectionName.findOne({}, {sort:{$natural:-1}}) 

$natural:-1 means the order opposite to what records are inserted.

Change For all downvoters above, Mongoose syntax, mongo command line syntax: db.collectionName.find({}).sort({$natural:-1}).limit(1)

+24
Feb 02 '14 at 21:28
source share

I need a time response request

By default, indexes in MongoDB are B-trees. A B-Tree search is an O (logN) operation, so even find({_id:...}) will not provide constant time, O (1) answers.

You can also sort by _id if you use the ObjectId for your identifiers. See here for more details . Of course, even this is only good until the last second.

You can resort to "writing twice." Write once to the main collection and write again to the "last updated" collection. Without transactions, this will not be ideal, but with only one item in the โ€œlast updatedโ€ collection, it will always be fast.

+19
Jun 06 2018-12-06T00:
source share

Another way to get the last item from the MongoDB collection (never mind the examples):

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

Normal projection

 > db.Sports.find() { "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2017", "Body" : "Again, the Pats won the Super Bowl." } { "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." } { "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." } 

Sorted projection (_id: reverse order)

 > db.Sports.find().sort({'_id':-1}) { "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." } { "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." } { "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2018", "Body" : "Again, the Pats won the Super Bowl" } 

sort({'_id':-1}) , defines the projection in descending order of all documents based on their _id .

Sorted projection (_id: reverse order): get the last (last) document from the collection.

 > db.Sports.find().sort({'_id':-1}).limit(1) { "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." } 
+6
Nov 26 '18 at 3:35
source share

php7.1 mongoDB:
$data = $collection->findOne([],['sort' => ['_id' => -1],'projection' => ['_id' => 1]]);

0
Oct. 13 '16 at 4:01
source share

My decision:

db.collection("name of collection").find({}, {limit: 1}).sort({$natural: -1})

0
Jan 07 '18 at 7:45
source share

If you use the automatically generated Mongo object identifiers in your document, it contains a timestamp as the first 4 bytes by which you can find the last document inserted into the collection. I understand this is an old question, but if someone is still staying here looking for another alternative.

 db.collectionName.aggregate( [{$group: {_id: null, latestDocId: { $max: "$_id"}}}, {$project: {_id: 0, latestDocId: 1}}]) 

Above request will give _id for the last document inserted into the collection

0
Mar 28 '18 at 8:59
source share



All Articles