title: Mongoose - Create document if not exists, otherwise, update- return document in either case
body: <p>I'm looking for a way to refactor part of my code to be shorter and simpler, but I don't know Mongoose very well and I'm not sure how to proceed.</p>
  
  <p>I am trying to check a collection for the existence of a document and, if it doesn't exist, create it. If it does exist, I need to update it. In either case I need to access the document's contents afterward.</p>
  
  <p>What I've managed to do so far is query the collection for a specific document and, if it's not found, create a new document. If it is found, I update it (currently using dates as dummy data for this). From there I can access either the found document from my initial <code>find</code> operation or the newly saved document and this works, but there must be a better way to accomplish what I'm after.</p>
  
  <p>Here's my working code, sans distracting extras.</p>
  
  <pre><code>var query = Model.find({
      /* query */
  }).lean().limit(1);
  
  // Find the document
  query.exec(function(error, result) {
      if (error) { throw error; }
      // If the document doesn't exist
      if (!result.length) {
          // Create a new one
          var model = new Model(); //use the defaults in the schema
          model.save(function(error) {
              if (error) { throw error; }
              // do something with the document here
          });
      }
      // If the document does exist
      else {
          // Update it
          var query = { /* query */ },
              update = {},
              options = {};
  
          Model.update(query, update, options, function(error) {
              if (error) { throw error; }
              // do the same something with the document here
              // in this case, using result[0] from the topmost query
          });
      }
  });
  </code></pre>
  
  <p>I've looked into <code>findOneAndUpdate</code> and other related methods but I'm not sure if they fit my use case or if I understand how to use them correctly. Can anyone point me in the right direction?</p>
  
  <p>(Probably) Related questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/16882938">How to check if that data already exist in the database during update (Mongoose And Express)</a></li>
  <li><a href="https://stackoverflow.com/q/13337685">Mongoose.js: how to implement create or update?</a></li>
  <li><a href="https://stackoverflow.com/q/13010290">NodeJS + Mongo: Insert if not exists, otherwise - update</a></li>
  <li><a href="https://stackoverflow.com/q/20667739">Return updated collection with Mongoose</a></li>
  </ul>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>I didn't come across the question pointed out to me in my searching, but after reviewing the answers there I've come up with this. It's certainly prettier, in my opinion, and it works, so unless I'm doing something horribly wrong I think my question can probably be closed.</p>
  
  <p>I would appreciate any additional input on my solution.</p>
  
  <pre><code>// Setup stuff
  var query = { /* query */ },
      update = { expire: new Date() },
      options = { upsert: true };
  
  // Find the document
  Model.findOneAndUpdate(query, update, options, function(error, result) {
      if (!error) {
          // If the document doesn't exist
          if (!result) {
              // Create it
              result = new Model();
          }
          // Save the document
          result.save(function(error) {
              if (!error) {
                  // Do something with the document
              } else {
                  throw error;
              }
          });
      }
  });
  </code></pre>