Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version #9930

Closed Systerr closed 3 years ago

Systerr commented 3 years ago

Do you want to request a feature or report a bug? bug (deprecation warning)

What is the current behavior? On start we have deprecation warning into console

If the current behavior is a bug, please provide the steps to reproduce.

(node:9455) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
    at _setClient (node_modules/mongoose/lib/connection.js:943:6)
    at node_modules/mongoose/lib/connection.js:839:7
    at node_modules/mongodb/lib/utils.js:693:5
    at node_modules/mongodb/lib/mongo_client.js:227:7
    at connectCallback (node_modules/mongodb/lib/operations/connect.js:366:5)
    at node_modules/mongodb/lib/operations/connect.js:602:5
    at Object.connectHandler [as callback] (/node_modules/mongodb/lib/core/sdam/topology.js:297:43)
    at processWaitQueue (node_modules/mongodb/lib/core/sdam/topology.js:1048:21)
    at NativeTopology.serverUpdateHandler (node_modules/mongodb/lib/core/sdam/topology.js:567:7)
    at Server.emit (events.js:315:20)

What is the expected behavior? No deprecation warning What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Latest 5.11.16

I digged in into warning and found why this happens: 1) This introduced into mongo driver on version v3.6.4 https://github.com/mongodb/node-mongodb-native/pull/2610 as part on preparation of driver v4 release 2) Latest mongoose 5.11.16 introduced mongo drive update https://github.com/Automattic/mongoose/pull/9893 3) Original change happens because of https://github.com/mongodb/node-mongodb-native/pull/2251

Systerr commented 3 years ago

https://github.com/Automattic/mongoose/blob/5.11.16/lib/connection.js#L943

pnutmath commented 3 years ago

https://jira.mongodb.org/browse/NODE-1709?focusedCommentId=2962047&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-2962047

Only MongoClient will receive events not DB class in future

PurpShell commented 3 years ago

how does this get fixed?

lantw44 commented 3 years ago

The warning still exists in mongoose 5.11.18:

(node:90089) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
    at _setClient (node_modules/mongoose/lib/connection.js:932:10)
    at node_modules/mongoose/lib/connection.js:838:7
    at node_modules/mongodb/lib/utils.js:693:5
    at node_modules/mongodb/lib/mongo_client.js:227:7
    at connectCallback (node_modules/mongodb/lib/operations/connect.js:366:5)
    at node_modules/mongodb/lib/operations/connect.js:602:5
    at connectHandler (node_modules/mongodb/lib/core/sdam/topology.js:297:43)
    at cb (node_modules/mongodb/lib/core/sdam/topology.js:681:26)
    at node_modules/mongodb/lib/cmap/connection_pool.js:350:13
    at handleOperationResult (node_modules/mongodb/lib/core/sdam/server.js:558:5)
    at MessageStream.messageHandler (node_modules/mongodb/lib/cmap/connection.js:277:5)
    at MessageStream.emit (events.js:315:20)
    at MessageStream.EventEmitter.emit (domain.js:529:15)
    at processIncomingData (node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
troykelly commented 3 years ago

@vkarpov15 can this please be reopened until it's confirmed resolved?

vkarpov15 commented 3 years ago

@lantw44 @troykelly do you have useUnifiedTopology: true set?

lantw44 commented 3 years ago

@lantw44 @troykelly do you have useUnifiedTopology: true set?

Yes, useUnifiedTopology: true is set when calling mongoose.createConnection.

vkarpov15 commented 3 years ago

I'm unable to repro, the below script doesn't print any warnings for me. Can you please modify the below script to demonstrate your issue?

'use strict';

const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.createConnection('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  console.log('Done');
}
FlorentinTh commented 3 years ago

@vkarpov15 just replacing your connection string from localhost to an instance of MongoDB Atlas in your script fires the warning like so :

mongodb+srv://<username>:<password>@cluster-ign8c.mongodb.net/test?retryWrites=true&w=majority

So maybe the issue is not related to Mongoose ?!

Unknow1002 commented 3 years ago

This link may help you: https://stackoverflow.com/questions/66190532/deprecationwarning-listening-to-events-on-the-db-class-has-been-deprecated-and

hillct commented 3 years ago

For what it's worth, I'm able to reproduce this issue with the following stack trace and configuration:

DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version. at _setClient (/[...]/node_modules/mongoose/lib/connection.js:932:10) at /home/[...]/node_modules/mongoose/lib/connection.js:838:7 at /home/[...]/node_modules/mongodb/lib/utils.js:693:5

It's worth reviewing https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L932 which relates to the mongoose.connection.on('connected',function(){...}); call in my code that surfaces the bug. There are a half dozen instances in mongoose/lib/connection.js of listeners created using db.on() rather than wha are presumably meant to be listeners attached to the DB client (connection). This is almost certainly the root cause of the issue.

My configuration: Node: v13.14.0 Mongo Native Driver: 3.1.0 (via connect-mongo) Mongoose: ^5.11.19 Connected to Atlas at their newest stable deployment version using useUnifiedTopology: true

Upgrading the mongodb native driver to 3.6.4 (as expected) has no impact in clearing this warning.

The following patch should resolve the warnings and ensure compliance with he MongoDB driver API through v4. https://github.com/Automattic/mongoose/pull/10004 Please test it against each of connect, close and timeout events.

tukusejssirs commented 3 years ago

This happens to me connecting to new database (const newDb = await mongoose.connection.useDb('newDb')), while I am already connected to a different database; see #8267.

Note: I am connected to a local, fresh database, therefore it is not only connected to Mongo Altas databases.

vkarpov15 commented 3 years ago

@tukusejssirs the below script doesn't print any warnings. Please modify the below script to demonstrate your issue:

'use strict';

const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.createConnection('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  const newDb = await mongoose.connection.useDb('newDb');

  console.log('Done');
}

@FlorentinTh I tried with my Atlas cluster and I don't get any warning:

  await mongoose.createConnection('mongodb+srv://OMITTED:OMITTED@cluster0.xvnqv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

I'm going to close this issue. If you're still seeing this warning:

1) Please make sure you've upgraded to the latest version of Mongoose, v5.12.3 as of this writing. 2) If that doesn't fix the issue, please open a new issue and follow the issue template.