Clifford Fajardo
Clifford Fajardo

Reputation: 1447

MongoDB: How to get the newest documents within a collection by using _id?

How would I retrieve the newest documents created in my collection by using _id?

I'm trying to implement a pagination in my web application by:

  1. Retrieving the _id of the last document loaded in my page
  2. Retrieve documents greater than “_id” of the last document in the next page (http://tldrify.com/dcj)

The reason I took this approach was because I was trying to leverage the natural order in the stored data - the id stored in the document.

Example of getting next set of posts:

Posts.find({ _id : { $gt : lastObjectId } }, function(){
  if(error){
    console.log('error in get_next_posts');
  } else {
    response.json(results);
  }
}).limit(5)

My mongoose schema:

var PostSchema = new mongoose.Schema({
  upvotes: {type: Number, default: 0},
  comments: Array,
  imageURI: String,
  caption: String,
  location: Object,
  hashtag: String
});

To my understanding, Mongo's documents are already sorted from oldest to newest, as per the documentation :

"MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created. (https://docs.mongodb.org/v3.0/core/index-single/)"

All was working well initially after pushing a few posts to my mongoDB database, which is hosted on Heroku (seems to be posting in ascending order)

Post1 id --- 56756e2047b977030017b36d
Post2 id --- 56756e4f47b977030017b36e
Post3 id --- 56756e8447b977030017b36f
Post4 id --- 56756ef747b977030017b370
Post5 id --- 56756f3f47b977030017b371
Post6 id --- 56756f7247b977030017b372

But after adding my 7th and 8th posts to the collection, it seems my Mongo database has suddenly stopped sorting the posts in ascending order, which means I can no longer use my previous mongoose query.

I'm not sure why after a few posts to my database the ascending order just stops - anyone have an idea why?

Post1 id --- 56756e2047b977030017b36d
Post7 id --- 5675727647b977030017b374   <-- why are they here now?
Post8 id --- 5675744847b977030017b375   <--
Post2 id --- 56756e4f47b977030017b36e
Post3 id --- 56756e8447b977030017b36f
Post4 id --- 56756ef747b977030017b370
Post5 id --- 56756f3f47b977030017b371
Post6 id --- 56756f7247b977030017b372

UPDATE: this is how I ended up getting oldest posts by _id:

Post.find({}, function(error, results) {
  if(error) {
    console.log('error in show');
  } else {
    console.log('show query successful');
    response.json(results);
  }
}).sort({_id:1})

Upvotes: 2

Views: 1734

Answers (3)

BalajiMalala
BalajiMalala

Reputation: 89

"Never recompute what you can precompute".

Maintain a seperate field "last_added" and whenever new post gets created, store that post's _id into that field. So it'll be more easy to retrieve details of last added post using that _id

Upvotes: 0

Himanshu sharma
Himanshu sharma

Reputation: 7881

It doesn't matter what the database showing if you see those hexadecimal id they are in ascending order as number.
if you want server side pagination in your page you can do like this

Posts.find({
    deleted: false
}).skip(skips).limit(limits).sort({_id:1}).exec(function (err, objects) {
    callback(null, objects);
})

skips means how much page count you skip .if you are in 1 page and page need 5 records means to search next page record your skips will we 5 so that first 5 record skips

limits means how much element you need at one time.

then sort({_id:1} means sorting in ascending and sort({_id:-1} means sorting in descending

Upvotes: 0

kulak
kulak

Reputation: 882

I don't think it can be achieved only by using Mongo id. The mongo id is computed based on time, connection id and few other things therefore it's not guaranteed to be linear with the creation time.

https://docs.mongodb.org/v3.0/reference/object-id/

Upvotes: 1

Related Questions