You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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); }
The text was updated successfully, but these errors were encountered:
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); }
The text was updated successfully, but these errors were encountered: