christkv/node-mongodb-native — Issue #527: Options passed to collection/db.ensureIndex ...
Author: Bwen
Created: 2012-03-01 02:34:24+00:00
State: open

I was trying to add a unique index to a collection but it always ended up to be false...

So I peeked thru this little function:
Collection.prototype.ensureIndex = function ensureIndex (fieldOrSpec, options, callback) {
// Clean up call
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
options = args.length ? args.shift() : {};
// Execute create index
this.db.ensureIndex(this.collectionName, fieldOrSpec, options, callback);
};
#####################################

I put the handling of the options in comments so the options get to the next function cleanly.

Then I looked to the next function:
Db.prototype.ensureIndex = function(collectionName, fieldOrSpec, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 2);
callback = args.pop();
options = args.length ? args.shift() : {};

// Figure out the safe mode settings
var safe = self.strict != null && self.strict == false ? true : self.strict;
// Override with options passed in if applicable
safe = options != null && options['safe'] != null ? options['safe'] : safe;
// Ensure it's at least set to safe
safe = safe == null ? true : safe;

// Create command
var command = DbCommand.createCreateIndexCommand(this, collectionName, fieldOrSpec, options);
####################################

I understand about the optional {safe:true} second parameter. But its clearly not working... because when I put all the options handling in comment I got my unique:true in the database.

Hope this get fixed soon,
Thx