【发布时间】:2011-11-12 03:49:57
【问题描述】:
我想使用 findAndModify 原子地增加一个字段,使用 Mongoose。
但是,下面的代码会抛出错误“TypeError: Object # has no method 'findAndModify'”:
// defining schema for the "counters" table
var tableSchema = new Schema({
_id: String,
next: Number
});
// creating table object for the counters table
var counters_table = mongoose.model('counters', tableSchema);
var tableObj = new counters_table();
// increment the "next" field on the table object
var query = {_id: 'messagetransaction'};
var update = {'$inc': {next: 1}};
var ret = tableObj.findAndModify(query, [], update, true, true, function(err) {
if (err) {
throw err;
}
else {
console.log("updated!");
}
});
【问题讨论】:
-
根据 mongodb 文档,findAndModify 应如下所示, Collection.prototype.findAndModify = function(query, sort, update,new_doc, remove_doc, function(err) { //}) 但不起作用将其转换为猫鼬类型时!请澄清一下!
-
Mongoose v3 文档:findOneAndUpdate 或 findOneAndRemove