title: How to expose MongoDB documents primary keys in a REST API?
body: <p>I am building a REST API with MongoDB + nodeJS. All the documents are stored and are using _id as the primary key. I've read <a href="http://toddfredrich.com/ids-in-rest-api.html" rel="nofollow noreferrer">here</a> that we should not expose the _id and we should use another ID which is not incremental. </p>
  
  <p>In the DB, a document is represented as:</p>
  
  <pre><code>{ 
     _id: ObjectId("5d2399b83e9148db977859ea")
     bookName: "My book"
  }
  </code></pre>
  
  <p>For the following the endpoints, how should the documents be exposed?</p>
  
  <pre><code>GET /books
  GET /books/{bookId}
  </code></pre>
  
  <p>Currently my API returns:</p>
  
  <pre><code>{ 
     _id: "5d2399b83e9148db977859ea"
     bookName: "My book"
  }
  </code></pre>
  
  <p>but should it instead return something like:</p>
  
  <pre><code>{ 
     id: "some-unique-id-generated-on-creation"
     bookName: "My book"
  }
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ul>
  <li><p>Should I expose the _id so that one can make queries such as:</p>
  
  <p>GET /books/5d2399b83e9148db977859ea</p></li>
  <li><p>Should I use a UUID for my ID instead of ObjectId? </p></li>
  <li>Should I keep the internal _id (but never expose it) and create another attribute id which would use UUID or another custom generated ID ?</li>
  <li>Is it a good practice to work with _id in my backend or should I only make queries using my own custom ID? Example: find({ id:  }) instead of find({ _id:  })</li>
  </ul>