1

Im working with node.js express and using MongoDB, I have a list of data with username and id. If i want to console.log 5 lastest data that inserted to the database, how would you do it? Any ideas?

Community
  • 1
  • 1
nihulus
  • 1,475
  • 4
  • 24
  • 46

1 Answers1

1

The ObjectID, which is the default value of the _id field, contains a timestamp. You can sort() your data by ObjectID to get the latest documents inserted into the database.

If you're using a different _id, you'll need to make sure that you store a timestamp somewhere in your documents.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%7B%7Bsort%28%29%7D%7D

Jenna
  • 2,386
  • 17
  • 10