How to drop a database with Mongoose? - node.js
I'm preparing a database creation script in Node.js and Mongoose.
How can I check if the database already exists, and if so, drop (delete) it using Mongoose?
I could not find a way to drop it with Mongoose.
There is no method for dropping a collection from mongoose, the best you can do is remove the content of one :
Model.remove({}, function(err) {
console.log('collection removed')
});
But there is a way to access the mongodb native javascript driver, which can be used for this
mongoose.connection.collections['collectionName'].drop( function(err) {
console.log('collection dropped');
});
Warning
Make a backup before trying this in case anything goes wrong!
Mongoose will create a database if one does not already exist on connection, so once you make the connection, you can just query it to see if there is anything in it.
You can drop any database you are connected to:
var mongoose = require('mongoose');
/* Connect to the DB */
mongoose.connect('mongodb://localhost/mydatabase',function(){
/* Drop the DB */
mongoose.connection.db.dropDatabase();
});
If you modify #hellslam's solution like this then it will work
I use this technique to drop the Database after my integration tests
//CoffeeScript
mongoose = require "mongoose"
conn = mongoose.connect("mongodb://localhost/mydb")
conn.connection.db.dropDatabase()
//JavaScript
var conn, mongoose;
mongoose = require("mongoose");
conn = mongoose.connect("mongodb://localhost/mydb");
conn.connection.db.dropDatabase();
HTH at least it did for me, so I decided to share =)
Tried #hellslam's and #silverfighter's answers. I found a race condition holding my tests back. In my case I'm running mocha tests and in the before function of the test I want to erase the entire DB. Here's what works for me.
var con = mongoose.connect('mongodb://localhost/mydatabase');
mongoose.connection.on('open', function(){
con.connection.db.dropDatabase(function(err, result){
done();
});
});
You can read more https://github.com/Automattic/mongoose/issues/1469
An updated answer, for 4.6.0+, if you have a preference for promises (see docs):
mongoose.connect('mongodb://localhost/mydb', { useMongoClient: true })
.then((connection) => {
connection.db.dropDatabase();
// alternatively:
// mongoose.connection.db.dropDatabase();
});
I tested this code in my own code, using mongoose 4.13.6. Also, note the use of the useMongoClient option (see docs). Docs indicate:
Mongoose's default connection logic is deprecated as of 4.11.0. Please opt in to the new connection logic using the useMongoClient option, but make sure you test your connections first if you're upgrading an existing codebase!
The difficulty I've had with the other solutions is they rely on restarting your application if you want to get the indexes working again.
For my needs (i.e. being able to run a unit test the nukes all collections, then recreates them along with their indexes), I ended up implementing this solution:
This relies on the underscore.js and async.js libraries to assemble the indexes in parellel, it could be unwound if you're against that library but I leave that as an exerciser for the developer.
mongoose.connection.db.executeDbCommand( {dropDatabase:1}, function(err, result) {
var mongoPath = mongoose.connections[0].host + ':' + mongoose.connections[0].port + '/' + mongoose.connections[0].name
//Kill the current connection, then re-establish it
mongoose.connection.close()
mongoose.connect('mongodb://' + mongoPath, function(err){
var asyncFunctions = []
//Loop through all the known schemas, and execute an ensureIndex to make sure we're clean
_.each(mongoose.connections[0].base.modelSchemas, function(schema, key) {
asyncFunctions.push(function(cb){
mongoose.model(key, schema).ensureIndexes(function(){
return cb()
})
})
})
async.parallel(asyncFunctions, function(err) {
console.log('Done dumping all collections and recreating indexes')
})
})
})
This works for me as of Mongoose v4.7.0:
mongoose.connection.dropDatabase();
2020 update
make a new file call it drop.js i.e
and put inside
require('dotenv').config()
const url = process.env.ATLAS_URI;
mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
mongoose.connection.dropDatabase().then(
async() => {
try {
mongoose.connection.close()
}
catch (err) {
console.log(err)
}
}
);
in your package.json
in your package.json
"scripts": {
"drop": "node models/drop.js",
}
run it on ur console and
To empty a particular collection in a database:
model.remove(function(err, p){
if(err){
throw err;
} else{
console.log('No Of Documents deleted:' + p);
}
});
Note:
Choose a model referring to particular schema(schema of collection
you wish to delete).
This operation will not delete collection name
from database.
This deletes all the documents in a collection.
The best way to drop your database in Mongoose depends on which version of Mongoose you are using. If you are using a version of Mongoose that 4.6.4 or newer, then this method added in that release is likely going to work fine for you:
mongoose.connection.dropDatabase();
In older releases this method did not exist. Instead, you were to use a direct MongoDB call:
mongoose.connection.db.dropDatabase();
However, if this was run just after the database connection was created, it could possibly fail silently. This is related to the connection actually be asynchronous and not being set up yet when the command happens. This is not normally a problem for other Mongoose calls like .find(), which queue until the connection is open and then run.
If you look at the source code for the dropDatabase() shortcut that was added, you can see it was designed to solve this exact problem. It checks to see if the connection is open and ready. If so, it fires the command immediately. If not, it registers the command to run when the database connection has opened.
Some of the suggestions above recommend always putting your dropDatabase command in the open handler. But that only works in the case when the connection is not open yet.
Connection.prototype.dropDatabase = function(callback) {
var Promise = PromiseProvider.get();
var _this = this;
var promise = new Promise.ES6(function(resolve, reject) {
if (_this.readyState !== STATES.connected) {
_this.on('open', function() {
_this.db.dropDatabase(function(error) {
if (error) {
reject(error);
} else {
resolve();
}
});
});
} else {
_this.db.dropDatabase(function(error) {
if (error) {
reject(error);
} else {
resolve();
}
});
}
});
if (callback) {
promise.then(function() { callback(); }, callback);
}
return promise;
};
Here's a simple version of the above logic that can be used with earlier Mongoose versions:
// This shim is backported from Mongoose 4.6.4 to reliably drop a database
// http://stackoverflow.com/a/42860208/254318
// The first arg should be "mongoose.connection"
function dropDatabase (connection, callback) {
// readyState 1 === 'connected'
if (connection.readyState !== 1) {
connection.on('open', function() {
connection.db.dropDatabase(callback);
});
} else {
connection.db.dropDatabase(callback);
}
}
Mongoose 4.6.0+:
mongoose.connect('mongodb://localhost/mydb')
mongoose.connection.once('connected', () => {
mongoose.connection.db.dropDatabase();
});
Passing a callback to connect won't work anymore:
TypeError: Cannot read property 'commandsTakeWriteConcern' of null
beforeEach((done) => {
mongoose.connection.dropCollection('products',(error ,result) => {
if (error) {
console.log('Products Collection is not dropped')
} else {
console.log(result)
}
done()
})
})
To drop all documents in a collection:
await mongoose.connection.db.dropDatabase();
This answer is based off the mongoose index.d.ts file:
dropDatabase(): Promise<any>;
mongoose.connect(`mongodb://localhost/${dbname}`, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: true,
useUnifiedTopology: true
})
.then((connection) => {
mongoose.connection.db.dropDatabase();
});
To delete a complete database, just pass the name...
This one is working perfectly fine on version 4.4
Since the remove method is depreciated in the mongoose library we can use the deleteMany function with no parameters passed.
Model.deleteMany();
This will delete all content of this particular Model and your collection will be empty.
For dropping all documents in a collection:
myMongooseModel.collection.drop();
as seen in the tests
Related
How can I get rid of this MongoDB error? mongoose MongoNotConnectedError: MongoClient must be connected to perform this operation
Please I am using the MongoDB database for my next application that is my final project for me Bootcamp and there is this error that has prevented me from making queries to my database as I always get the mongoclient not connected error. I am using mongoose and this error started after I upgraded to the latest mui(material UI) because that is what I am using for this application. I have been trying since yesterday to fix this error as I thought it is something I could handle but till this moment it persist. It has been going from this mongoose MongoNotConnectedError: MongoClient must be connected to perform this operation and this one MongoExpiredSessionError: Cannot use a session that has ended` and it happens on every button that clicks that makes a request to the database. Below is the code I am using to connect to MongoDB with mongoose: import mongoose from 'mongoose'; const connection = {}; async function connect() { if (connection.isConnected) { console.log('already connected'); return; } if (mongoose.connections.length > 0) { connection.isConnected = mongoose.connections[0].readyState; if (connection.isConnected === 1) { console.log('use previous connection'); return; } await mongoose.disconnect(); } const db = await mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true, }); console.log('new connection'); connection.isConnected = db.connections[0].readyState; } async function disconnect() { if (connection.isConnected) { if (process.env.NODE_ENV === 'production') { await mongoose.disconnect(); connection.isConnected = false; } else { console.log('not disconnect'); } } } function convertDocToObj(doc) { doc._id = doc._id.toString(); doc.createdAt = doc.createdAt.toString(); doc.updatedAt = doc.updatedAt.toString(); return doc; } const db = { connect, disconnect, convertDocToObj }; export default db; I will really appreciate it so much if anybody can help me out with this problem, please. I don't know what is causing it or where it is coming from as I have tried to as much as i can to all to no0 avail Thanks
I see that you are using the mongo URL as an environment variable with MONGODB_URI. Do you have the dotenv module installed? Did you require('dotenv').config() in your applications? is the `.env file in the root directory of your project? If so, if you are using MongoDB Atlas, make sure the URL in your .env file is the correct one. When you generate the connection string from MongoDB Atlas it gives a default DB in the URL string named myFirstDatabase or something like that. Change it to the DB you want to connect to.
Should I open/close database connection after every query/insert?
Hey everyone i'm developer a simple app in last days using nodejs and create this function to return client instance from mongodb const mongodb = require("mongodb"); const { db } = require("../config/env"); const conection = async () => { try { const client = await mongodb.MongoClient.connect(db.uri, { useNewUrlParser: true, useUnifiedTopology: true, }); return client; } catch (error) { throw error; } }; module.exports = conection; and i make this simple function for acess data layer and return records instered const index = async ({ limit = 10, offset = 0, filter = {} }) => { const client = await conection(); if (filter._id) { filter._id = mongodb.ObjectID(filter._id); } try { const collection = client.db("api").collection("user"); const data = await collection .find({ ...filter }) .skip(offset) .limit(limit) .toArray(); return data; } catch (error) { throw new Error(error); } finally { await client.close(); } }; I would like to know if I really need to make the connection and close it with each query or should I keep the connection open NOTE: in this case I am using a simple Atlas cluster (free) but I would like to know if I should do this also when working with sql banks like postgres
Don't close your connection unless you are exiting your app, and then make sure you do. Ensure that you are using the same connection when you come back to do more I/O. Database connections are resource-intensive and should be established as few times as possible and shared as much as possible. You can also get middleware problems with connections like ODBC if you don't pick up existing connections and get weird errors like connection pools running out. Get to know your connector and how to use it most effectively, it will be a rewarding activity :-)
You can use the mongoose module for managing MongoDB. Installation npm install mongoose Usage mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true }); I am sure that Mongoose can help you solve your issues.
It's a good practice to do so. so that after every operation(insert etc) you close the connection and before every operation(insert etc) you reopen the connection.
connection.on is not a function in mongoose v4.13
I'm trying to clear collection of the entire database but what's wrong with below code? const mongoose = require('mongoose') mongoose.Promise = global.Promise const connection = mongoose.connect( mongodb://admin#example.com/myDB ) exports.default = (dropAllCollections = () => { connection.on('open', function() { connection.db.listCollections().toArray(function(err, names) { if (err) { console.log(err) } else { console.log(names) } mongoose.connection.close() }) }) })() How to find if something has changed in mongoose api? above code is from one of the answer in other question.
Still you are using old version of mongoose, you can try using connection.db.collectionNames Try running your collection names function after connection. connection.on('open', function (ref) { connection.db.collectionNames(function (err, names) { console.log(names); }); }) you can read the docs from here and here
I'm sorry about ressurrecting this topic, but if anyone has the same problem (I'm using 4.13 too), go to your tsconfig.json and delete the following line: "esModuleInterop": true
Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true
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() }
switching database with mongoose
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