【问题标题】:MongoDB mongoose Deprecation WarningMongoDB mongoose 弃用警告
【发布时间】:2019-01-25 18:00:21
【问题描述】:

在使用 collection.find 查询文档时,我开始在控制台中收到以下警告

DeprecationWarning:collection.find 选项 [fields] 已弃用,并且 将在以后的版本中删除

为什么我会看到这个,我该如何解决这个问题? (可能的替代方案)

编辑:添加查询

Session
        .find({ sessionCode: '18JANMON', completed: false })
        .limit(10)
        .sort({time: 1})
        .select({time: 1, sessionCode: 1});

猫鼬版本 5.2.9

【问题讨论】:

  • 你尝试使用collection.find(query).limit(1).project({name:1})吗?
  • 嗨 @DanieleTassone 每当我使用 find() 时都会出现此警告
  • 您能发布完整的查询
  • 原生 mongodb 驱动程序是 Mongoose 内部用来处理 MongoDB 的东西。如果 Mongoose 不遵守某些新的“规则”,则会返回警告。例如,使用本机驱动程序时,如果您使用“字段选项”而不是光标功能,您将收到此警告。看这里:github.com/Automattic/mongoose/issues/6667
  • There is an issue on github isopens...所以在解决之前你可以使用猫鼬版本5.2.8

标签: javascript node.js mongodb mongoose nosql


【解决方案1】:

更新:

5.2.10 发布并可供下载here

有关文档的更多信息,您可以查看页面 https://mongoosejs.com/docs/deprecations

有关此问题及其修复的更多信息 https://github.com/Automattic/mongoose/issues/6880

原答案:

Mongoose 5.2.9 版本将原生 mongodb 驱动升级到 3.1.3,其中添加了更改以在调用已弃用的原生驱动方法时抛出警告消息。

fields 选项已弃用,取而代之的是projection 选项。

您将不得不等待 mongoose 在其末尾进行更改以将字段选项替换为投影。该修复计划在 5.2.10 版本中发布。

暂时您可以返回到 5.2.8,这将禁止所有弃用警告。

npm install mongoose@5.2.8

对于所有其他已弃用的警告,您必须逐个处理它们。

当您使用其他收集方法时,您会看到其他弃用警告。

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
DeprecationWarning: collection.save is deprecated. Use insertOne, insertMany, updateOne, or updateMany instead.
DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

所有findOne* mongoose 写入方法默认使用findAndModify 方法,该方法在mongodb 本机驱动程序中已弃用。

使用 mongoose.set('useFindAndModify', false); 让 mongooose 在 mongodb 原生驱动程序上调用适当的 findOne* 方法。

对于removeupdate,分别将这些调用替换为delete*update* 方法。

对于save,将这些调用分别替换为insert*/update* 方法。

使用mongoose.set('useCreateIndex', true); 让mongooose 在mongodb 原生驱动上调用createIndex 方法。

【讨论】:

  • 我很困惑:当这个问题与已弃用的 find 选项有关时,您为什么要使用有关如何修复 ensureIndex 弃用的信息来更新答案?
  • @skerit 谢谢 - 更新了答案。我试图涵盖所有已弃用的警告。
【解决方案2】:

您可以发送npm install mongoose@5.2.8,这将帮助您恢复到不会显示任何弃用警告的早期版本

【讨论】:

  • 只是降级版本以避免警告? ??
【解决方案3】:

升级到版本 5.2.10 后。可以使用以下任何选项

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', {

  useCreateIndex: true,
  useNewUrlParser: true

})
.then(() => console.log('connecting to database successful'))
.catch(err => console.error('could not connect to mongo DB', err));



const mongoose = require('mongoose');

mongoose.set('useCreateIndex', true);

mongoose.connect('mongodb://localhost/test',{

    useNewUrlParser: true

})
.then(() =>  console.log('connecting to database successful') )
.catch(err =>  console.error('could not connect to mongo DB', err) );

【讨论】:

    【解决方案4】:
    mongoose.connect('your db url', {
      useCreateIndex: true,
      useNewUrlParser: true
    })
    

    mongoose.set('useCreateIndex', true)
    mongoose.connect('your db url', { useNewUrlParser: true })
    

    【讨论】:

    • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和/或文档链接。如果没有围绕它们的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案。
    • 不,这是一个很好的答案。警告消失了。
    【解决方案5】:

    这在 2020 年 4 月对我有用:

    mongoose.connect(process.env.DATABASE_URL, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true
    })
    

    【讨论】:

      【解决方案6】:

      mongoose.connect('mongodb://localhost:27017/tablename',{ useUnifiedTopology: true, useNewUrlParser: true,useCreateIndex: true },()=>{ console.log(connected db) });

      【讨论】:

        【解决方案7】:

        在数据库连接时只需传递以下选项

        例如

        const mongoose = require("mongoose");
        
        mongoose.connect("uri",{ 
                               "useNewUrlParser": true,
                               "useUnifiedTopology": true, 
                               "useCreateIndex": true 
                               // other deprecations 
                               },(err)=>{
             // connection logging
        

        });

        【讨论】:

          【解决方案8】:

          我目前正在使用“mongoose@5.11.15” 你可以通过使用这个来摆脱 DeprecationWarning,

          方法一

          mongoose.connect("Your DB address", {
          useNewUrlParser: true,                       
          useUnifiedTopology: true,                 
          useCreateIndex: true               // to handle collection.ensureIndex is deprecated
          });
          

          方法二

          mongoose.connect("Your DB address", {
          useNewUrlParser: true,                       
          useUnifiedTopology: true,                 // other deprecation warnings            
          });
          mongoose.set("useCreateIndex", true);     // to handle collection.ensureIndex is deprecated
          

          【讨论】:

            【解决方案9】:

            你也可以使用 --不弃用 在 CLI 中忽略弃用警告。

            我收到了这个警告 - (node:108) [MONGODB DRIVER] 警告:collection.update 已被弃用。请改用 updateOne、updateMany 或 bulkWrite。 通过使用 --no-deprecation 效果很好

            在此处查看文档 - https://nodejs.org/dist/latest-v10.x/docs/api/cli.html#cli_no_deprecation

            【讨论】:

              【解决方案10】:
              mongoose.connect(process.env.DATABASE, {
               useNewUrlParser: true,
               useCreateIndex: true,
               useFindAndModify: false,
               useUnifiedTopology: true,
              })
              .then(() => {
               console.log("DB connected");
              })
              .catch((err) => console.log(`DB connection Err`, err));
              

              【讨论】:

                【解决方案11】:

                这里 connectionString 是您的数据库地址

                mongoose
                  .connect(connectionString, {
                    useNewUrlParser: true,
                    useUnifiedTopology: true,
                    useCreateIndex: true,
                  })
                  .then(
                    app.listen(port, () => {
                      console.log(`server started on port ${port}`);
                    })
                  )
                  .catch((err) => console.log(err));
                

                【讨论】:

                  【解决方案12】:

                  当我们发出连接请求以连接 mongo db 时,我们只需在该回调函数中再添加一个键值对

                  useCreateIndex:true
                  

                  函数内部

                  mongoose.connect(config.DB_URI, {
                    useNewUrlParser: true,
                    useUnifiedTopology: true,
                    useCreateIndex:true
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2019-02-21
                    • 2021-07-12
                    • 2022-12-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-09-15
                    • 1970-01-01
                    相关资源
                    最近更新 更多