3

I have a .net core project with MongoDB.

Before starting to work with this I need to create DB for this manually.

I mean I need to go mongo shell and use next commands like this:

use admin
db.createUser(
  {
    user: "admin",
    pwd: "abc123!",
    roles: [ { role: "root", db: "admin" } ]
  }
);
exit;

But I don't like do this every time when I need to deploy my project.

How can I automate this process and creating DB dynamically from C# code or use some other solution if it's possible?

P.S. I use this article for building work with MongoDB + .net core.

1 Answer 1

1

The solution is to use a mongoDb driver. U can get it via nuget.

In general u just instantiate a server object and call GetDatabase to create a Database.

server.GetDatabase("myDB");

This way you can also create users using the mongoDb driver as mentioned here : How to create a user in MongoDB

Further reading:

https://www.nuget.org/packages/MongoDB.Driver/2.3.0

http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/installation/

The following Video provides a short tutorial how to do it: https://www.youtube.com/watch?v=6x0-vHHHpv8 Thanks to that answer : https://stackoverflow.com/a/30629593/4992212

Sign up to request clarification or add additional context in comments.

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.