Node.js 如何在nedb中设置自动递增字段?
我想要一个像关系数据库或目标数据库一样的自动递增字段,所以我需要一个带有自动设置字段值的整数Node.js 如何在nedb中设置自动递增字段?,node.js,nedb,nosql,Node.js,Nedb,Nosql,我想要一个像关系数据库或目标数据库一样的自动递增字段,所以我需要一个带有自动设置字段值的整数\u id字段,值应该是最后一个记录\u id值,如下所示: var Datastore = require('nedb'); var db = new Datastore({ filename: __dirname + '/dbFilePath.db', autoload: true }); // Initialize the initial index value // (if it
\u id
字段,值应该是最后一个记录\u id
值,如下所示:
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;
}
数据:
删除最后一条记录:
{_id:1,name"foo"}
添加新记录:
{_id:1,name"foo"}
{_id:3,name"newbar"}
我在数据存储中添加了一个函数,计算_id的最大值和+1max(_id)+1
,并设置为字段值,但这里有一个问题:
当我们在关系数据库中使用auto increment字段时,它的工作原理与我所说的一样,在您删除最后一条记录后,它保留了一个已删除的记录编号,新插入的记录继续递增,但以我的方式,它表示新记录的已删除记录的_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;
}
您可以将索引的最后一个值存储在数据库中。大概是这样的:
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;
}
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;
};
{
name: nameDb,
nextId: itemID
}
这相当于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;
};
我不知道它是否对你有用,我使用数据库存储下一个ID,灵感来自mysql系统。他总是保留下一个身份证。 因此,我创建了一个函数来验证数据库是否有id,如果没有,则添加值“1”,当它更新时,会查找它,如果它存在,并执行序列。 这让我可以完全控制我的身份证。 模式将是:
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;
};
{
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'});
});
});
}