title: How to skip from _id with mongoose?
body: <p>The problem with <code>.skip(num)</code> in mongoose is that while adding data to the collection, <code>skip(num)</code> could return data existed in previous search. A solution would be skip from _id but I failed to find such option.</p>
  
  <p>So rather than:</p>
  
  <p><code>db.collection.find().sort({ _id: -1 }).skip(num).limit(50);</code></p>
  
  <p>I like to be able to:</p>
  
  <p><code>db.collection.find().sort({ _id: -1 }).skip(_id).limit(50);</code></p>
  
  <p>Alternatively if I can get index of <code>_id</code> it should be fine too:</p>
  
  <pre><code>let res = await db.collection.findOne({_id});
  let {index} = res;
  db.collection.find().sort({ _id: -1 }).skip(index).limit(50);
  </code></pre>
  
  <p>I use Caffeinated.tech's method and tried:</p>
  
  <pre><code>const list = async function({
    end_cursor = ''
  } = {}) {
    return await db.discovery.where('_id').gt(end_cursor).sort({ _id: -1 }).limit(50);
  }
  </code></pre>
  
  <p>and I got</p>
  
  <pre><code>CastError: Cast to ObjectId failed for value "" at path "_id" for model "discovery"
  </code></pre>
  
  <p>The correct answer is</p>
  
  <pre><code>const list = async function(end_cursor) {
    if (!end_cursor) {
      return await db.discovery.where('_id').sort({ _id: -1 }).limit(50);
    }
    return await db.discovery.where('_id').lt(end_cursor).sort({ _id: -1 }).limit(50);
  }
  </code></pre>