title: Mongoose FindOne not working directly after create
body: <p>I am setting up seed data with the goal of only creating a record if it does not already exist.</p>
  
  <p>I am using the <a href="https://www.npmjs.com/package/mongoose-find-one-or-create" rel="nofollow noreferrer">https://www.npmjs.com/package/mongoose-find-one-or-create</a> plugin to only create records if they do not exist and it is not working.</p>
  
  <p>I have also debugged into their code and written my own tests doing the following.</p>
  
  <ul>
  <li>Create Record - Name = 'Abc'</li>
  <li>Find Record where Name = 'Abc'</li>
  <li>if found</li>
  <li>&nbsp; &nbsp; <strong>print found</strong></li>
  <li>if NOT found</li>
  <li>&nbsp; &nbsp; <strong>Create Record - Name = 'Xyz'</strong></li>
  </ul>
  
  <p>I was expecting to see the message 'Found', but instead I am getting a new record called 'Xyz'</p>
  
  <p>I have a feeling it maybe some sort of asynchronous issue but I am new to mongo so not totally sure.</p>
  
  <p>Here is all my test code, plus the code from the <a href="https://www.npmjs.com/package/mongoose-find-one-or-create" rel="nofollow noreferrer">plugin</a>.</p>
  
  <p><strong>Scheme Definition for Account Table</strong></p>
  
  <pre><code>'use strict';
  
  var mongoose = require('mongoose');
  var findOneOrCreate = require('mongoose-find-one-or-create');
  
  var Schema = mongoose.Schema;
  
  var AccountSchema = new Schema({
    name: String,
    type: String,
    notes: String,
    locationIds: String,
  });
  
  
  AccountSchema.plugin(findOneOrCreate);
  
  module.exports = mongoose.model('Account', AccountSchema);
  </code></pre>
  
  <p><strong>Seed File for creating new rows</strong></p>
  
  <pre><code>'use strict';
  
  var Account = require('../../api/account/account.model');
  
  Account.create({ name: 'Abc' }, function (err, small) {
  });
  
  Account.findOne({ name: 'Abc' }, function (err, row) {
  
    console.log("Inside Find One");
    if (row) {
      console.log("Found: Yes");
      console.log(row);
    } else {
      console.log("NOT FOUND");
      Account.create({ name: 'Xyz' }, function (err, small) {
      });
    }
  });
  </code></pre>
  
  <p><strong>The FindOneOrCreatePlugin code</strong></p>
  
  <pre><code>'use strict';
  
  /**
   * Mongoose Plugin: findOneOrCreate
   * Copyright(c) 2014 Mohammad Khan &lt;mohammad.khan@gmx.us&gt;
   * MIT Licensed
  **/
  
  function findOneOrCreatePlugin(schema) {
    //console.log('findOneOrCreatePlugin: SETUP');
      schema.statics.findOneOrCreate = function findOneOrCreate(condition, doc, callback) {
          var self = this;
          //console.log('CONDITION');
          //console.log('----------------------------------------------------------------------------------------------------');
          //console.log(condition);
          //console.log('----------------------------------------------------------------------------------------------------');
  
          self.findOne(condition, function(err, result ) {
            //console.log('ERROR');
            //console.log('----------------------------------------------------------------------------------------------------');
            //console.log(err);
            //console.log('----------------------------------------------------------------------------------------------------');
            //console.log('RESULT');
            //console.log('----------------------------------------------------------------------------------------------------');
            //console.log(result);
            //console.log('----------------------------------------------------------------------------------------------------');
  
            if (result) {
              //console.log('----------------------------------------------------------------------------------------------------');
              //console.log('YEEEEEEEEEEEEEEY an UPDATE');
              //console.log('----------------------------------------------------------------------------------------------------');
  
                  callback(err, result);
            } else {
              //console.log('CREATE');
              //console.log('----------------------------------------------------------------------------------------------------');
              //console.log(doc);
              //console.log('----------------------------------------------------------------------------------------------------');
  
                  self.create(doc, function(err, result) {
                      callback(err, result);
                  });
              }
          });
      };
  }
  
  module.exports = findOneOrCreatePlugin;
  </code></pre>
  
  <p><strong>Query in Mongo Shell</strong></p>
  
  <p><a href="https://i.stack.imgur.com/kWVM0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kWVM0.png" alt="Mongo Shell Output"></a></p>
  
  <p><strong>Console Output in Web Server</strong></p>
  
  <p><a href="https://i.stack.imgur.com/1NYqj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1NYqj.png" alt="Console Output in Web Server"></a></p>
  
  <p>**Here is the use case that I'm attempting to deal with **</p>
  
  <p>NOTE: this is part of a set of files generated by end users with a </p>
  
  <p>The USE case I was attempting to resolve was if the user accidently put the same account in twice.</p>
  
  <p>This code is essentially meant to be self-healing when users accidently put duplicates in, it is only a seed system for populating a new application. </p>
  
  <p>I do plan to use this on the product table which has 1000's of products.</p>
  
  <p>According to @RobertMoskal, I need to put the calls inside of call back functions but then I'll have huge nested callbacks, 1 for each product.</p>
  
  <pre><code>// Create account called 'Chicane'
  Account.findOneOrCreate(
      { name: 'Chicane'},
      {
          name: 'Chicane',
          type: 'Orderer',
      },
      function (err, account) {
          console.log('Account.findOneOrCreate');
          console.log(account);
      }
  );
  
  // Create account called 'Campos'
  Account.findOneOrCreate(
      { name: 'Campos'},
      {
          name: 'Campos',
          type: 'Supplier',
      },
      function (err, account) {
          console.log('Account.findOneOrCreate');
          console.log(account);
      }
  );
  
  // Create account called 'Chicane', this has already been created so it should NOT be created again, but it is
  Account.findOneOrCreate(
      { name: 'Chicane'},
      {
          name: 'Chicane',
          type: 'Orderer',
      },
      function (err, account) {
          console.log('Account.findOneOrCreate');
          console.log(account);
      }
  );
  </code></pre>