use mongodb::bson::{doc, oid::ObjectId};
use oximod::{Model, OxiClient};
use serde::{Deserialize, Serialize};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv::dotenv().ok();
let mongodb_uri = std::env::var("MONGODB_URI")?;
OxiClient::init_global(mongodb_uri).await?;
#[derive(Debug, Serialize, Deserialize, Model)]
#[db("query_example_db")]
#[collection("users")]
struct User {
#[serde(skip_serializing_if = "Option::is_none")]
_id: Option<ObjectId>,
name: String,
age: i32,
#[default(true)]
active: bool,
}
User::clear().await?;
let users = vec![
User::new().name("Alice".to_string()).age(30).active(true),
User::new().name("Bob".to_string()).age(40).active(false),
User::new().name("Charlie".to_string()).age(25).active(true),
];
for user in &users {
let id = user.save().await?;
println!("📝 Inserted user {} with _id: {}", user.name, id);
}
let active_users = User::find(doc! { "active": true }).await?;
println!("\n✅ Active users:");
for user in active_users {
println!("- {} (age: {})", user.name, user.age);
}
if let Some(bob) = User::find_one(doc! { "name": "Bob" }).await? {
println!("\n🔍 Found Bob (age: {})", bob.age);
}
let exists = User::exists(doc! { "active": false }).await?;
println!("\n❓ Is there any inactive user? {}", exists);
Ok(())
}