I am using mongodb on typescript and my problem is that the connections don't close after querying.
I had the following approach in doing it:
import { Db, MongoClient } from 'mongodb';
let cachedConnection: { client: MongoClient; db: Db } | null = null;
export async function connectToDatabase(mongoUri?: string, database?: string) {
if (!mongoUri) {
throw new Error(
'Please define the MONGO_URI environment variable inside .env.local'
);
}
if (!database) {
throw new Error(
'Please define the DATABASE environment variable inside .env.local'
);
}
if (cachedConnection) return cachedConnection;
cachedConnection = await MongoClient.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then((client) => ({
client,
db: client.db(database),
}));
return cachedConnection!;
}
and I use it in my nodejs server by doing:
const { db } = await connectToDatabase(config.URI, DB_NAME);
const result = await db.collection(COLLECTION_NAME).aggregate([MyPipelines])
I have the database on Atlas and if I go to the dashboard I see a lot of connections not closed, and when it reaches 500 then the server need to be stopped because it goes in timeout and closes it. They send me one email saying "You're nearing the maximum connections treshold"
In fact now I have all these connections active.
And on console I get these errors:
What do you think I did wrong? Is there a better way to handle mongoDB connection with typescript?
You should close all connections when your app closes completely.
process.on('SIGINT', async () => {
await cachedConnection.db.close();
process.exit(0);
});
My problem is quite peculiar, as I believe to have done everything "by the book".
I'm able to successfully connect to a MongoDB cluster I've created through MongoDB Atlas. When I make a 'POST' request to save a choice that was picked from an array of choices, I successfully create a document through the Model specified below. I then try to save that document to MongoDB by calling the 'save()' method, but it hangs and nothing comes out of it (even if I use a 'catch' to see if any errors occurred).
I'm completely lost as to what is wrong, and what I need to do to solve this. I was hoping you could give me some pointers.
MongoDB connection, schema, and model:
const mongoose = require('mongoose');
const URL = process.env.MONGODB_URL;
mongoose.connect(URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Successfully connected to our MongoDB database.');
}).catch((error) => {
console.log('Could not connect to our MongoDB database.', error.message);
});
const choicesMadeSchema = new mongoose.Schema({
allChoices: Array,
pickedChoice: String
});
const ChoiceMade = mongoose.model('ChoiceMade', choicesMadeSchema);
module.exports = ChoiceMade; // Exports our 'ChoiceMade' constructor, to be used by other modules.
index.js:
/* 1 - Setting things up */
require('dotenv').config();
const express = require('express');
const server = express();
const PORT = process.env.PORT;
const parserOfRequestBody = require('body-parser');
server.use(parserOfRequestBody.json());
/* 2 - Retrieving all the data we need from our 'MongoDB' database */
// Imports the 'mongoose' library, which will allow us to easily interact with our 'MongoDB' database.
const mongoose = require('mongoose');
// Imports our 'ChoiceMade' constructor.
const ChoiceMade = require('./database/database.js');
// Will hold the five latest choices that have been made (and thus saved on our 'MongoDB' database).
let fiveLatestChoicesMade;
// Retrieves the five latest choices that have been made (and thus saved on our 'MongoDB' database).
ChoiceMade.find({}).then((allChoicesEverMade) => {
const allChoicesEverMadeArray = allChoicesEverMade.map((choiceMade) => {
return choiceMade.toJSON();
});
fiveLatestChoicesMade = allChoicesEverMadeArray.slice(allChoicesEverMadeArray.length - 5).reverse();
console.log("These are the five latest choices that have been made:", fiveLatestChoicesMade);
mongoose.connection.close();
});
/* 3 - How the server should handle requests */
// 'GET' (i.e., 'retrieve') requests
server.get('/allChoicesMade', (request, response) => {
console.log("This is the data that will be sent as a response to the 'GET' request:", fiveLatestChoicesMade);
response.json(fiveLatestChoicesMade);
});
// 'POST' (i.e., 'send') requests
server.post('/allChoicesMade', (request, response) => {
const newChoiceMadeData = request.body;
if (Object.keys(newChoiceMadeData).length === 0) {
return response.status(400).json({ error: "No data was provided." });
}
const newChoiceMade = new ChoiceMade({
allChoices: newChoiceMadeData.allChoices,
pickedChoice: newChoiceMadeData.pickedChoice
});
console.log("This is the new 'choice made' entry that we are going to save on our 'MongoDB' database:", newChoiceMade); // All good until here
newChoiceMade.save().then((savedChoiceMade) => {
console.log('The choice that was made has been saved!');
response.json(savedChoiceMade);
mongoose.connection.close();
}).catch((error) => {
console.log('An error occurred:', error);
});
});
/* 4 - Telling the server to 'listen' for requests */
server.listen(PORT, () => {
console.log("Our 'Express' server is running, and listening for requests made to port '" + PORT + "'.");
});
SOLUTION TO THE PROBLEM
In my code's section 2, I was mistakenly closing the connection upon retrieving all the data I need to make my app work. I was doing this (...)
// Retrieves the five latest choices that have been made (and thus saved on our 'MongoDB' database).
ChoiceMade.find({}).then((allChoicesEverMade) => {
const allChoicesEverMadeArray = allChoicesEverMade.map((choiceMade) => {
return choiceMade.toJSON();
});
fiveLatestChoicesMade = allChoicesEverMadeArray.slice(allChoicesEverMadeArray.length - 5).reverse();
console.log("These are the five latest choices that have been made:", fiveLatestChoicesMade);
mongoose.connection.close(); // This should not be here!!!
});
(...) when I should be doing
// Retrieves the five latest choices that have been made (and thus saved on our 'MongoDB' database).
ChoiceMade.find({}).then((allChoicesEverMade) => {
const allChoicesEverMadeArray = allChoicesEverMade.map((choiceMade) => {
return choiceMade.toJSON();
});
fiveLatestChoicesMade = allChoicesEverMadeArray.slice(allChoicesEverMadeArray.length - 5).reverse();
console.log("These are the five latest choices that have been made:", fiveLatestChoicesMade);
// Now that I don't have mongoose.connection.close(), everything's OK!
});
Basically, and in my particular case, I was closing my connection to the MongoDB database after retrieving data from it, and then trying to add a new record to it when I didn't have a connection to it anymore.
I have a database wrapper class that establishes a connection to some MongoDB instance:
async connect(connectionString: string): Promise<void> {
this.client = await MongoClient.connect(connectionString)
this.db = this.client.db()
}
This gave me a warning:
(node:4833) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
The connect() method accepts a MongoClientOptions instance as second argument. But it doesn't have a property called useNewUrlParser. I also tried to set those property in the connection string like this: mongodb://127.0.0.1/my-db?useNewUrlParser=true but it has no effect on those warning.
So how can I set useNewUrlParser to remove those warning? This is important to me since the script should run as cron and those warnings result in trash-mail spam.
I'm using mongodb driver in version 3.1.0-beta4 with corresponding #types/mongodb package in 3.0.18. Both of them are the latest avaliable using npm install.
Workaround
Using an older version of mongodb driver:
"mongodb": "~3.0.8",
"#types/mongodb": "~3.0.18"
Check your mongo version:
mongo --version
If you are using version >= 3.1.0, change your mongo connection file to ->
MongoClient.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true })
or your mongoose connection file to ->
mongoose.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true });
Ideally, it's a version 4 feature, but v3.1.0 and above are supporting it too. Check out MongoDB GitHub for details.
As noted the 3.1.0-beta4 release of the driver got "released into the wild" a little early by the looks of things. The release is part of work in progress to support newer features in the MongoDB 4.0 upcoming release and make some other API changes.
One such change triggering the current warning is the useNewUrlParser option, due to some changes around how passing the connection URI actually works. More on that later.
Until things "settle down", it would probably be advisable to "pin" at least to the minor version for 3.0.x releases:
"dependencies": {
"mongodb": "~3.0.8"
}
That should stop the 3.1.x branch being installed on "fresh" installations to node modules. If you already did install a "latest" release which is the "beta" version, then you should clean up your packages ( and package-lock.json ) and make sure you bump that down to a 3.0.x series release.
As for actually using the "new" connection URI options, the main restriction is to actually include the port on the connection string:
const { MongoClient } = require("mongodb");
const uri = 'mongodb://localhost:27017'; // mongodb://localhost - will fail
(async function() {
try {
const client = await MongoClient.connect(uri,{ useNewUrlParser: true });
// ... anything
client.close();
} catch(e) {
console.error(e)
}
})()
That's a more "strict" rule in the new code. The main point being that the current code is essentially part of the "node-native-driver" ( npm mongodb ) repository code, and the "new code" actually imports from the mongodb-core library which "underpins" the "public" node driver.
The point of the "option" being added is to "ease" the transition by adding the option to new code so the newer parser ( actually based around url ) is being used in code adding the option and clearing the deprecation warning, and therefore verifying that your connection strings passed in actually comply with what the new parser is expecting.
In future releases the 'legacy' parser would be removed and then the new parser will simply be what is used even without the option. But by that time, it is expected that all existing code had ample opportunity to test their existing connection strings against what the new parser is expecting.
So if you want to start using new driver features as they are released, then use the available beta and subsequent releases and ideally make sure you are providing a connection string which is valid for the new parser by enabling the useNewUrlParser option in MongoClient.connect().
If you don't actually need access to features related to preview of the MongoDB 4.0 release, then pin the version to a 3.0.x series as noted earlier. This will work as documented and "pinning" this ensures that 3.1.x releases are not "updated" over the expected dependency until you actually want to install a stable version.
The below highlighted code to the mongoose connection solved the warning for the mongoose driver:
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
There is nothing to change. Pass only in the connect function {useNewUrlParser: true }.
This will work:
MongoClient.connect(url, {useNewUrlParser:true,useUnifiedTopology: true }, function(err, db) {
if(err) {
console.log(err);
}
else {
console.log('connected to ' + url);
db.close();
}
})
You just need to set the following things before connecting to the database as below:
const mongoose = require('mongoose');
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect('mongodb://localhost/testaroo');
Also,
Replace update() with updateOne(), updateMany(), or replaceOne()
Replace remove() with deleteOne() or deleteMany().
Replace count() with countDocuments(), unless you want to count how many documents are in the whole collection (no filter).
In the latter case, use estimatedDocumentCount().
You need to add { useNewUrlParser: true } in the mongoose.connect() method.
mongoose.connect('mongodb://localhost:27017/Notification',{ useNewUrlParser: true });
The connection string format must be mongodb://user:password#host:port/db
For example:
MongoClient.connect('mongodb://user:password#127.0.0.1:27017/yourDB', { useNewUrlParser: true } )
The following works for me
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/playground", { useNewUrlParser: true,useUnifiedTopology: true })
.then(res => console.log('Connected to db'));
The mongoose version is 5.8.10.
The problem can be solved by giving the port number and using this parser: {useNewUrlParser: true}
The solution can be:
mongoose.connect("mongodb://localhost:27017/cat_app", { useNewUrlParser: true });
It solves my problem.
I don't think you need to add { useNewUrlParser: true }.
It's up to you if you want to use the new URL parser already. Eventually the warning will go away when MongoDB switches to their new URL parser.
As specified in Connection String URI Format, you don't need to set the port number.
Just adding { useNewUrlParser: true } is enough.
Updated for ECMAScript 8 / await
The incorrect ECMAScript 8 demo code MongoDB inc provides also creates this warning.
MongoDB provides the following advice, which is incorrect
To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Doing this will cause the following error:
TypeError: final argument to executeOperation must be a callback
Instead the option must be provided to new MongoClient:
See the code below:
const DATABASE_NAME = 'mydatabase',
URL = `mongodb://localhost:27017/${DATABASE_NAME}`
module.exports = async function() {
const client = new MongoClient(URL, {useNewUrlParser: true})
var db = null
try {
// Note this breaks.
// await client.connect({useNewUrlParser: true})
await client.connect()
db = client.db(DATABASE_NAME)
} catch (err) {
console.log(err.stack)
}
return db
}
The complete example for Express.js, API calling case and sending JSON content is the following:
...
app.get('/api/myApi', (req, res) => {
MongoClient.connect('mongodb://user:password#domain.com:port/dbname',
{ useNewUrlParser: true }, (err, db) => {
if (err) throw err
const dbo = db.db('dbname')
dbo.collection('myCollection')
.find({}, { _id: 0 })
.sort({ _id: -1 })
.toArray(
(errFind, result) => {
if (errFind) throw errFind
const resultJson = JSON.stringify(result)
console.log('find:', resultJson)
res.send(resultJson)
db.close()
},
)
})
}
The following work for me
for the mongoose version 5.9.16
const mongoose = require('mongoose');
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect('mongodb://localhost:27017/dbName')
.then(() => console.log('Connect to MongoDB..'))
.catch(err => console.error('Could not connect to MongoDB..', err))
Here's how I have it. The hint didn't show on my console until I updated npm a couple of days prior.
.connect has three parameters, the URI, options, and err.
mongoose.connect(
keys.getDbConnectionString(),
{ useNewUrlParser: true },
err => {
if (err)
throw err;
console.log(`Successfully connected to database.`);
}
);
We were using:
mongoose.connect("mongodb://localhost/mean-course").then(
(res) => {
console.log("Connected to Database Successfully.")
}
).catch(() => {
console.log("Connection to database failed.");
});
→ This gives a URL parser error
The correct syntax is:
mongoose.connect("mongodb://localhost:27017/mean-course" , { useNewUrlParser: true }).then(
(res) => {
console.log("Connected to Database Successfully.")
}
).catch(() => {
console.log("Connection to database failed.");
});
const mongoose = require('mongoose');
mongoose
.connect(connection_string, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then((con) => {
console.log("connected to db");
});
try to use this
These lines did the trick for all other deprecation warnings too:
const db = await mongoose.createConnection(url, { useNewUrlParser: true });
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
I am using mongoose version 5.x for my project. After requiring the mongoose package, set the value globally as below.
const mongoose = require('mongoose');
// Set the global useNewUrlParser option to turn on useNewUrlParser for every connection by default.
mongoose.set('useNewUrlParser', true);
This works for me nicely:
mongoose.set("useNewUrlParser", true);
mongoose.set("useUnifiedTopology", true);
mongoose
.connect(db) //Connection string defined in another file
.then(() => console.log("Mongo Connected..."))
.catch(() => console.log(err));
I was using mlab.com as the MongoDB database. I separated the connection string to a different folder named config and inside file keys.js I kept the connection string which was:
module.exports = {
mongoURI: "mongodb://username:password#ds147267.mlab.com:47267/projectname"
};
And the server code was
const express = require("express");
const mongoose = require("mongoose");
const app = express();
// Database configuration
const db = require("./config/keys").mongoURI;
// Connect to MongoDB
mongoose
.connect(
db,
{ useNewUrlParser: true } // Need this for API support
)
.then(() => console.log("MongoDB connected"))
.catch(err => console.log(err));
app.get("/", (req, res) => res.send("hello!!"));
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port ${port}`)); // Tilde, not inverted comma
You need to write { useNewUrlParser: true } after the connection string as I did above.
Simply put, you need to do:
mongoose.connect(connectionString,{ useNewUrlParser: true }
// Or
MongoClient.connect(connectionString,{ useNewUrlParser: true }
If username or password has the # character, then use it like this:
mongoose
.connect(
'DB_url',
{ user: '#dmin', pass: 'p#ssword', useNewUrlParser: true }
)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.log('Could not connect to MongoDB', err));
(node:16596) DeprecationWarning: current URL string parser is
deprecated, and will be removed in a future version. To use the new
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use node --trace-deprecation ... to show where the warning was
created) (node:16596) [MONGODB DRIVER] Warning: Current Server
Discovery and Monitoring engine is deprecated, and will be removed in
a future version. To use the new Server Discover and Monitoring
engine, pass option { useUnifiedTopology: true } to the MongoClient
constructor.
Usage:
async connect(connectionString: string): Promise<void> {
this.client = await MongoClient.connect(connectionString, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
this.db = this.client.db()
}
Hi is there a way to switch database with mongoose?
I thought I could do it like that:
mongoose.disconnect();
mongoose.connect('localhost',db);
but it does not work I receive this error:
Error: Trying to open unclosed connection.
I do not know if it is because is asynchronous
As already stated you could do it using useDb function :
Example code :
async function myDbConnection() {
const url = 'mongodb+srv://username:password#cluster0-pauvx.mongodb.net/test?retryWrites=true&w=majority';
try {
await mongoose.connect(url, { useNewUrlParser: true });
console.log('Connected Successfully')
// Here from above url you've already got connected to test DB,
So let's make a switch as needed.
mongoose.connection.useDb('myDB'); // Switching happens here..
/**
* Do some DB transaction with mongoose models as by now models has already been registered to created DB connection
*/
} catch (error) {
console.log('Error connecting to DB ::', error);
}
}
Or if you wanted to create a complete new connection then you've to try mongoose.createConnection(). Just for reference in case of mongoDB driver you would use ::
mongodb.MongoClient.connect(mongourl, function(err, primaryDB) {
// open another database over the same connection
const secondaryDB = primaryDB.db(SECONDARY_DATABASE_NAME);
// now you can use both `database` and `database2`
...
});
Ref : mongoose multiple different connections, mongoose useDb(), mongoDB driver switch connections
It is asynchronous. If you pass a callback function to disconnect and try to connect to the next database in that callback, it will work.
Ex.
var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test1', function() {
console.log('Connected to test 1')
mongoose.disconnect(connectToTest2)
})
function connectToTest2() {
mongoose.connect('mongodb://localhost/test2', function() {
console.log('Connected to test 2')
process.exit()
})
}
The top-voted answer had thrown me in a loop for a few hours. To help the OP and others who may have the same issue, here's my take on the solution:
Suppose you have two databases with the same schema and you want to switch between them on the fly. Let's call them DB1 and DB2. Here's how you can go about doing that:
(async () => {
const connection = await mongoose.createConnection(url, options);
const getModel = (database) => {
const dbConnection = connection.useDb(database);
return dbConnection.model('Model', modelSchema);
};
const Db1Model = getModel('DB1');
const Db2Model = getModel('DB2');
})();
Tested on Node.js v12 and Mongoose v5.
One way you can achieve this is by appending the database name with the database URL. For eg: if you are working with localhost
mongoose.connect('mongodb://localhost:portNumber/xyz_db');
When you connect like this, all your models will be saved in the xyz_db under your model as a collection.
You should use the useDb function like so:
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.useDb('Users'); # Change the string to the name of the database you want to use