REST API has become the De Facto for connecting and transferring data from one source to another. It offers a set of guidelines and architectural p...
For further actions, you may consider blocking this person and/or reporting abuse
Hi @malomz, I'm one of the maintainers of the
mongodb
crate. Thanks for writing up this tutorial!I wanted to point out that since you are writing an asynchronous application (as Actix is an async framework), it would be most appropriate to use the mongodb
async
API here rather than the sync API. With the sync API, every time you interact with the database you will be running blocking code on the current thread and preventing the async runtime (Tokio) from scheduling any other async tasks on the thread while waiting for a response from the database. The end result is that your application is more resource-intensive and less performant, since threads cannot be shared as efficiently.With the async API, the thread can be "given up" to another task while you
await
the response from the database.This is a nice blog post on this subject.
The async API is identical to the sync one besides everything being
async
; to adopt your example here I think you'd just need to mark the methods on yourMongoRepo
typeasync
and call the equivalent async mongodb API methods.You can find a simple example of using the async mongodb API with actix here.
Feel free to get in touch with us via GitHub if you have any questions or run into any issues.
Hi @kmahar ,
Thanks for pointing this out ๐๐.
I have updated accordingly๐
Awesome! One more thing to note is that since Actix uses the tokio runtime, I'd suggest using the driver with tokio as well, rather than async-std, so that you do not need to have two separate runtimes going that cannot coordinate with one another.
By default the mongodb crate uses tokio, so to do this, you can just remove these lines from your Cargo.toml:
There's some documentation on all of the drivers' feature flags available here.
i use this
[dependencies.mongodb]
and with async std it takes almost 4 sec to create a use but when i use this tokio it takes around 600 ms thanksversion = "2.8.2"
default_features = false
features = ["tokio-runtime"]
I expect to see more people start to use rust + postgres + an ORM, when they want something with a richer type system or more functional idioms than go, python, or node/ts. Do you think that emerge and possibly reach fourth place over ruby for web dev?
Well, rust is relatively new to the ecosystem as compared to others. Hopefully, it becomes mainstream! :)
hi there, just wanna collection ppl opinion but could I ask why you choose Postgres over Mongodb when stack with Rust or actix-web for more specific? TIA
As I understand, postgres will be easier to implement a wide variety of queries, while mongo will help keep inserts fast, so having more flexibility with queries may be a good default.
Thanks!
Good article, thank you.
Thank you for this tutorial
Thanks for putting the effort to write this amazing post definitely will try Actix Web soon ๐
Glad you enjoyed it.
Yea, you should give it a try!
nice post thanks @malomz for the nice guide and thanks @kmahar for feedback.
AWESOME!! Thanks for this ammount of knowledge shared. I really appreciate it!
Glad you enjoyed it!
Thanks for your article. I have a question. Suppose after creating the client if you want to store it in global variables for further use how I can do that?
How would you model the MongoRepo struct if you have multiple collections?
Check out this article
dev.to/hackmamba/create-a-graphql-...
I did something similar here