January 2019
Intermediate to advanced
520 pages
14h 32m
English
To connect to a database, we extract the connection URL from the --database command-line argument. If it isn't set, we use the mongodb://localhost:27017/admin default value:
let addr = matches.value_of("database") .unwrap_or("mongodb://localhost:27017/admin");let url = Url::parse(addr)?;
But we also parse it to the Url struct. This is necessary because MongoDB connections expect options sets to be collected by separate values:
let opts = ConnectionOptionsBuilder::new() .with_host(url.host_str().unwrap_or("localhost")) .with_port(url.port().unwrap_or(27017)) .with_db(&url.path()[1..]) .build();let manager = MongodbConnectionManager::new(opts);let pool = Pool::builder() .max_size(4) .build(manager)?;let conn = pool.get()?; ...Read now
Unlock full access