I took the code from the documentation, but it doesn’t work.
pub fn get_countries(&self) {
let cursor = self.countries.find(None, None);
for doc in cursor {
println!("{}", doc?)
}
}
mongodb::sync::Cursor<bson::Document> doesn't implement std::fmt::Display
mongodb::sync::Cursor<bson::Document> cannot be formatted with the default formatter
the ? operator can only be applied to values that implement std::ops::Try
the ? operator cannot be applied to type mongodb::sync::Cursor<bson::Document>
Also the cursor.collect() does not work correctly.
the method
collect
exists for enumstd::result::Result<mongodb::sync::Cursor<bson::Document>, mongodb::error::Error>
, but its trait bounds were not satisfiedmethod cannot be called on
std::result::Result<mongodb::sync::Cursor<bson::Document>, mongodb::error::Error>
due to unsatisfied trait bounds
I tried using cursor.iter() or cursor.into_iter(), the result was the same
Full code of module
use bson::Document;
use mongodb::{
error::Error,
sync::{ Collection, Database},
};
pub struct Core {
db: Database,
countries: Collection<Document>,
}
impl Core {
pub fn new(db: &Database) -> Core {
Core {
db: db.clone(),
countries: db.collection("countries"),
}
}
pub fn get_country(&self, name: &String) -> Result<Option<Document>, Error> {
self.countries.find_one(bson::doc! { "idc": name }, None)
}
pub fn get_countries(&self) {
let cursor = self.countries.find(None, None);
for doc in cursor {
println!("{}", doc?)
}
}
}
2
Answers
My solution
It seems that the
doc
value is returning aCursor
, so I’m guessing thatcursor
must be rather theResult<Cursor<T>>
type returned by theCollection::find
method. https://docs.rs/mongodb/latest/mongodb/sync/struct.Collection.html#method.findShouldn’t you unwrap (or handle the result with a proper match) your
self.countries.find(None, None)
result ?