我想用findAndModify自动增加一个字段,使用Mongoose。Mongoose是否支持Mongodb findAndModify方法?
但是,下面的代码引发错误“类型错误:对象#有没有方法‘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){ //}) 但将其转换为猫鼬类型时不起作用!请在此澄清我! – Raja
在v3中添加了新功能:http://aaronheckmann.posterous.com/mongoose-v3-part-2-findandmodify – EvdB
Mongoose v3文档:[findOneAndUpdate](http://mongoosejs.com/docs/api.html#query_Query -findOneAndUpdate)或[findOneAndRemove](http://mongoosejs.com/docs/api.html#model_Model.findOneAndRemove) – aaronheckmann