0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rust: MongoDB のデータを削除 (Delete)

Posted at
Cargo.toml
[package]
name = "mongodb_delete"
version = "0.1.0"
edition = "2018"


[dependencies]
mongodb = "1.0.0"
tokio = "*"
src/main.rs
// --------------------------------------------------------------------
/*
	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
*** 終了 ***

参考ページ
Quick Start: Up and Running with Rust and MongoDB

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?