No method named `database` found for enum `std::result::Result<Client, mongodb::error::Error>` in the current scope

According to mongodb - Rust , I tested that code such as:

use mongodb::{Client, options::ClientOptions,options::StreamAddress};
pub use bson::{doc, bson};
use mongodb::bson;

fn main(){

let client = Client::with_uri_str("mongodb://localhost:27017/");

let options = ClientOptions::builder().hosts(vec![StreamAddress{hostname: "localhost".into(),port: Some(27017),}]).build();

let client = Client::with_options(options);// Get a handle to the deployment.
let db = client.database("some_db");
for coll_name in db.list_collection_names(None) {
    println!("collection: {}", coll_name);
}

let coll = db.collection("some-coll");
let result = coll.insert_one(doc! { "x": 1 }, None);
println!("{:#?}", result);
}

But I get the following error:

no method named `database` found for enum `std::result::Result<Client, mongodb::error::Error>` in the current scope

How to solve it? database method doesn't exist.

Client::with_options returns a Result value, meaning that it could fail, and you need to handle the possibility of a mongodb::error::Error before you can access the "contained" Client. The simplest approach is to write

let client: Client = Client::with_options(options).unwrap();

which will panic (terminate the running application with an error message and backtrace) if Client::with_options returns an Err. There are also other, more robust and flexible error-handling strategies; to learn about them, and about when different strategies are appropriate, you can read this chapter of The Rust Programming Language.

Hello @m_pahlevanzadeh

I rewrote your code - now it compiles and seems to do something.
Cargo.toml

[package]
name = "mongodb_test"
version = "0.1.0"
authors = ["Stefan Dörig <sdoerig@bluewin.ch>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mongodb = {version = "1.1.1", default-features = false, features = ["sync"]}
bson = "1"

And the code main.rs

use std::{ process};
use mongodb::{sync::Client, options::ClientOptions,options::StreamAddress};
pub use bson::{doc, bson};
use mongodb::bson;

fn main(){

let client = Client::with_uri_str("mongodb://localhost:27017/");

let options = ClientOptions::builder().hosts(vec![StreamAddress{hostname: "localhost".into(),port: Some(27017),}]).build();

let client = Client::with_options(options);// Get a handle to the deployment.

let db = match client {
    Ok(client) => client.database("some_db"),
    Err(_e) => process::exit(1)
};

let coll_vec = match db.list_collection_names(None) {
    Ok(v) => v,
    Err(_e) => Vec::new()
};

for coll_name in coll_vec {
    println!("collection: {}", coll_name);
}

let coll = db.collection("some-coll");
let result = coll.insert_one(doc! { "x": 1 }, None);
println!("{:#?}", result);
}

Please note this runs on the sync mongodb client. Compiled it with Rust 1.52.

Hope it helps.

Stefan

1 Like

I would suggest just .unwrap() here, which will give the user more information if an Err occurs. Also note that exit code 0 usually indicates successful termination, so using it here is not a great idea.

1 Like

@cole-miller
True and true - I just was glad it compiled and did not much thinking.

Stefan

1 Like