LearnBoost/mongoose — Issue #714: _id:{type:Number} causes RangeError: Maximum call stack size exceeded 
Author: sjoerdmulder
Created: 2012-02-03 08:32:41+00:00
State: open

When you create a model it's fine, but when then after try to manipulate that model (`remove` or `save` method on instance). Node crashes with a `RangeError: Maximum call stack size exceeded`

```javascript

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var db = mongoose.connect('mongodb://localhost/test');

var MySchema = new Schema({
_id: { type: Number, unique: true },
name: String
});

var MyModel = module.exports = mongoose.model('MyModel', MySchema);

//Autoincrement
MySchema.pre('save', function(next) {
if (this.isNew || !this._id) {
MyModel.find({},{_id:1}).sort('_id', -1).limit(1).find(function(err, items) {
this._id = items.length ? items[0]._id + 1 : 1;
next();
}.bind(this));
}
});

var instance = new MyModel({
name: 'test'
});

instance.save(function(err, instance) {
console.log(instance);
//Crash!
instance.remove();
});
```