Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rustは厳しいが役に立つ Part3「Rustでデータべース(MongoDB編)」
Search
NearMeの技術発表資料です
PRO
September 30, 2022
Programming
0
630
Rustは厳しいが役に立つ Part3「Rustでデータべース(MongoDB編)」
NearMeの技術発表資料です
PRO
September 30, 2022
Tweet
Share
More Decks by NearMeの技術発表資料です
See All by NearMeの技術発表資料です
LlamaIndex Workflow: Build Practical AI Agents Fast
nearme_tech
PRO
0
6
Box-Muller法
nearme_tech
PRO
1
18
Kiro触ってみた
nearme_tech
PRO
0
58
今だからこそ入門する Server-Sent Events (SSE)
nearme_tech
PRO
4
400
ReactNative のアップグレード作業が (意外に)楽しかった話
nearme_tech
PRO
2
98
強化学習アルゴリズムPPOの改善案を考えてみた
nearme_tech
PRO
0
39
Apple Containerについて調べて触ってみた
nearme_tech
PRO
0
480
Rust 並列強化学習
nearme_tech
PRO
0
33
並列で⽣成AIにコーディングをやらせる
nearme_tech
PRO
1
270
Other Decks in Programming
See All in Programming
マンガアプリViewerの大画面対応を考える
kk__777
0
440
Towards Transactional Buffering of CDC Events @ Flink Forward 2025 Barcelona Spain
hpgrahsl
0
120
Vue 3.6 時代のリアクティビティ最前線 〜Vapor/alien-signals の実践とパフォーマンス最適化〜
hiranuma
2
340
Webサーバーサイド言語としてのRustについて
kouyuume
1
5k
Node-REDのノードの開発・活用事例とコミュニティとの関わり(Node-RED Con Nagoya 2025)
404background
0
110
Amazon Verified Permissions実践入門 〜Cedar活用とAppSync導入事例/Practical Introduction to Amazon Verified Permissions
fossamagna
2
110
エンジニアに事業やプロダクトを理解してもらうためにやってること
murabayashi
0
100
alien-signals と自作 OSS で実現する フレームワーク非依存な ロジック共通化の探求 / Exploring Framework-Agnostic Logic Sharing with alien-signals and Custom OSS
aoseyuu
3
5.1k
Register is more than clipboard
satorunooshie
1
190
React Nativeならぬ"Vue Native"が実現するかも?_新世代マルチプラットフォーム開発フレームワークのLynxとLynxのVue.js対応を追ってみよう_Vue Lynx
yut0naga1_fa
2
1.9k
エンジニアインターン「Treasure」とHonoの2年、そして未来へ / Our Journey with Hono Two Years at Treasure and Beyond
carta_engineering
0
470
pnpm に provenance のダウングレード を検出する PR を出してみた
ryo_manba
1
170
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Writing Fast Ruby
sferik
630
62k
Why You Should Never Use an ORM
jnunemaker
PRO
60
9.6k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Rails Girls Zürich Keynote
gr2m
95
14k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
Scaling GitHub
holman
463
140k
How to Think Like a Performance Engineer
csswizardry
27
2.2k
Transcript
0 Rustは厳しいが役に立つ Part3「Rustでデータべース(MongoDB編)」 2022-09-30 第15回NearMe技術勉強会 Kaito Asahi
1 目次 RustとMongoDB MongoDBについて ハンズオン!! 実際に簡単なアプリケーションをつくります
2 MongoDBとは? • Build faster. Build smarter. → 迅速そして賢いデータベースの構築 →
シャーディング、インメモリでの処理、レプリカセット • JSONライクにデータを扱っている • さまざまなユースケースがある(https://www.mongodb.com/ja-jp/use-cases) • データベース言語「SQL」を使えない ※次回以降で、MySQLについても扱います。 出典:https://www.mongodb.com/
3 ハンズオンのために 以下の作業をおこなってください。 (1) 必須 • Rust環境の構築(https://www.rust-lang.org/ja/tools/install) • MongoDBのインストール (https://www.mongodb.com/docs/manual/installation/)
(2) 任意 • Mongo Compassのインストール(GUIでデータベースを確認できる) (https://www.mongodb.com/try/download/compass)
4 データのインサート (0) 準備 src/bin/insert.rsを作成し、以下を追加してください。 use mongodb::{ bson::{doc, oid::ObjectId}, options::ClientOptions,
Client, }; use serde::{Deserialize, Serialize};
5 データのインサート (1) データベースクライアントを作成する main関数内に、以下の2つの変数を宣言する。 ※parseメソッドの引数は、一応各デバイスで確認してください。 ※基本のport番号は27017です。 let client_options =
ClientOptions::parse("mongodb://localhost:27017").await?; let client = Client::with_options(client_options)?;
6 データのインサート (2) データベースを作成し、コレクションを作成 main関数内に、以下の2つの変数を追加する。 let db = client.database("test_pokemon"); let
collection = db.collection::<Pokemon>("pokemons");
7 データのインサート (3) データの準備 3-1. main関数の外にPokemon構造体を作成する #[derive(Serialize, Deserialize)] struct Pokemon
{ #[serde(rename = "_id", skip_serializing)] id: Option<ObjectId>, name: String, where_from: String, }
8 データのインサート (3) データの準備 3-2. main関数内にポケモンのデータのサンプルを追加する。 let pokemons = vec![
Pokemon { id: None, name: "Bulbasaur".to_string(), where_from: "Kanto".to_string(), }, … ];
9 データのインサート (4) pokemonsドキュメントにデータを挿入する main関数内に、以下を追加する。 ※Ok(())は、main関数でResultを返すために置いてあります。 collection.insert_many(pokemons, None).await?; Ok(())
10 データのインサート (5) Mongo Compassで確認
11 データの検索 (0) 準備 src/bin/search.rsを作成し、以下を追加してください。 use futures::stream::TryStreamExt; use mongodb::{ bson::{doc,
oid::ObjectId}, options::{ClientOptions, FindOptions}, Client, }; use serde::{Deserialize, Serialize};
12 データの検索 (1) フィルターを作成する 先程のcollection変数の部分までは同じで、以下のようにフィルターを作成。 → ここでは、Venusaur(フシギバナ)を検索しています。 → sortは、”where_from”で逆順(-1)にしている。 let
filter = doc! {"name": "Venusaur"}; let find_options = FindOptions::builder().sort(doc! {"where_from": -1}).build();
13 データの検索 (2) フィルターをつけて検索する 先程のcollection変数の部分までは同じで、以下のようにフィルターを作成する。 ※awaitを用いて、検索が終了するまで、後に現れる検索結果の表示を行わないよ うにしています。 let mut cursor
= collection.find(filter, find_options).await?;
14 データの検索 (3) 検索結果の表示 検索して、データがあればそれを報告する。 while let Some(pokemon) = cursor.try_next().await?
{ if let name = pokemon.name { println!("name: {}, where from: {}", name, pokemon.where_from); } }
15 (おまけ)Pokeapiを用いて、全てのポケモンを取得しました 別途結果をお見せいたします。
16 参考リンクまとめ 1. MongoDB:https://www.mongodb.com/ 2. MongoDB Compass:https://www.mongodb.com/try/download/compass 3. Rust:https://www.rust-lang.org/ja/tools/install 4.
Rustでのデータベース作成について ・https://zenn.dev/kyoheiu/articles/c3b6b6f156e57a ・https://zenn.dev/tfutada/articles/e5bf173edd541b 5. Pokeapi:https://pokeapi.co/ 6. Rustでapiを叩く:https://qiita.com/odayushin/items/0e2a5a3d047e6b08c811
17 余談 Rustに関する本で、Webアプリに特化したものが出るらしいので、 速攻購入してブラッシュアップしていきたいです... https://www.shuwasystem.co.jp/book/9784798067315.html
18 Thank you