title: How to delete rows within an interval in MongoDb
body: <p>I have a collection with lot of rows, for instance:</p>
  
  <pre><code>{ "_id" : 1, "state" : "1+" }
  </code></pre>
  
  <p>I want to set up a cron tab in order to remove the first N rows in my collection.</p>
  
  <p>I tried:</p>
  
  <pre><code>db.history.remove(
      {
          _id : 
          { 
              $lt : db.history.find().sort({_id:1}).limit(1)._id + N
          }
          , 
          $atomic : true 
      }
  );
  </code></pre>
  
  <p>Where <code>N</code> is the number of rows to remove, and then I will add this string inside the <code>--eval</code> in my cron task, but this command return nothing.</p>
  
  <p>What am I doing wrong? I can probably write a server side function with <code>N</code> in parameter...</p>