guileen/node-mongoskin — Issue #20: E11000 is not being forwarded to the callback of Collection#insert
Author: humanchimp
Created: 2011-11-29 01:20:12+00:00
State: open

Hi there —

I'm having a problem because the E11000 error for a non-unique value is not being forwarded to my callback. Consider the following code for the latest mongoskin (0.2.1)

```javascript
var db, mongo;

mongo = require('mongoskin');

db = mongo.db('localhost:27017/test');

db.collection('unique_index_fails').ensureIndex({qux: 1}, {unique: true}, function() {
var docs;

docs = [{qux: 'same'}, {qux: 'different'}, {qux: 'same'}]; // Oops, two qux have the same value

db.collection('unique_index_fails').insert(docs, function(err) {
console.log('err should be 11000', err); // err is null

db.collection('unique_index_fails').find(function(err, results) {
results.toArray(console.log); // only have one document with {qux:'same'}
});
});
});
```
Console output:

err should be 11000 null
null [ { qux: 'same', _id: 4ed42f9b6b159afb75000001 },
{ qux: 'different', _id: 4ed42f9b6b159afb75000002 } ]

I tested with mongoskin 0.2.1 and mongodb 2.0.1