下面是我的代码

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var Cat = mongoose.model('Cat', {
    name: String,
    age: {type: Number, default: 20},
    create: {type: Date, default: Date.now} 
});

Cat.findOneAndUpdate({age: 17}, {$set:{name:"Naomi"}},function(err, doc){
    if(err){
        console.log("Something wrong when updating data!");
    }

    console.log(doc);
});

我已经在我的mongo数据库中有一些记录,我想运行这段代码来更新年龄为17岁的名称,然后在代码的末尾打印结果。

然而,为什么我仍然从控制台得到相同的结果(不是修改后的名称),但当我进入mongo db命令行并键入“db.cats.find();”。结果是修改后的名称。

然后我返回去再次运行这段代码,结果被修改了。

我的问题是:如果数据被修改了,那么为什么我仍然在console.log它的第一时间得到原始数据。


当前回答

如果你想返回修改后的文档,你需要设置选项{new:true} API引用,你可以使用Cat。findOneAndUpdate(conditions, update, options, callback) //执行

通过官方的Mongoose API http://mongoosejs.com/docs/api.html#findoneandupdate_findOneAndUpdate,您可以使用以下参数

A.findOneAndUpdate(conditions, update, options, callback) // executes
A.findOneAndUpdate(conditions, update, options)  // returns Query
A.findOneAndUpdate(conditions, update, callback) // executes
A.findOneAndUpdate(conditions, update)           // returns Query
A.findOneAndUpdate()                             // returns Query

另一个没有在官方API页面中表达的实现是我更喜欢使用的Promise基实现,它允许你有.catch,在那里你可以处理所有的各种错误。

    let cat: catInterface = {
        name: "Naomi"
    };

    Cat.findOneAndUpdate({age:17}, cat,{new: true}).then((data) =>{
        if(data === null){
            throw new Error('Cat Not Found');
        }
        res.json({ message: 'Cat updated!' })
        console.log("New cat data", data);
    }).catch( (error) => {
        /*
            Deal with all your errors here with your preferred error handle middleware / method
         */
        res.status(500).json({ message: 'Some Error!' })
        console.log(error);
    });

其他回答

对于那些使用ES6 / ES7风格和本地承诺的人来说,这里有一个模式你可以采用…

const user = { id: 1, name: "Fart Face 3rd"};
const userUpdate = { name: "Pizza Face" };

try {
    user = await new Promise( ( resolve, reject ) => {
        User.update( { _id: user.id }, userUpdate, { upsert: true, new: true }, ( error, obj ) => {
            if( error ) {
                console.error( JSON.stringify( error ) );
                return reject( error );
            }

            resolve( obj );
        });
    })
} catch( error ) { /* set the world on fire */ }

下面显示了查询mongoose的findOneAndUpdate。这里new: true用于获取更新的文档,fields用于获取特定的字段。

如。findOneAndUpdate(条件,更新,选项,回调)

await User.findOneAndUpdate({
      "_id": data.id,
    }, { $set: { name: "Amar", designation: "Software Developer" } }, {
      new: true,
      fields: {
        'name': 1,
        'designation': 1
      }
    }).exec();

我知道,我已经迟到了,但让我在这里补充一个简单而有效的答案

const query = {} //your query here
const update = {} //your update in json here
const option = {new: true} //will return updated document

const user = await User.findOneAndUpdate(query , update, option)

为什么会发生这种情况?

默认是返回原始的、未修改的文档。如果你想要返回新的更新的文档,你必须传递一个额外的参数:一个新属性设置为true的对象。

猫鼬医生说:

查询# findOneAndUpdate 模型。findOneAndUpdate(条件,更新,选项,(错误,doc) => { // error:发生的任何错误 // doc:如果' new: false '则应用更新前的文档,如果' new = true '则应用更新后的文档 }); 可用选项 New: bool -如果为true,则返回修改后的文档而不是原始文档。默认为false(在4.0中更改)

解决方案

如果你想在doc变量中得到更新后的结果,传递{new: true}:

//                                                         V--- THIS WAS ADDED
Cat.findOneAndUpdate({age: 17}, {$set:{name:"Naomi"}}, {new: true}, (err, doc) => {
    if (err) {
        console.log("Something wrong when updating data!");
    }

    console.log(doc);
});

我是猫鼬的维护者。您需要将新选项设置为true(或等效地将returnorigoriginal设置为false)

await User.findOneAndUpdate(filter, update, { new: true });

// Equivalent
await User.findOneAndUpdate(filter, update, { returnOriginal: false });

请参阅Mongoose findOneAndUpdate()文档和在Mongoose中更新文档的教程。