代码之家  ›  专栏  ›  技术社区  ›  Sudhanshu Gaur

不推荐使用警告:collection.findAndModify已不推荐使用。改用findOneAndUpdate、findOneAndReplace或findOneAndDelete?

  •  16
  • Sudhanshu Gaur  · 技术社区  · 6 年前

    我在用猫鼬 findOneAndUpdate

    不推荐使用警告:collection.findAndModify已不推荐使用。改用findOneAndUpdate、findOneAndReplace或findOneAndDelete。

    findAndModify ,为什么要将我的查询转换为 查找和修改

    3 回复  |  直到 6 年前
        1
  •  108
  •   Shivam Pandey    6 年前

    您需要在查询中设置选项 useFindAndModify false ,如 docs .

    当前支持的选项包括 )

    findOneAndUpdate()和findOneAndRemove()使用本机 findOneAndUpdate()而不是findAndModify()。

    如果您看到mongoose的定义文件,其中提到它调用findAndModify update命令。

     /**
      * Issues a mongodb findAndModify update command.
      * Finds a matching document, updates it according to the update arg, 
        passing any options,
      * and returns the found document (if any) to the callback. The query 
        executes immediately
      * if callback is passed else a Query object is returned.
      */
     findOneAndUpdate(): DocumentQuery<T | null, T>;
    

    Click here )对于提到的这些折旧:

    findOneAndUpdate()函数,因此它使用MongoDB驱动程序的

    有三种以上的方法可以避免使用 FindAndModify

    1. 在全局级别:将选项设置为false。
    // Make Mongoose use `findOneAndUpdate()`. Note that this option is `true`
    // by default, you need to set it to false.
    mongoose.set('useFindAndModify', false);
    
        mongoose.connect(uri, { useFindAndModify: false });
    
    1. 在查询级别:
       await ModelName.findOneAndUpdate({matchQuery},
       {$set: updateData}, {useFindAndModify: false});
    
    
        2
  •  23
  •   Ikram Ud Daula    6 年前

    mongoose.set('useFindAndModify', false);
    

    Person.findOneAndUpdate({_id: id}, {$set: body}, {new: true, useFindAndModify: false}).then(..
    

    您还可以管理前面提到的其他mongoose弃用警告 docs

    mongoose.set('useNewUrlParser', true);
    mongoose.set('useCreateIndex', true);
    

    就这样。

        3
  •  7
  •   Polliny    6 年前

    您还可以在与requirement选项的连接处传递选项 useNewUrlParser https://mongoosejs.com/docs/deprecations.html

    mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useFindAndModify: false}); 
    
        4
  •  3
  •   C.OG    5 年前

    您必须更改连接方法选项以消除错误:

    mongoose.connect("mongodb://localhost/DB_Name", {
      keepAlive: true,
      useNewUrlParser: true,
      useCreateIndex: true,
      useFindAndModify: false
    });
    

    你可以这样用。

        5
  •  2
  •   Raghu P Rangan    4 年前

    猫鼬版本更新太多,

    供使用 Model.findByIdAndUpdate() 它需要一个option参数,另请参见下文

    List.findByIdAndUpdate(id, update, options, callback) // executes
    

    为了解决这个问题

    useFindAndModify: false 在mongoose.connect中启动

    mongoose.connect("mongodb://localhost:27017/yourDatabase", { useNewUrlParser: true, useUnifiedTopology: true ,useFindAndModify: false });
    

    mongoose.set('useFindAndModify', false); 
    

    clickhere 检查相关的否决

        6
  •  0
  •   Sujeewa K. Abeysinghe    4 年前
    Mongoose.connect(Config.database,{useUnifiedTopology: true,useNewUrlParser: true,useFindAndModify:false});
    

    要跳过所有错误:-) 把这个添加到index.js或者你命名的任何东西上。我是说主js文件。;-)

        7
  •  0
  •   Sounak Roy    4 年前
      mongoose.set('useNewUrlParser', true);
      mongoose.set('useFindAndModify', false);
      mongoose.set('useCreateIndex', true);
      mongoose.set('useUnifiedTopology', true);