Skip to content
Advertisement

Why am I getting this deprecated warning?! MongoDB

I’m working with MongoDB in NodeJS,

JavaScript

and when the last c.getUser statement is executed (that’s to say, when I make a SECOND connectio) Mongodb outputs this warning:

JavaScript

But I’m not using any deprecated options. Any ideas?


EDIT

After a little discussion with molank in the comments, it looks like open several connections from the same server is not a good practice, so maybe that’s what the warning is trying to say (badly I think). So if you have the same problem, save the connection instead of the mongo client.

Advertisement

Answer

Reposting from https://jira.mongodb.org/browse/NODE-1868:

The deprecation messages are likely because client.connect is being called multiple times. Overall, calling client.connect multiple times currently (as of driver v3.1.13) has undefined behavior, and it is not recommended. It is important to note that once the promise returned from connect resolves, the client remains connected until you call client.close:

JavaScript

The client by default maintains multiple connections to each server it is connected to, and can be used for multiple simultaneous operations*. You should be fine running client.connect once, and then running your operations on the client object

* Note that the client is NOT thread-safe or fork-safe, so it cannot be shared across forks, and it not compatible with node’s cluster or worker_threads modules.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement