title: Obtaining MongoDb ObjectId in Node in ASCII
body: <p>On querying a MongoDb database via the <code>mongo</code> CLI (v3.0.7), documents and their ObjectIds are returned looking like:</p>
  
  <pre><code>{ "_id" : ObjectId("566ef12723b89d23fa5003fe"),
    "address": "30 Pair Road",
    ...
  }
  </code></pre>
  
  <p>But performing a similar search using the Node MongoDb library (mongodb@2.0.52, mongodb-core@1.2.28) returns:</p>
  
  <pre><code>{ _id: { _bsontype: 'ObjectID', id: 'Vnñ\'#¸#úP\u0003þ' },
    address: '30 Pair Road',
    ...
  }
  </code></pre>
  
  <p>I'm trying to figure out how to convert the <code>id</code> value of <code>'Vnñ\'#¸#úP\u0003þ'</code> back into the 24 hexadecimal characters of <code>"566ef12723b89d23fa5003fe"</code> so that I can query the database by it (as querying with an _id of 'Vnñ\'#¸#úP\u0003þ' obviously doesn't work).</p>
  
  <p>The later call includes a call to <code>toArray</code> on the cursor returned from the collection which I guess is partially deserialising the ObjectId instance.</p>
  
  <p><strong>**edit**</strong></p>
  
  <p>I put in a logging statement into the function accepting the result from <code>toArray</code> which showed:</p>
  
  <pre><code>{ _id : 566ef12723b89d23fa5003fe,
    address: "30 Pair Road",
    ...
  }
  </code></pre>
  
  <p>The garbled value I was seeing is coming from two locations.  One in the node debug repl:</p>
  
  <pre><code>&gt; users[0]
  { _id: { _bsontype: 'ObjectID', id: 'Vnñ\'#¸#úP\u0003þ' ... }
  &gt; users[0]._id.toString()
  '567b1a7d818d826f1dab9c6f'
  &gt; users[0]._id.id.toString()
  'Vnñ\'#¸#úP\u0003þ'
  </code></pre>
  
  <p>And then secondly when I was serialising it.  I am now calling <code>toString</code> explicitly on the _id field and it's behaving correctly.</p>