Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDbContext.cs GetDatabaseName Function Not Working Well #556

Open
yavuzgunay opened this issue Jul 21, 2024 · 2 comments
Open

MongoDbContext.cs GetDatabaseName Function Not Working Well #556

yavuzgunay opened this issue Jul 21, 2024 · 2 comments

Comments

@yavuzgunay
Copy link

Hi,

I had a problem getting the database name in the function you just wrote. I use the connection string structure provided by MongoDB. I've improved your code below. I updated your structure and authSource to bring the database name from there if there is one. If it fits your structure, you can apply this code in the project.

My connectionString structure "mongodb+srv://server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB"

private string GetDatabaseName(string mongoDbConnectionString) { int dbNameStartIndex = mongoDbConnectionString.IndexOf("authSource=") + "authSource=".Length; if (dbNameStartIndex < "authSource=".Length) { var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1); if (databaseName.Contains("?")) { databaseName = databaseName.Substring(0, databaseName.IndexOf("?", StringComparison.InvariantCultureIgnoreCase)); } return databaseName; } int dbNameEndIndex = mongoDbConnectionString.IndexOf('&', dbNameStartIndex); if (dbNameEndIndex == -1) { dbNameEndIndex = mongoDbConnectionString.Length; } return mongoDbConnectionString.Substring(dbNameStartIndex, dbNameEndIndex - dbNameStartIndex); }

@Oceania2018
Copy link
Member

@iceljc Can you take look at this? @yavuzgunay It will be more productive if you can PR this change.

@iceljc
Copy link
Collaborator

iceljc commented Jul 22, 2024

@Oceania2018 @yavuzgunay

Here is the PR.
#558

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants