Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
How can I implement a MongoDB schema along with business logic in Rust?

I'm new to Rust and a frequent user of MongoDB. I usually use node.js + Mongoose ODM.

Is there any MongoDB ODM in RUST that implements a custom schema and business logic hooks like Mongoose ODM ? Or do I need to implement it myself inside the Rust's type system?

This is an example from Mongoose

    const schema = kittySchema = new Schema(..);

    // they implement the 'meow' method in the Kitty Schema
    schema.method('meow', function () {
        console.log('meeeeeoooooooooooow');
    })
    
    // so anytime a Kitty schema instance is created
    const Kitty = mongoose.model('Kitty', schema);

    const fizz = new Kitty;
    
    // fizz as the instance of Kitty Schema automatically has meow() method
    fizz.meow(); // outputs: meeeeeooooooooooooow

As far as I know.. mongodm-rs doesn't do that nor Wither nor Avocado

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You may not need an ODM. With serde (which is used by all those ODMs), the "schema" is implemented on the struct itself, so you can add any methods you want with an impl block. It would look something like this:

    use serde::{Deserialize, Serialize};

    // Deriving from serde's Serialize and Deserialize, meaning this struct can be converted to and from BSON for storage and retrieval in MongoDB:
    #[derive(Serialize, Deserialize, Debug)]
    struct Kitty {
        name: String,
    }

    impl Kitty {
        fn meow(&self) {
            println!("{} says 'meeeeeoooooooooooow'", &self.name);
        }
    }

    let fizz = Kitty {
        name: String::from("Fizz"),
    };

    fizz.meow();

This struct can be stored in MongoDB (because it derives Serialize and Deserialize) - to see how, check out my blog post Up and Running with Rust and MongoDB. There's some more up-to-date details about how to use serde with MongoDB in my colleague Isabelle's post.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!