📜  在 mongoose 中生成一些自定义 id - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:20.960000             🧑  作者: Mango

代码示例1
// (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);
});