title: MongoDB locking database for a single document - is it optimizable?
body: <p>I've got a mongo instance that serves dictionary knowledge to other services that concurrently read/update the collection. I've noticed (with slowlog) that some queries take a long time to do what they should - because they are locking the database and other queries queue. There are multiple indexes and compound index on the collection, so it only hits one document. There are aprox. 1million records in the collection.</p>
  
  <p>I am bewildered why mongo needs to lock global, database AND collection? The modification only takes place on one document within one collection of one database.</p>
  
  <p>Since I am using <code>WiredTiger</code>, I would assume the lock would only be on the document itself? How can I optimize the performance?</p>
  
  <p>I don't care if other concurrent reads get an "old" version or that a concurrent write tries to create the same document twice (upsert) as the unique index would just throw an error and let my service handle it. Are there other optimizations I could consider?</p>
  
  <p>the slowlog message I get is following (formatted for readability)</p>
  
  <pre><code>command myDB.words
      command: findAndModify { 
          findandmodify: "words",
          query: { lang: "en", name: "cat" },
          new: true,
          remove: false,
          upsert: true,
          fields: {},
          update: { 
              $setOnInsert: { __v: 0 },
              $inc: { count: 2 },
              $addToSet: { 
                  type: { $each: [ 0 ] },
                  occured: ObjectId('....')
              },
              $set: { lang: "en", name: "cat" }
          },
          writeConcern: { w: 1 }
      }
      planSummary:
          IXSCAN { name: 1, lang: 1 }
          update: { .... }
          keysExamined:1
          docsExamined:1
          nMatched:1
          nModified:1
          keysInserted:1
          numYields:0
          reslen:2649211
          locks:{
              Global: { acquireCount: { r: 1, w: 1 } },
              Database: { acquireCount: { w: 1 } },
              Collection: { acquireCount: { w: 1 } }
          }
      protocol:op_query
      754ms
  </code></pre>
  
  <p>Following indexes exist;</p>
  
  <ul>
  <li>indexes on <code>lang</code> and <code>name</code></li>
  <li>compound index { name:1, lang:1 } </li>
  </ul>
  
  <p>If it matters, i am using <code>mongoose</code> and the services connecting to mongdb are running on node8.4. MongoDB 3.4.7</p>