You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What operating system are you using? macOS Sonoma 14.5
What versions of the driver and its dependencies are you using? mongodb 2.8.2, bson 2.11.0
What version of MongoDB are you using? 7.0.11
What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? Standalone, locally hosted
Describe the bug
I am trying to use the find_one() method on a collection typed with a custom struct, but the compiler cannot find the method.
rustc output: 'error[E0599]: no method named find_one found for struct mongodb::Collection<User> in the current scope'
This error doesn't occur if I just retrieve Documents rather than my User struct.
Reproduce with this code:
use mongodb::{
bson::{doc,DateTime},Client,};use serde::{Deserialize,Serialize};constMONGODB_URI:&'static str = "mongodb://localhost";// Represents a document in the collection#[derive(Clone,Debug,Serialize,Deserialize)]structUser{id:u32,name:&'static str,last_login:DateTime,}implPartialEqforUser{fneq(&self,other:&Self) -> bool{self.id == other.id && self.name == other.name && self.last_login == other.last_login}}asyncfnget_user(name:&str) -> User{let client = Client::with_uri_str(MONGODB_URI).await.expect("Database should be connectable");// Get user bios collectionlet users_collection = client.database("skyserver").collection::<User>("users");let retrieved_user:User = users_collection
.find_one(doc!{"name": name},None).await.expect("User should be in the database");
retrieved_user
}#[cfg(test)]mod test {use mongodb::bson::DateTime;usecrate::{get_user,User};constTEST_USER:User = User{id:1,name:"Sample User",last_login:DateTime::from_millis(0),};#[tokio::test]asyncfngets_sample_user(){// upsert_sample_user().await;assert_eq!(get_user("Sample User").await, TEST_USER);}}
I have already tried to uses the clone_with_type() collection method, with the same results.
I am new to the mongodb Rust driver so I might be missing something obvious, thank you in advance for your help!
The text was updated successfully, but these errors were encountered:
Hey @SkylerMime, thanks for opening this issue! The find_one method only exists for Collection<T> where T: DeserializeOwned. Deriving or implementing the Deserialize trait is usually sufficient to satisfy this bound; however, this doesn't work when your struct contains borrowed data. This deserializer lifetimes guide gives a nice overview of why references in structs lead to different deserialization behavior, specifically the bullet point for <T> where T: DeserializeOwned under the trait bounds section. Note this sentence in particular:
the data that is being deserialized from is going to be thrown away before the function returns, so T must not be allowed to borrow from it
The driver does not hold onto the BSON bytes it receives from the server after find_one finishes executing, so the type deserializing from those bytes cannot store references to them.
The fix for this problem is to make sure that all of the fields in your struct are owned, which in your case would entail updating the name field to be a regular String:
Versions/Environment
1.79.0
macOS Sonoma 14.5
mongodb 2.8.2, bson 2.11.0
7.0.11
Standalone, locally hosted
Describe the bug
I am trying to use the find_one() method on a collection typed with a custom struct, but the compiler cannot find the method.
rustc output: 'error[E0599]: no method named
find_one
found for structmongodb::Collection<User>
in the current scope'This error doesn't occur if I just retrieve Documents rather than my User struct.
Reproduce with this code:
I have already tried to uses the clone_with_type() collection method, with the same results.
I am new to the mongodb Rust driver so I might be missing something obvious, thank you in advance for your help!
The text was updated successfully, but these errors were encountered: