如何在 r2d2 和 actix 中使用 MongoDB
How to use MongoDB with r2d2 and actix in rust
我正在尝试使用 rust 语言,使用 actix 框架和 r2d2 制作一个基本的网络应用程序 以 mongodb 作为数据库。我找不到任何关于如何存档的完整和有效的文档。也许有人可以帮我。
问题是,我似乎无法从 r2d2 连接池获得 mongodb 连接。遗憾的是,我找到的任何文档都没有涵盖这部分内容。
我找到的一些链接:
- 将 r2d2 与 actix 一起使用:https://github.com/actix/examples/blob/master/r2d2/src/main.rs
- 将 mongodb 与 r2d2 一起使用:https://docs.rs/r2d2-mongodb/0.2.2/r2d2_mongodb/
这部分创建连接池并交给actix。
fn main() {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let manager = MongodbConnectionManager::new(
ConnectionOptions::builder()
.with_host("localhost", 27017)
.with_db("mydatabase")
.build()
);
let pool = Pool::builder()
.max_size(16)
.build(manager)
.unwrap();
HttpServer::new( move || {
App::new()
// enable logger
.wrap(middleware::Logger::default())
// store db pool in app state
.data(pool.clone())
// register simple handler, handle all methods
.route("/view/{id}", web::get().to(view))
})
.bind("127.0.0.1:8080")
.expect("Can not bind to port 8080")
.run()
.unwrap();
}
这是试图访问连接池的处理函数
fn view(req: HttpRequest,
pool: web::Data<Pool<MongodbConnectionManager>>) -> impl Responder {
let id = req.match_info().get("id").unwrap_or("unknown");
let conn = pool.get().unwrap();
let result = conn.collections("content").findOne(None, None).unwrap();
// HERE BE CODE ...
format!("Requested id: {}", &id)
}
这是显示我的问题的错误。 conn 变量似乎不是正确的 mongodb 连接。
error[E0599]: no method named `collections` found for type `std::result::Result<r2d2::PooledConnection<r2d2_mongodb::MongodbConnectionManager>, r2d2::Error>` in the current scope --> src\main.rs:29:23
|
29 | let result = conn.collections("content").findOne(None, None).unwrap();
|
10 | let coll = conn.collection("simulations");
| ^^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
`use crate::mongodb::db::ThreadedDatabase;`
我的编译器告诉我在范围内添加 mongodb::db::ThreadedDatabase
。
我正在尝试使用 rust 语言,使用 actix 框架和 r2d2 制作一个基本的网络应用程序 以 mongodb 作为数据库。我找不到任何关于如何存档的完整和有效的文档。也许有人可以帮我。
问题是,我似乎无法从 r2d2 连接池获得 mongodb 连接。遗憾的是,我找到的任何文档都没有涵盖这部分内容。
我找到的一些链接:
- 将 r2d2 与 actix 一起使用:https://github.com/actix/examples/blob/master/r2d2/src/main.rs
- 将 mongodb 与 r2d2 一起使用:https://docs.rs/r2d2-mongodb/0.2.2/r2d2_mongodb/
这部分创建连接池并交给actix。
fn main() {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let manager = MongodbConnectionManager::new(
ConnectionOptions::builder()
.with_host("localhost", 27017)
.with_db("mydatabase")
.build()
);
let pool = Pool::builder()
.max_size(16)
.build(manager)
.unwrap();
HttpServer::new( move || {
App::new()
// enable logger
.wrap(middleware::Logger::default())
// store db pool in app state
.data(pool.clone())
// register simple handler, handle all methods
.route("/view/{id}", web::get().to(view))
})
.bind("127.0.0.1:8080")
.expect("Can not bind to port 8080")
.run()
.unwrap();
}
这是试图访问连接池的处理函数
fn view(req: HttpRequest,
pool: web::Data<Pool<MongodbConnectionManager>>) -> impl Responder {
let id = req.match_info().get("id").unwrap_or("unknown");
let conn = pool.get().unwrap();
let result = conn.collections("content").findOne(None, None).unwrap();
// HERE BE CODE ...
format!("Requested id: {}", &id)
}
这是显示我的问题的错误。 conn 变量似乎不是正确的 mongodb 连接。
error[E0599]: no method named `collections` found for type `std::result::Result<r2d2::PooledConnection<r2d2_mongodb::MongodbConnectionManager>, r2d2::Error>` in the current scope --> src\main.rs:29:23
|
29 | let result = conn.collections("content").findOne(None, None).unwrap();
|
10 | let coll = conn.collection("simulations");
| ^^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
`use crate::mongodb::db::ThreadedDatabase;`
我的编译器告诉我在范围内添加 mongodb::db::ThreadedDatabase
。