Created code to upload packets to a mongo instance. Pretty boring but just practising how to use the mongo driver
This commit is contained in:
parent
898613a56a
commit
ce6e443c41
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "rustic_mongo"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
mongodb = "2.5.0"
|
||||||
|
serde = "1.0.163"
|
||||||
|
tokio = "1.28.1"
|
35
src/main.rs
Normal file
35
src/main.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use core::panic;
|
||||||
|
use mongodb::{Client, options::{ClientOptions, Credential}};
|
||||||
|
mod packet;
|
||||||
|
use packet::Packet;
|
||||||
|
use std::time;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
// Parse a connection string into an options struct.
|
||||||
|
let mut client_options = ClientOptions::parse("mongodb://localhost:27017").await.unwrap();
|
||||||
|
|
||||||
|
// Manually set an option.
|
||||||
|
client_options.app_name = Some("My App".to_string());
|
||||||
|
client_options.credential = Some(Credential::default());
|
||||||
|
client_options.credential.as_mut().unwrap().username = Some(std::env::var("mongo_username").unwrap());
|
||||||
|
client_options.credential.as_mut().unwrap().password = Some(std::env::var("mongo_password").unwrap());
|
||||||
|
|
||||||
|
// Get a handle to the deployment.
|
||||||
|
let client = Client::with_options(client_options).unwrap();
|
||||||
|
|
||||||
|
let db = client.database("test");
|
||||||
|
|
||||||
|
match db.create_collection("test", None).await {
|
||||||
|
Err(_) => panic!("ERROR: incorrect usage"),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
let collection = db.collection::<Packet>("test");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
collection.insert_one(Packet{
|
||||||
|
time: time::SystemTime::now(),
|
||||||
|
text: format!("Current Unix time: {:?}", time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap())
|
||||||
|
}, None).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
7
src/packet.rs
Normal file
7
src/packet.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Packet {
|
||||||
|
pub time: std::time::SystemTime,
|
||||||
|
pub text: String
|
||||||
|
}
|
19
src/test.json
Normal file
19
src/test.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"first_name": "Tom",
|
||||||
|
"last_name": "Cruise",
|
||||||
|
"photo": "https://jsonformatter.org/img/tom-cruise.jpg"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"first_name": "Maria",
|
||||||
|
"last_name": "Sharapova",
|
||||||
|
"photo": "https://jsonformatter.org/img/Maria-Sharapova.jpg
|
||||||
|
"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"first_name": "Robert",
|
||||||
|
"last_name": "Downey Jr",
|
||||||
|
"photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user