Rust:删除MongoDB的数据
[package]
name = "mongodb_delete"
version = "0.1.0"
edition = "2018"
[dependencies]
mongodb = "1.0.0"
tokio = "*"
// --------------------------------------------------------------------
/*
mongodb_delete/src/main.rs
Jul/26/2020
*/
// --------------------------------------------------------------------
use std::env;
use mongodb::{
bson::doc,
error::Result,
Client
};
#[tokio::main]
async fn main() -> Result<()> {
eprintln! ("*** 開始 ***");
let args: Vec<_> = env::args().collect();
let key_in = &args[1];
eprintln!("{}",key_in);
let client =
Client::with_uri_str("mongodb://localhost:27017").await?;
let coll = client
.database("city")
.collection("saitama");
let delete_result = coll.delete_many(
doc! {"key": key_in}, None,
).await?;
println!("Deleted {} documents", delete_result.deleted_count);
eprintln! ("*** 終了 ***");
Ok(())
}
// --------------------------------------------------------------------
实施
$ cargo run t1162
Finished dev [unoptimized + debuginfo] target(s) in 0.29s
Running `target/debug/mongodb_delete t1162`
*** 開始 ***
t1162
Deleted 1 documents
*** 終了 ***
快速入门:使用Rust和MongoDB启动和运行的参考页面