Search code examples
mongodb.net-coremongodb-.net-drivermlab

How to connect Mongolab with MongoDb.Driver


i'm testing mongolab with .netCore using MongoDb.Driver. I have this connection string

mongodb://<dbuser>:<dbpassword>@mymongolaburl:46367/somedatabase

And i'm connecting this way

        var connectionString = @"mongodb://<dbuser>:<dbpassword>@mymongolaburl:46367/somedatabase";
        var databaseName = "somedatabase";
        var client = new MongoClient(connectionString);
        if (client != null)
        {
            _database = client.GetDatabase(databaseName);

            _database.GetCollection<User>("User").InsertOne(new User {Name="Luke Skywalker" });
        }

It is not working 'cause it says the database name is invalid, if i use the connection string without the database name

mongodb://<dbuser>:<dbpassword>@mymongolaburl:46367

I get a timeout execption. I Already connected to database using Robo 3T.

Thanks in advance.


Solution

  • After one day i found how to do that in this link

    The The solution is to specify which is database in the connection string

    mongodb://<dbuser>:<dbpassword>@mymongolaburl:46367/?authSource=somedatabase
    

    Thank you!