Update mongodb example (#2944)

This commit is contained in:
Jonas Platte 2024-09-28 21:40:52 +00:00 committed by GitHub
parent 52fd139a86
commit 3df15fd2ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 9 deletions

View file

@ -6,9 +6,9 @@ publish = false
[dependencies]
axum = { path = "../../axum" }
mongodb = "2.8.0"
mongodb = "3.1.0"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = ["full"] }
tower-http = { version = "0.5.0", features = ["add-extension", "trace"] }
tower-http = { version = "0.6.1", features = ["add-extension", "trace"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

View file

@ -30,7 +30,7 @@ async fn main() {
// pinging the database
client
.database("axum-mongo")
.run_command(doc! { "ping": 1 }, None)
.run_command(doc! { "ping": 1 })
.await
.unwrap();
println!("Pinged your database. Successfully connected to MongoDB!");
@ -38,8 +38,9 @@ async fn main() {
// logging middleware
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_mongo=debug,tower_http=debug".into()),
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();
@ -70,7 +71,7 @@ async fn create_member(
State(db): State<Collection<Member>>,
Json(input): Json<Member>,
) -> Result<Json<InsertOneResult>, (StatusCode, String)> {
let result = db.insert_one(input, None).await.map_err(internal_error)?;
let result = db.insert_one(input).await.map_err(internal_error)?;
Ok(Json(result))
}
@ -81,7 +82,7 @@ async fn read_member(
Path(id): Path<u32>,
) -> Result<Json<Option<Member>>, (StatusCode, String)> {
let result = db
.find_one(doc! { "_id": id }, None)
.find_one(doc! { "_id": id })
.await
.map_err(internal_error)?;
@ -94,7 +95,7 @@ async fn update_member(
Json(input): Json<Member>,
) -> Result<Json<UpdateResult>, (StatusCode, String)> {
let result = db
.replace_one(doc! { "_id": input.id }, input, None)
.replace_one(doc! { "_id": input.id }, input)
.await
.map_err(internal_error)?;
@ -107,7 +108,7 @@ async fn delete_member(
Path(id): Path<u32>,
) -> Result<Json<DeleteResult>, (StatusCode, String)> {
let result = db
.delete_one(doc! { "_id": id }, None)
.delete_one(doc! { "_id": id })
.await
.map_err(internal_error)?;