index.js
function getNextId(db, collectionName, fieldName, callback) {
    if (typeof fieldName == "function") {
        callback = fieldName;
        fieldName = null;
    }

    fieldName = fieldName || getOption(collectionName, "field");
    var collection = db.collection(defaultSettings.collection);
    var step = getOption(collectionName, "step");

    collection.findAndModify(
        {_id: collectionName, field: fieldName},
        null,
        {$inc: {seq: step}},
        {upsert: true, new: true},
        function (err, result) {
            if (err) {
                if (err.code == 11000) {
                    process.nextTick(getNextId.bind(null, db, collectionName, fieldName, callback));
                } else {
                    callback(err);
                }
            } else {
                if (result.value && result.value.seq) {
                    callback(null, result.value.seq);
                } else {
                    callback(null, result.seq);
                }
            }
        }
    );
}
function getNextId(db, collectionName, fieldName, callback) {
    if (typeof fieldName == "function") {
        callback = fieldName;
        fieldName = null;
    }

    fieldName = fieldName || getOption(collectionName, "field");
    var collection = db.collection(defaultSettings.collection);
    var step = getOption(collectionName, "step");

    collection.findAndModify(
        {_id: collectionName, field: fieldName},
        null,
        {$inc: {seq: step}},
        {upsert: true, new: true},
        function (err, result) {
            if (err) {
                if (err.code == 11000) {
                    process.nextTick(getNextId.bind(null, db, collectionName, fieldName, callback));
                } else {
                    callback(err);
                }
            } else {
                if (result.value && result.value.seq) {
                    callback(null, result.value.seq);
                } else {
                    callback(null, result.seq);
                }
            }
        }
    );
}
function getNextId ( db , collectionName , fieldName , callback ) { if ( typeof fieldName == "function" ) { callback = fieldName ; fieldName = null ; } fieldName = fieldName || getOption ( collectionName , "field" ) ; var collection = db . collection ( defaultSettings . collection ) ; var step = getOption ( collectionName , "step" ) ; collection . findAndModify ( { _id : collectionName , field : fieldName } , null , { $inc : { seq : step } } , { upsert : true , new : true } , function ( err , result ) { if ( err ) { if ( err . code == 11000 ) { process . nextTick ( getNextId . bind ( null , db , collectionName , fieldName , callback ) ) ; } else { callback ( err ) ; } } else { if ( result . value && result . value . seq ) { callback ( null , result . value . seq ) ; } else { callback ( null , result . seq ) ; } } } ) ; }
Get next auto increment index for the given collection
@param {MongodbNativeDriver} db
@param {String} collectionName
@param {String} [fieldName]
@param {Function} callback
Get next auto increment index for the given collection