首先,哪一个更好呢?findAndModify
、findOneAndUpdate
还是findByIdAndUpdate
?
在我的情况下,我有这样一张表:
seqkeys
{
"_id" : "invoice",
"seq" : NumberInt(1)
},
{
"_id" : "receipt",
"seq" : NumberInt(1)
}
我希望找到发票的序列号,加1并保存。(然后使用该编号保存发票表中的实际发票记录)
但我无法让其正常工作,我一直得到
TypeError: seqkeysTable.findAndModify is not a function
下面是我的模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var seqkeysSchema = new Schema({
_id: String, // Table Name
seq: Number // Sequence Number
});
module.exports = mongoose.model('seqkeysModel', seqkeysSchema,'seqkeys');
这里是我的findkey
Node.js函数...
var seqkeysModel = require('../../models/seqkeysModel');
var seqkeysTable = mongoose.model('seqkeysModel');
var findKey = function(tableName) {
return new Promise((resolve, reject) => {
seqkeysTable.findAndModify(
{ "_id": tableName },
{ $inc: {"seq":1} },
{ new: true }
,function (err, data) {
if (err) {
reject(new Error('findKey ERROR : ' + err));
return;
} else {
resolve(data.seq);
};
});
})};