mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
refactor: update mongodb to 3
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
[package]
|
||||
name = "mongodb"
|
||||
name = "db-mongo"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web.workspace = true
|
||||
mongodb = "2"
|
||||
mongodb = "3"
|
||||
serde.workspace = true
|
||||
|
@ -1,6 +1,6 @@
|
||||
# MongoDB
|
||||
|
||||
Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver, visit the [documentation](https://docs.rs/mongodb/2.0.0/mongodb/index.html) and [source code](https://github.com/mongodb/mongo-rust-driver).
|
||||
Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver, visit the [documentation](https://docs.rs/mongodb/3) and [source code](https://github.com/mongodb/mongo-rust-driver).
|
||||
|
||||
## Usage
|
||||
|
||||
|
@ -15,7 +15,7 @@ const COLL_NAME: &str = "users";
|
||||
#[post("/add_user")]
|
||||
async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpResponse {
|
||||
let collection = client.database(DB_NAME).collection(COLL_NAME);
|
||||
let result = collection.insert_one(form.into_inner(), None).await;
|
||||
let result = collection.insert_one(form.into_inner()).await;
|
||||
match result {
|
||||
Ok(_) => HttpResponse::Ok().body("user added"),
|
||||
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
|
||||
@ -27,10 +27,7 @@ async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpRespo
|
||||
async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse {
|
||||
let username = username.into_inner();
|
||||
let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME);
|
||||
match collection
|
||||
.find_one(doc! { "username": &username }, None)
|
||||
.await
|
||||
{
|
||||
match collection.find_one(doc! { "username": &username }).await {
|
||||
Ok(Some(user)) => HttpResponse::Ok().json(user),
|
||||
Ok(None) => {
|
||||
HttpResponse::NotFound().body(format!("No user found with username {username}"))
|
||||
@ -49,7 +46,7 @@ async fn create_username_index(client: &Client) {
|
||||
client
|
||||
.database(DB_NAME)
|
||||
.collection::<User>(COLL_NAME)
|
||||
.create_index(model, None)
|
||||
.create_index(model)
|
||||
.await
|
||||
.expect("creating an index should succeed");
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ async fn test() {
|
||||
client
|
||||
.database(DB_NAME)
|
||||
.collection::<User>(COLL_NAME)
|
||||
.drop(None)
|
||||
.drop()
|
||||
.await
|
||||
.expect("drop collection should succeed");
|
||||
|
||||
|
Reference in New Issue
Block a user