Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda