How to create multiple database in mongodb atlas with single request

I want to create seven different databases with a single request in mongoDb Atlas on submission of a form.I am looking for any mongoose feature or module with which I can create multiple databases on a single request.I searched for Atlas API but there is no API for this purpose.Is there any mongoose feature with which I can perform this task. I don’t want to make multiple connection request, I tried createConnection of mongoose but in that I have to use it seven time.I want to perform this task in one request.

Welcome to the MongoDB Community Forums @Shrishti_Raghav1!

Databases are implicitly created when you insert the first data into a collection in a database namespace.

As far as I’m aware there is no driver or API command that will create multiple databases in a single request, however this is not a particularly time consuming task.

Please provide some further details:

  • What are you hoping to gain from creating all of the collections in a single request?

  • Are you creating empty collections/databases or loading some initial data as well?

If you send multiple server commands in a loop, the Node.js driver will be creating and reusing a single connection rather than creating multiple connections. The Node.js driver maintains a connection pool to efficiently reuse established connections and avoid some of the overhead of establishing a connection.

A few approaches to consider:

  • If you are using MongoDB 4.4+ and want to ensure that all seven collections are created at the same time, you could consider creating collections in a transaction. However, you’d still have to call createCollection() seven times and this is unlikely to have any noticeable performance benefit.

  • If you are looking for a more convenient way to create a standard set of collections/databases with initial data, you could perhaps use mongodump and mongorestore to backup & restore multiple databases. This still translates into the same underlying server commands but you can adjust concurrency options like --numParallelCollections and --numInsertionWorkersPerCollection to speed up import of a larger data set.

Regards,
Stennie

Hi Stennie,

I want to create Seven to ten databases with no initial data and then assign a user to them.I will be having many users , each user will be assigned their separate collection of databases which I created using single request.

Regards,
Shrishti