{"library":"mongodb-connection-string-url","title":"MongoDB Connection String URL Parser","description":"This package provides a robust API for parsing and manipulating MongoDB connection strings, aligning with the WhatWG URL API specification while introducing MongoDB-specific extensions. It is currently at version 7.0.1 (as of Jan 2026) and is actively maintained by the MongoDB Node.js team. Releases are typically aligned with the major versions of the `mongodb` driver, with rapid iterations within a major version to address issues. Key differentiators from a standard URL object include case-insensitive URL parameters, a `.hosts` property for extracting all host details, an `.isSRV` property to identify `mongodb+srv` schemes, and the immutability of standard `.host`, `.hostname`, `.port`, and `.href` properties, which cannot be directly set and may not return meaningful results in the MongoDB context.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mongodb-connection-string-url"],"cli":null},"imports":["import ConnectionString from 'mongodb-connection-string-url';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import ConnectionString from 'mongodb-connection-string-url';\n\n// Example 1: Basic connection string manipulation\nconst cs1 = new ConnectionString('mongodb://user:pass@host1:27017,host2:27018/mydatabase?replicaSet=rs0&authSource=admin');\ncs1.searchParams.set('readPreference', 'secondary');\nconsole.log('Modified URL:', cs1.href); // 'mongodb://user:pass@host1:27017,host2:27018/mydatabase?replicaSet=rs0&authSource=admin&readPreference=secondary'\nconsole.log('Hosts:', cs1.hosts.map(h => `${h.host}:${h.port}`)); // ['host1:27017', 'host2:27018']\nconsole.log('Is SRV:', cs1.isSRV); // false\n\n// Example 2: Using mongodb+srv (DNS Seed List) with environment variables\n// For a real application, replace placeholders with actual values from process.env\nconst username = process.env.MONGO_USER ?? 'myUser';\nconst password = process.env.MONGO_PASS ?? 'myComplexPassword123';\nconst srvHost = process.env.MONGO_SRV_HOST ?? 'cluster0.example.com';\nconst secureCs = new ConnectionString(`mongodb+srv://${username}:${password}@${srvHost}/testdb?retryWrites=true&w=majority`);\nconsole.log('SRV URL (obfuscated pass):', secureCs.href.replace(password, '******'));\nconsole.log('Is SRV:', secureCs.isSRV); // true\nconsole.log('Database from SRV:', secureCs.pathname.substring(1)); // 'testdb'","lang":"typescript","description":"Demonstrates parsing various MongoDB connection string formats, modifying URL parameters, and accessing MongoDB-specific properties like `hosts` and `isSRV`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}