2016-05-29 53 views
0

我想有一个像关系或目标数据库的准确自动递增字段,所以我需要自动设置字段值的整数_id场,值应该是一个更加最后一个记录_id值是这样的:如何在nedb中使用自动增量字段?

数据:

{_id:1,name"foo"} 
{_id:2,name"bar"} 

删除最后一个记录:

{_id:1,name"foo"} 

添加新的记录:

{_id:1,name"foo"} 
{_id:3,name"newbar"} 

我增加了一个功能,我的数据存储和计算_id的最大和加1 max(_id)+1并设置为字段值,但这里有问题:

当我们在关系数据库中使用自动递增字段,它就像我说,在你之后remove last record它保留了一个已删除的记录号码,并且新插入的记录继续递增,但在我的方式中,它表示删除了新记录的记录的_id。

我的代码是:

var Datastore = require('nedb'), 
localDb = new Datastore({ 
    filename: __dirname + '/dbFilePath.db', 
    autoload: true 
}); 

localDb.getMax = function(fieldName, onFind){ 
    db.find({}).sort({_id:-1}).limit(1).exec(function (err, docs) {onFind && onFind(err, docs['_id']);}); 
    return localDb; 
} 

localDb.insertAutoId = function(data, onAdd){ 
    var newIndex = 0; 
    localDb.getMax(function (err, maxValue) { 
     newIndex = maxValue+1; 

     if(!data["_id"]) 
      data["_id"] = newIndex; 

     localDb.insert(data, function (err, newDoc) { 
      onAdd && onAdd(err, newDoc); 
     }); 
    }); 
    return localDb; 
} 

回答

2

可以存储在数据库索引的最后一个值。事情是这样的:

var Datastore = require('nedb'); 
var db = new Datastore({ 
    filename: __dirname + '/dbFilePath.db', 
    autoload: true 
}); 

// Initialize the initial index value 
// (if it already exists in the database, it is not overwritten) 
db.insert({_id: '__autoid__', value: -1}); 

db.getAutoId = function(onFind) { 
    db.findOne({ _id: '__autoid__' }, function(err, doc) { 
    if (err) { 
     onFind && onFind(err) 
    } else { 
     // Update and returns the index value 
     db.update({ _id: '__autoid__'}, { $set: {value: ++doc.value} }, {}, 
     function(err, count) { 
      onFind && onFind(err, doc.value); 
     }); 
    } 
    }); 
    return db; 
} 
2

nedb改进的答案是:

db.getAutoincrementId = function (cb) { 
    this.update(
     { _id: '__autoid__' }, 
     { $inc: { seq: 1 } }, 
     { upsert: true, returnUpdatedDocs: true }, 
     function (err, affected, autoid) { 
      cb && cb(err, autoid.seq); 
     } 
    ); 
    return this; 
}; 

这相当于MongoDB的方式:

db.getAutoincrementId = function (cb) { 
    this.findAndModify({ 
      query: { _id: '__autoid__' }, 
      update: { $inc: { seq: 1 } }, 
      new: true 
     } 
     function (err, autoid) { 
      cb && cb(err, autoid.seq); 
     } 
    ); 
    return this; 
}; 
1

我不知道它将对你有用,我使用数据库来存储下一个id,灵感来自于mysql系统。谁总是保留下一个ID。 所以我创建了一个函数来验证数据库是否有一个id,如果没有,它会添加值“1”,并且当它更新时会查找它是否存在并执行序列。 这给了我完全控制我的ID。 的模式将是:

{ 
name: nameDb, 
nextId: itemID 
} 

如果你愿意,你可以更新的文件,版本创建功能等

例如:

db.autoincrement = new Datastore({filename: 'data/autoincrement.db', autoload: true}); 

function getUniqueId(nameDb, cb) { 
    db.autoincrement.findOne({name: nameDb}, function (err, doc) { 
     if (err) { 
      throw err; 
     } else { 
      if (doc) { 
       const itemID = doc.nextId + 1; 
       db.autoincrement.update({name: nameDb}, { 
        name: nameDb, 
        nextId: itemID 
       }, {}, function (err, numReplaced) { 
        db.autoincrement.persistence.compactDatafile(); 
        if (err) { 
         throw err; 
        } else { 
         // console.log(numReplaced); 
        } 
        cb(doc.nextId); 
       }); 
      } else { 
       const data = { 
        name: nameDb, 
        nextId: 2 
       }; 

       db.autoincrement.insert(data, function (err, newDoc) { 
        if (err) { 
         throw err; 
        } else { 
         // console.log(newDoc); 
        } 
        cb(1); 
       }); 
      } 
     } 

    }); 
} 

插入新文档,例如:

 function insert(req, cb) { 
     getUniqueId("testdb", function (uniqueId) { 
      data.itemId = uniqueId; 

      db.testdb.insert(data, function (err, newDoc) { 
       if (err) { 
        cb({error: '1', message: 'error#2'}); 
        throw err; 
       } 
       cb({error: '0', message: 'Item add'}); 
      }); 

     }); 
     }