title: Unique index ignored when updating with Mongoose + Mockgoose in NodeJS
body: <p>I'm currently using Mongoose ODM to manage database connections to MongoDB in a NodeJS application, and intercepting the connection using Mockgoose in Mocha tests. I've ran into an issue where my unique indexes are being ignored when performing an update to a document. I'm simply wrapping Mongoose with another package called Mongoose-bird, which just enables the use of promises.</p>
  
  <p>One schema in particular is as follows:</p>
  
  <pre><code>// Gallery.js
  'use strict';
  
  var mongoose = require('mongoose-bird')(require("mongoose"));
  var Schema = mongoose.Schema;
  var ObjectId = Schema.Types.ObjectId;
  
  var deepPopulate = require('mongoose-deep-populate')(mongoose);
  
  var GallerySchema = new Schema({
      _id: ObjectId,
      type: String,
      title: String,
      slug: String,
      _albums: [{
          type: ObjectId,
          ref: 'Albums'
      }]
  });
  
  GallerySchema.index({ slug: 1 }, { unique: true });
  GallerySchema.plugin(deepPopulate, {});
  mongoose.model('Galleries', GallerySchema);
  </code></pre>
  
  <p>When calling <code>Gallery.update(conditions, data, opts)</code> from my controller in a test, purposely setting the <code>slug</code> to be a duplicate of another, it updates the document and then I end up with two documents with the same <code>slug</code> path.</p>
  
  <p>FYI, I've found a way around this by using the <code>save()</code> function instead, which seems to obey the unique index without any questions.</p>
  
  <p>However, as I'd prefer to use <code>update()</code> over <code>save()</code> (i.e. to update documents partially as opposed to the whole document each time), I'm interested to know if anyone else has had this same issue and how you have overcome it?</p>
  
  <p>The application follows a standard MVC pattern based on MEAN.js so there's a bit more to it than just one model, though if I've left out anything which may be useful, please let me know.</p>
  
  <p><strong>Update</strong><br/>
  After looking into the source code of the Mockgoose NPM module, I can confirm the validation against the schema is never performed when running update(). There is an issue logged here: <a href="http://www.github.com/mccormicka/Mockgoose/issues/58" rel="nofollow">http://www.github.com/mccormicka/Mockgoose/issues/58</a></p>