Error access mongoDM collection API

I'm building a minimal web server with tokio/axum and MongoDB.
I have a file src/config/database.rs containing my MongoDB configuration

use mongodb::{options::ClientOptions, Client};
use std::env;

pub async fn mongodb() -> mongodb::Client {
    let database_uri = env::var("DATABASE_URI").expect("error reading database URI");
    let database_name = env::var("DATABASE_NAME").expect("error reading database name");

    // Get a handle to the cluster  and Ping the server to see if you can connect to the cluster
    let database_client_options = ClientOptions::parse(&database_uri)
        .await
        .expect("err connecting  to database");

    let database_client =
        Client::with_options(database_client_options).expect("unable to connect to db");
    database_client.database(&database_name);
    println!("Successfully Connected to Database.");

    //return database client
    database_client
}

I imported this configuration into my src/main.rs an initialize the config thus

...
  config::database::mongodb().await;
mod config;

I want to be able to perform CRUD operation in src/controllers/auth.rs thus

use crate::{config::database::mongodb, shared::user_schema::User};
///create a new user
pub async fn sign_up(Json(payload): Json<User>) -> impl IntoResponse {
    let database = mongodb().await;

    let collection = database.collection::<User>("user").await;
 ...
}

But I'm getting the following error

 Compiling nitrogen v0.1.0 (/home/drizzle/devs/nitrogen)
error[E0599]: no method named `collection` found for struct `Client` in the current scope
  --> src/controllers/auth.rs:12:31
   |
12 |     let collection = database.collection::<User>("user").await;
   |                               ^^^^^^^^^^ method not found in `Client`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `nitrogen` due to previous error
[Finished running. Exit status: 101]

I really don't know what I'm doing wrong please see the full project on GitHub

See the very first example of Client. Collections are associated with a database, not with a client.

@H2CO3 I suppose I've mounted the database here with the .database(&database_name); method

But you ignored its return value. The method .collection() is not on Client, it's on the returned database.

Okays. Could you point out where I'm ignoring the returned database value. I can't see to figure it out

Again, see the example. You have to call .collection() on the return value of client.database(). You are not storing the return value of client.database() anywhere, and you are not returning it. You are returning the client itself, on which you are trying to call .collection(), but the Client type doesn't have any method named collection.

I think I've gotten it
I updated src/config/database.rs to

use mongodb::{options::ClientOptions, Client, Database};
use std::env;

pub async fn mongodb() -> mongodb::Database{
    let database_uri = env::var("DATABASE_URI").expect("error reading database URI");
    let database_name = env::var("DATABASE_NAME").expect("error reading database name");

    // Get a handle to the cluster  and Ping the server to see if you can connect to the cluster
    let database_client_options = ClientOptions::parse(&database_uri)
        .await
        .expect("err connecting  to database");

    let database_client =
        Client::with_options(database_client_options).expect("unable to connect to db");
  let database =  database_client.database(&database_name);
    println!("Successfully Connected to Database.");

    //return database
    database
}
1 Like