Node.js 在mongoose中使用findByIdAndUpdate方法时,仅获取弃用警告,而不获取结果或任何错误

Node.js 在mongoose中使用findByIdAndUpdate方法时,仅获取弃用警告,而不获取结果或任何错误,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个用户模型,我正试图使用MongooseODM提供的findByIdAndUpdate方法来更新年龄。我在控制台上看到的只是一个弃用警告,而不是结果或错误。时代也没有更新 mongoose.js文件的内容 require('mongoose') const User = require('../src/models/users'); User.findByIdAndUpdate('5e2fd89f475fd04adb5f938c',{age:35}).then((user)=>{

我有一个用户模型,我正试图使用MongooseODM提供的findByIdAndUpdate方法来更新年龄。我在控制台上看到的只是一个弃用警告,而不是结果或错误。时代也没有更新

mongoose.js文件的内容

require('mongoose')
const User = require('../src/models/users');

User.findByIdAndUpdate('5e2fd89f475fd04adb5f938c',{age:35}).then((user)=>{
    console.log(user);
    return User.countDocuments({age:35})
}).then((result)=>{
    console.log(result);
}).catch((error)=>{
    console.log(error);
});

这是因为您正在将
id
作为
object
传递。而
findbyiandupdate
希望它
string
。您可以尝试以下方法:

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/task-manager-api',{
    useNewUrlParser:true,
    useCreateIndex:true,
    useUnifiedTopology:true,
    useFindAndModify:false
})

希望这对您有用。

这是一个错误,我后来更正并将Id作为字符串传递,但仍然是相同的问题。
User.findByIdAndUpdate('5e2fd89f475fd04adb5f938c',{age:35}).then((user)=>{
    console.log(user);
    return User.countDocuments({age:35})
}).then((result)=>{
    console.log(result);
}).catch((error)=>{
    console.log(error);
});