Mongoose是否支持Mongodb`searchAndModify`方法?

Raj*_*aja 32 mongoose mongodb

我想使用findAndModify使用Mongoose以原子方式递增字段.

但是,下面的代码会引发错误"TypeError:Object#have method'seadAndModify'":

// 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!");
     }
});
Run Code Online (Sandbox Code Playgroud)

小智 61

该功能不太好(完全阅读:记录),但在阅读完源代码后,我想出了以下解决方案.

创建集合架构.

var Counters = new Schema({
  _id: String,
  next: Number     
});
Run Code Online (Sandbox Code Playgroud)

在架构上创建一个静态方法,该方法将公开模型集合的findAndModify方法.

Counters.statics.findAndModify = function (query, sort, doc, options, callback) {
  return this.collection.findAndModify(query, sort, doc, options, callback);
};
Run Code Online (Sandbox Code Playgroud)

创建您的模型.

var Counter = mongoose.model('counters', Counters);
Run Code Online (Sandbox Code Playgroud)

查找并修改!

Counter.findAndModify({ _id: 'messagetransaction' }, [], { $inc: { next: 1 } }, {}, function (err, counter) {
  if (err) throw err;
  console.log('updated, counter is ' + counter.next);
});
Run Code Online (Sandbox Code Playgroud)

奖金

Counters.statics.increment = function (counter, callback) {
  return this.collection.findAndModify({ _id: counter }, [], { $inc: { next: 1 } }, callback);
};

Counter.increment('messagetransaction', callback);
Run Code Online (Sandbox Code Playgroud)

  • 如何通过这种方法获取findAndModify的返回值? (3认同)

jua*_*aco 18

现在Mongoose 3.x完全支持这一点,但名称略有不同.

http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate

http://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

http://mongoosejs.com/docs/api.html#model_Model.findOneAndRemove

http://mongoosejs.com/docs/api.html#model_Model.findByIdAndRemove

  • MongoDB具有相同的限制(http://docs.mongodb.org/manual/reference/command/findAndModify/). (3认同)
  • 这些仅允许查找单个条目 (2认同)

Kli*_*kin 14

为Mongoose 3.x制作工作版增量

var mongoose = require('mongoose');

var CounterSchema = new mongoose.Schema({
    _id: String,
    next: {type: Number, default: 1}
});

CounterSchema.statics.increment = function (counter, callback) {
    return this.findByIdAndUpdate(counter, { $inc: { next: 1 } }, {new: true, upsert: true, select: {next: 1}}, callback);
};
Run Code Online (Sandbox Code Playgroud)

使用这样的东西:

Counter.increment('photo', function (err, result) {
    if (err) {
        console.error('Counter on photo save error: ' + err); return;
    }
    photo.cid = result.next;
    photo.save();
});
Run Code Online (Sandbox Code Playgroud)

我希望有人派上用场


nin*_*123 6

在版本3中,mongoose findOneAndUpdate方法公开了mongodb的findAndModify操作.它的工作原理如下:

var query = { name: 'Sprinkls' };
var update = { name: 'Sprinkles' };
var options = { new: false };
Cat.findOneAndUpdate(query, update, options, function (err, cat) {
  if (err) ..
  render('cat', cat);
});
Run Code Online (Sandbox Code Playgroud)

更多信息:http://aaronheckmann.tumblr.com/post/48943524629/mongoose-v3-part-2-findandmodify


mst*_*arn 0

我建议使用http://www.mongodb.org/display/DOCS/findAndModify+Command底部显示的直接命令样式。我对 mongoose 不太熟悉,不知道运行命令的方法,但所有驱动程序都提供了一些方法来执行此操作。如果 mongoose 没有,您可以直接使用http://www.mongodb.org/display/DOCS/Commands顶部描述的样式来完成。

也就是说,您应该确保您确实需要它findAndModify,并且它update不会做您需要它做的事情。要了解update有哪些功能,请查看http://www.mongodb.org/display/DOCS/Updating