我是 Rust 的新手,正在尝试使用 Actix-web 创建一个网络服务器,以通过 MongoDB 执行 CRUD 操作。我创建的第一个 API 是通过从 POST 请求接收到的内容在 MongoDB 中保存一个简单的文档。发布请求处理函数的代码是:
extern crate r2d2;
extern crate r2d2_mongodb;
use r2d2::Pool;
use r2d2_mongodb::mongodb::db::ThreadedDatabase;
use r2d2_mongodb::{ConnectionOptions, MongodbConnectionManager};
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use bson::{doc, Bson, Document};
async fn post_request(info: web::Json<Info>, pool: web::Data<Pool<MongodbConnectionManager>>) -> HttpResponse {
let name: &str = &info.name;
let connection = pool.get().unwrap();
let doc = doc! {
"name": name
};
let result = connection.collection("user").insert_one(doc, None);
HttpResponse::Ok().body(format!("username: {}", info.name))
}
我正在使用 r2d2 为 MongoDB 建立连接池,而不是打开和关闭连接。我得到的错误是
error[E0308]: mismatched types
--> src/main.rs:17:59
|
17 | let result = connection.collection("user").insert_one(doc, None);
| ^^^ expected struct `OrderedDocument`, found struct `bson::Document`
insert_one
函数文档说它接受 a但是当bson::Document
我给它时,它说expected struct `r2d2_mongodb::mongodb::ordered::OrderedDocument`
这是我的 Cargo.toml 依赖项
mongodb = "1.1.1"
actix-web = "3.3.2"
dotenv = "0.15.0"
r2d2-mongodb = "0.2.2"
r2d2 = "0.8.9"
serde = "1.0.118"
bson = "1.1.0"
我该如何纠正?