我使用Actix-Web4.0.0.rc3和MongoDB 2.1.0 (异步模式)。我已经通过app_data将db与Actix集成在一起,但我正在努力研究如何显示多文档游标的结果。
目前我正在使用:
while let Some(doc) = cursor.try_next().await? {
// processing doc results, and building a row of data
}
说出结果很容易,但我不知道如何在浏览器中显示这些结果。我一直在寻找一个例子来解释如何做到这一点,但到目前为止还没有找到解决方案。使用手柄很容易从DB中获得一个字段,并显示这些结果,但是未知大小的游标使我感到困惑。我已经很容易地用RoR,或者凤凰/Elixir做了这件事--会很感激你的任何建议(或者只是对一个可能已经存在的例子的轻柔提示)。
谢谢
发布于 2022-02-24 12:33:20
我有个例子:
pub async fn get_aggregation_as_json_response(
db: &Database,
aggr_pipeline: Vec<Document>,
) -> HttpResponse {
let mut cursor = db
.collection_with_type::<ExampleModel>("examplemodel")
.aggregate(aggr_pipeline, None)
.await
.expect("Error performing aggregation on examplemodel collection.");
let mut results: Vec<Document> = Vec::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
results.push(document);
}
Err(_) => {
return HttpResponse::InternalServerError().finish();
}
}
}
HttpResponse::Ok().json(results)
}
这就是你要找的吗?
https://stackoverflow.com/questions/71257857
复制