-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rs
57 lines (50 loc) · 1.39 KB
/
model.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use mongodb::bson::{doc, Bson};
use mongodb::{Client, Collection};
// Relate the Address by passing the json data.
// #[derive(Debug, Serialize, Deserialize)]
// struct Address {
// city: String,
// country: String,
// }
// #[derive(Debug, Serialize, Deserialize)]
// struct User {
// name: String,
// age: i32,
// address: Address,
// }
// // Example of inserting a user with an embedded address
// async fn insert_user(users: Collection<User>) -> mongodb::error::Result<()> {
// let address = Address {
// city: "New York".to_string(),
// country: "USA".to_string(),
// };
// let user = User {
// name: "John Doe".to_string(),
// age: 30,
// address: address,
// };
// users.insert_one(user, None).await?;
// Ok(())
// }
// Reference them by ID
#[derive(Debug, Serialize, Deserialize)]
struct Address {
city: String,
country: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct User {
name: String,
age: i32,
address_id: ObjectId, // Reference to an Address document
}
// Example of inserting a user with a reference to an address
async fn insert_user(users: Collection<User>, address_id: ObjectId) -> mongodb::error::Result<()> {
let user = User {
name: "John Doe".to_string(),
age: 30,
address_id: address_id,
};
users.insert_one(user, None).await?;
Ok(())
}