function getNextSequence(name,cb) {
    getDB(function(db) {
        //console.log("getNextSequence",name);
        db.collection('counter', function(err, collection) {
            var ret = collection.findAndModify(
                     { _id: name },
                     [['_id','asc']],
                     { $inc: { seq: 1 } },
                     {new: true, upsert: true}
                     ,function (err, result) {
                       if (err)
                       {
                          console.log("errror",err);
                       }
                        //console.log("res===",result);
                        var toRet=result.seq;
                        // TODO, different results depending on mongodb version???
                        if (toRet)
                        {
                           cb(toRet);
                        }
                        else
                        {
                           toRet=result.value.seq;
                           cb(toRet);
                        }

                    });
        });
    });
}
Module that helps to work with mongodb DB
 DEPENDENCIES: mongodb

   Setting recid to 0  should create a new document unless record._id is given

    Note that mongo uses _id to uniquely identify documents.
    This code tries to set the _id to recid unless given from the record._id

    The code is not so fast but can get you started quickly


 To get a new correct recid you can use add-record to get a new recid for the next record
  i.e. for a grid

  Example of how to add a new record in a grid for later save.
   onAdd: function (event) {

        var ajaxOptions = {
            type     : 'POST',
            url      : '/api/whatever',
            data     : {
                  cmd: "add-record"
            },
            dataType : 'text'
         };

         $.ajax(ajaxOptions)
               .done(function (data, status, xhr) {
                  var resulData =  jQuery.parseJSON(data);
                  this.add(resulData.record);
               })
               .fail(function(jqXHR, textStatus, errorThrown) {
                    // Handle error
        });

  }


 TODO: Needs more error texts on errors from database.
 Smarter generation of the recid. Could use a counter collection,
 i.e. like this, http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/