title: Mongoose error findByIdAndUpdate fails in cast
body: <p>Trying to update a document using findByIdAndUpdate, i get an error that i don't understand.</p>
  
  <pre><code>    console.log(req.body);
      var data = req.body;
      data._id = undefined;
      Package.findByIdAndUpdate(req.params.id, data, function (err, pkg) {
          if (err) {
              console.log(err.stack);
              return next(restify.InternalServerError(err));
          }
          res.json(pkg);
          next();
      });
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>TypeError: Cannot read property '_id' of undefined
  at ObjectId.cast (/home/ubuntu/workspace/server/node_modules/mongoose/lib/schema/objectid.js:109:12)
  at ObjectId.castForQuery (/home/ubuntu/workspace/server/node_modules/mongoose/lib/schema/objectid.js:165:17)
  at Query._castUpdateVal (/home/ubuntu/workspace/server/node_modules/mongoose/lib/query.js:2009:17)
  at Query._walkUpdatePath (/home/ubuntu/workspace/server/node_modules/mongoose/lib/query.js:1969:25)
  at Query._castUpdate (/home/ubuntu/workspace/server/node_modules/mongoose/lib/query.js:1865:23)
  at castDoc (/home/ubuntu/workspace/server/node_modules/mongoose/lib/query.js:2032:18)
  at Query._findAndModify (/home/ubuntu/workspace/server/node_modules/mongoose/lib/query.js:1509:17)
  at Query.findOneAndUpdate (/home/ubuntu/workspace/server/node_modules/mongoose/node_modules/mquery/lib/mquery.js:2056:15)
  at Function.Model.findOneAndUpdate (/home/ubuntu/workspace/server/node_modules/mongoose/lib/model.js:1250:13)
  at Function.Model.findByIdAndUpdate (/home/ubuntu/workspace/server/node_modules/mongoose/lib/model.js:1344:32)
  </code></pre>
  
  <p>I have verified that the id is valid, data is a valid object as well.</p>
  
  <p>My model: </p>
  
  <pre><code>mongoose.model('Package', {
      name: {
          required: true,
          type: String
      },
      servers: [mongoose.Schema.Types.ObjectId],
      packageType: {
          type: String,
          enum: ['package', 'subscription']
      },
      subscriptionPeriodInDays: Number,
      pointsIncluded: Number,
      price: Number,
      rank: String,
  
      data: mongoose.Schema.Types.Mixed //For custom solutions
  });
  </code></pre>
  
  <p>The log also prints a valid <code>data</code> object</p>
  
  <pre><code>{
      name: 'Your Package',
      packageType: 'subscription',
      subscriptionPeriodInDays: 30,
      pointsIncluded: 10000,
      price: 10,
      rank: 'Donator',
      _id: undefined,
      __v: 0,
      servers: [],
      description: '&lt;p&gt;test&lt;/p&gt;\n'
  }
  </code></pre>
  
  <p>I have tried to step trough with the debugger but i couldn't find a reason for this.</p>