title: findOneAndUpdate work in mongo shell, but doesn't update with node-mongodb?
body: <p>I have this function, which search for a document with a given ID (mID), check if another field exist (u) and if not, add the given id (uID).</p>
  
  <p>The code doesn't throw any error, but doesn't update the document either, however, building the query myself from the same field (the two <code>console.log</code>) and using them in the mongo shell does work.</p>
  
  <p>The result of the query when used with node-mongodb-native is <code>{ ok: 1, nModified: 0, n: 0 }</code></p>
  
  <pre><code>'use strict';
  
  const MongoClient = require('mongodb').MongoClient,
      async = require('async'),
      uuid = require('node-uuid'),
      winston = require('winston'),
      logger = new (winston.Logger)({transports: [new winston.transports.Console()]});
  
  let Link = {};
  
  function link(mId, base, uId, callback) {
      let filter = {'mId': mId},
          update = {'$set': {}};
  
      filter[base] = {'$exists': false};
      update['$set'][base] = uId;
  
      logger.info('link update', {filter: filter, update: update});
      console.log('db.Link.findOne(' + require('util').inspect(filter) + ');');
      console.log('db.Link.findOneAndUpdate(' + require('util').inspect(filter) + ', ' + require('util').inspect(update) + ', {upsert: false, new: true});');
      Link.collection('link')
      .findOneAndUpdate(
          filter,
          update,
          {
              upsert: false,
              returnOriginal: false
          }
      ).then((result) =&gt; {
          logger.info('link update ok', {result: result});
          callback();
      }).catch((error) =&gt; {
          logger.error('link update error', {error: error});
          callback(new Error(4299));
      });
  }
  
  async.waterfall([
      (callback) =&gt; {
          MongoClient.connect('mongodb://127.0.0.1/Link').then((db) =&gt; {
              logger.info('Connected to Link');
              Link = db;
              callback(null);
          }).catch((error) =&gt; {
              logger.error('init Link', {error: error});
              callback(error);
          });
      },
      (callback) =&gt; {
          let mId = uuid.v4();
          logger.info('create');
  
          Link.collection('Link')
          .insertOne({'mId': mId})
          .then((error) =&gt; {
              logger.info('create ok')
              callback(null, mId);
          }).catch((error) =&gt; {
              logger.error('create error', {error: error});
              callback(new Error(4299));
          });
      },
      (mId, callback) =&gt; {
          link(mId, 'link', uuid.v4(), callback);
      }
  ], (error) =&gt; {
      if(error) {
          logger.error('End', {error, error});
      }
      logger.info('End');
      Link.close();
  });
  </code></pre>
  
  <p>I also tried with <code>update</code> and <code>updateOne</code> functions, but same result: command work, not the code.</p>
  
  <p>Can anybody explain why a command that work from the shell couldn't work when made from the driver, and why Mongo report it found a document, but don't update it?</p>
  
  <p>node v6.9.1, node-mongodb-native v2.2.11</p>
  
  <p>Edit:</p>
  
  <p>Base document:</p>
  
  <pre><code>{
      "_id" : ObjectId("58332c30224fe3273c7b1ba6"),
      "mId" : "37286c83-7d81-484d-b62a-310f690cac97"
  }
  </code></pre>
  
  <p>Updated document:</p>
  
  <pre><code>{
      "_id" : ObjectId("58332c30224fe3273c7b1ba6"),
      "mId" : "37286c83-7d81-484d-b62a-310f690cac97",
      "test" : "f7bb9386-eedd-43fe-890a-348cb3a97ed3"
  }
  </code></pre>
  
  <p>Logger output:</p>
  
  <pre><code>info: Connected to Link
  info: create
  info: create ok
  info: link update mId=f8ba93da-3b6d-43f7-9f90-4e345ba04131, $exists=false, link=f882d44d-60a3-4701-b5df-ba493c3b249b
  db.Link.findOne({ mId: 'f8ba93da-3b6d-43f7-9f90-4e345ba04131',
    link: { '$exists': false } });
  db.Link.findOneAndUpdate({ mId: 'f8ba93da-3b6d-43f7-9f90-4e345ba04131',
    link: { '$exists': false } }, { '$set': { link: 'f882d44d-60a3-4701-b5df-ba493c3b249b' } }, {upsert: false, new: true});
  info: link update ok updatedExisting=false, n=0, value=null, ok=1
  info: End
  </code></pre>
  
  <p>The <code>init</code> function connect to mongoDB, the <code>create</code> function insert into it a new document with a random <code>mId</code> that is passed to the <code>link</code> function.
  The uId is also created randomly and is also a UUID.</p>
  
  <p>While it should be equivalent, the command printed in the console logs:</p>
  
  <pre><code>db.Link.findOneAndUpdate({ mId: 'f8ba93da-3b6d-43f7-9f90-4e345ba04131',
  </code></pre>
  
  <p>link: { '$exists': false } }, { '$set': { link: 'f882d44d-60a3-4701-b5df-ba493c3b249b' } }, {upsert: false});</p>
  
  <p>do update the document</p>