Answers for "generate some custom ids in mongoose"

0

generate some custom ids in mongoose

// (assuming db is a reference to a MongoDB database)
var counters = db.collection('counters');
var query = {'name': 'counterName'};
var order = [['_id','asc']];
var inc = {$inc:{'next':1}};
var options = {new: true, upsert: true};
counters.findAndModify(query, order, inc, options, function(err, doc) {

  if(err) {
    callback(err);
    return;
  }      

  var id = doc.next;
  callback(null, id);
});
Posted by: Guest on August-31-2021

Code answers related to "generate some custom ids in mongoose"

Code answers related to "Javascript"

Browse Popular Code Answers by Language