Rust: 读取 MongoDB 的数据 (读取)
我将读取在这里创建的数据。
Rust: 创建 MongoDB 数据
[package]
name = "mongodb_read"
version = "0.1.0"
edition = "2018"
[dependencies]
mongodb = "1.0.0"
tokio = "*"
futures = "*"
// --------------------------------------------------------------------
/*
mongodb_read/src/main.rs
Jul/25/2020
*/
// --------------------------------------------------------------------
use futures::stream::StreamExt;
use mongodb::{
bson::{doc},
Client,
error::Result,
};
#[tokio::main]
async fn main()-> Result<()> {
let client =
Client::with_uri_str("mongodb://localhost:27017").await?;
let database = client.database("city");
let collection = database.collection("saitama");
let mut cursor = collection.find(doc! {}, None).await?;
while let Some(result) = cursor.next().await {
println!("{:?}",result);
}
eprintln! ("*** 終了 ***");
Ok(())
}
// --------------------------------------------------------------------
执行 (shí
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/mongodb_read`
Ok(Document({"_id": ObjectId(ObjectId(5f1bf38b00c8509a003a3a47)), "key": String("t1161"), "name": String("さいたま"), "population": Int32(45298), "date_mod": String("1956-6-12")}))
Ok(Document({"_id": ObjectId(ObjectId(5f1bf38b0077dbf3003a3a48)), "key": String("t1162"), "name": String("所沢"), "population": Int32(21983), "date_mod": String("1956-8-24")}))
Ok(Document({"_id": ObjectId(ObjectId(5f1bf38b0054d9c9003a3a49)), "key": String("t1163"), "name": String("越谷"), "population": Int32(51287), "date_mod": String("1956-3-9")}))
*** 終了 ***