0

I am trying to create a new database using mongo. (not a collection in the current database like the result of use "newDBname" ) Does anyone know how to do so?

An example in c# would be appreciated.

have a great day

2 Answers 2

1

according to doc, there is no such explicit operation/query, the database will be created implicitly when you will first time query to it if it wasn't created before. In c# it will be:

 var database = client.GetDatabase("db_name");  // on this step, the server doesn't know that you want to create db_name
 database.ListCollections(null, CancellationToken.None); // the database will be created only after running query, for example "ListCollections"
Sign up to request clarification or add additional context in comments.

1 Comment

You're right this is the correct way to do it. Additional in my case the actual problem was that I did it async way and I inserted the data before the new db created
0

With mongoose ? If you're using mongoose, create a new schema for new collection.

After launch your backend, the new collection should be create.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.