When your MongoDB connection string password contains special characters (like @, !, #, etc.), you need to URL encode the password. Use encodeURIComponent() to properly encode the password:
const password = 'password-with-special@characters'; const encodedPassword = encodeURIComponent(password);
const connectionString = mongodb+srv://username:${encodedPassword}@cluster.mongodb.net/database
;
You can checkitout here, for more info: https://www.mongodb.com/docs/atlas/troubleshoot-connection/#incorrect-connection-string-format