title: MongoDB ObjectID in JS
body: <p>I am trying to make pagination work with MongoDB without skip();</p>
  
  <p>In mongo shell I got the following results with my query, but in Javascript a empty [];</p>
  
  <p>I think I am doing the ObjectID wrong, I use the "mongodb ObjectID" and "mongojs" libs with Node.js.</p>
  
  <p><strong>Mongo shell:</strong></p>
  
  <pre><code>db.chat.find({
    _id: { $lt: ObjectId("53e901c125c68270311e5f41") },
    user_id: 1,
    target_user_id: 1,
    "$or": [{user_id: 1, target_user_id:1}]
  }).sort({
    ts: -1
  }).limit(5);
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>{ "_id" : ObjectId("53e88e1bb76e781413000029"), "user_id" : 1, "target_user_id" : 1, "message" : "Hey" }
  { "_id" : ObjectId("53e88f51b76e78141300002a"), "user_id" : 1, "target_user_id" : 1, "message" : "Hey" }
  //ect.
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>var ObjectID            = require('mongodb').ObjectID;
  var db                  = require("mongojs").connect(db_uri, db_collections);
  
  //last_ts = "53e901c125c68270311e5f41"
  var last_id = ObjectID.createFromHexString(last_ts);
  
  db.chat.find({
    _id: { $lt: last_id },
    user_id: 1,
    target_user_id: 1,
    "$or": [{user_id: 1, target_user_id:1}]
  }).sort({
    ts: -1
  }).limit(5).toArray(function (err, docs) {
    console.log("docs:"+docs); //"docs" - no result
    console.log("err:"+err); //"err:null"
  
    if(docs != null){
      console.log(JSON.stringify(docs)); //"docs:[]"
    }
  });
  </code></pre>
  
  <p>How can I get the same result, with my query in JS?</p>
  
  <p><strong>Edit</strong> with $oid from the docs:</p>
  
  <p><a href="http://docs.mongodb.org/manual/reference/mongodb-extended-json/#oid" rel="nofollow">http://docs.mongodb.org/manual/reference/mongodb-extended-json/#oid</a></p>
  
  <p>Still not working..</p>
  
  <pre><code>var last_ts = "53e901c125c68270311e5f41";
  db.chat.find({_id: {$lt: {"$oid": last_ts}}, user_id:1, target_user_id:1, "$or": [{user_id: 1, target_user_id:1}]}).sort({ts: -1}).limit(5)
  </code></pre>
  
  <p><strong>Edit</strong>
  Now working with simply as:</p>
  
  <pre><code>var last_ts = "53e901c125c68270311e5f41";
  new ObjectID(last_ts)
  </code></pre>