title: MongoDB and composite primary keys
body: <p>I'm trying to determine the best way to deal with a composite primary key in a mongo db. The main key for interacting with the data in this system is made up of 2 uuids. The combination of uuids is guaranteed to be unique, but neither of the individual uuids is.</p>
  
  <p>I see a couple of ways of managing this:</p>
  
  <ol>
  <li><p>Use an object for the primary key that is made up of 2 values (as suggested <a href="https://stackoverflow.com/a/7225657/13181">here</a>)</p></li>
  <li><p>Use a standard auto-generated mongo object id as the primary key, store my key in two separate fields, and then create a composite index on those two fields</p></li>
  <li><p>Make the primary key a hash of the 2 uuids</p></li>
  <li><p>Some other awesome solution that I currently am unaware of</p></li>
  </ol>
  
  <p>What are the performance implications of these approaches? </p>
  
  <p>For option 1, I'm worried about the insert performance due to having non sequential keys. I know this can kill traditional RDBMS systems and I've seen indications that this could be true in MongoDB as well.</p>
  
  <p>For option 2, it seems a little odd to have a primary key that would never be used by the system. Also, it seems that query performance might not be as good as in option 1. In a traditional RDBMS a clustered index gives the best query results. How relevant is this in MongoDB?</p>
  
  <p>For option 3, this would create one single id field, but again it wouldn't be sequential when inserting. Are there any other pros/cons to this approach?</p>
  
  <p>For option 4, well... what is option 4?</p>
  
  <p>Also, there's some discussion of possibly using CouchDB instead of MongoDB at some point in the future. Would using CouchDB suggest a different solution?</p>
  
  <p><strong>MORE INFO:</strong> some background about the problem can be found <a href="https://softwareengineering.stackexchange.com/questions/236309/strategy-for-generating-unique-and-secure-identifiers-for-use-in-a-sometimes-of">here</a></p>