Alleria
Alleria

Reputation: 21

Mongoskin on nodejs does not report duplicate error

I've tried inserting the same entry to mongodb twice on nodejs with mongoskin. Unique index was specified for the collection. However, no error was given on the second insertion. What should I do to detect if there is a duplicate error?

Thanks!

Upvotes: 1

Views: 266

Answers (1)

jared
jared

Reputation: 5539

Be sure you're using safe mode. To use safe mode as the default for your connection you can specify it as a parameter when creating the connection. From the mongoskin docs:

var db = mongoskin.db([
  '192.168.0.1:27017/?auto_reconnect=true',
  '192.168.0.2:27017/?auto_reconnect=true',
  '192.168.0.3:27017/?auto_reconnect=true'
], {
  database: 'testdb',
  safe: true
}, {
  connectArbiter: false,
  socketOptions: {
    timeout: 2000
  }
});

Safe mode instructs the client to call getLastError after each operation, which is how it knows whether there was an error. So you usually want to be sure to have safe mode on!

Upvotes: 4

Related Questions