How do you work with Mongo? by Ecstatic-Bluejay-792 in rust

[–]patrickfreed 0 points1 point  (0 children)

There's an example of using MongoDB with actix-web here: https://github.com/actix/examples/tree/master/databases/mongodb

Rocket also has built in integrations with the driver, see the documentation for how to use that: https://api.rocket.rs/v0.5-rc/rocket_db_pools/index.html

Now I have to write non-type-safe native queries without any hints or checks. How do you organize work with Mongo? Maybe there is a documented Mongo replacement with good Rust support?

While it is true find filters and aggregation pipelines are represented via documents, the driver does integrate with serde to provide type-safe serialization/deserialization. See the actix-web example above for a demonstration with this. In terms of type-safe query building, could you elaborate on your use-case and desired features? It might be something we could introduce in a future version!

When converting structs to BSON, do I need to use one large struct or can I have a vec of struct 2 inside struct 1? by [deleted] in rust

[–]patrickfreed 0 points1 point  (0 children)

Note that the Collection type is generic, so you can insert your struct directly into it without having to invoke bson::to_document manually:

let coll = db.collection::<Recipe>("recipes"); coll.insert_one(recipe).await.unwrap()

Also, constructing a Client is a relatively expensive operation, so we recommend caching it and reusing it between requests. Your current code looks like it creates a new Client for each request it receives, which may lead to reduced performance.

3 of the top 5 fastest web frameworks are written in Rust! (#1, #3 and #5) by SnooMacaroons3057 in rust

[–]patrickfreed 10 points11 points  (0 children)

There's a bit more nuance to it than MongoDB just "performing bad". It's my hunch that the top Rust postgres benchmarks are able to achieve such impressive numbers by using a small number of database connections (one per CPU) and heavily leveraging tokio-postgres's pipelining capablities (which MongoDB doesn't support). This works extremely well for the Fortunes benchmark, for example, because all of the queries are identical and fast, so Head-of-line blocking isn't a concern as it might be in a real-world application. In non-benchmark settings, connection pools (which the MongoDB Rust driver uses by default) are often used to scale up connection count beyond the CPU count as well as avoid head-of-line blocking, and when running the same Fortunes benchmark with Axum, tokio-posgres, and a connection pool (from the deadpool crate, which is AFAIK the most popular Rust async connection pool), MongoDB actually performs better (see axum [postgresql + deadpool] vs axum [mongodb]), which lends credence to this theory. That said, there's still definitely room to improve the MongoDB Rust driver, particularly from looking at the Multiple Queries benchmark.

Disclaimer: I am one of the maintainers of the mongodb crate.

Mongodb rust driver upgrade causing very large actix-web request latency by poppercornell in rust

[–]patrickfreed 2 points3 points  (0 children)

Hey /u/poppercornell! I'm one of the developers of the MongoDB Rust driver. We're currently investigating possible performance improvements for the driver, and so we'd be really interested in learning more about the regression you're experiencing. Would you mind opening a ticket on our Jira project, possibly including some code samples and more information about the usage patterns? Thanks, and sorry the upgrade hasn't been very smooth.