How to create Kotlin Ktor RestAPI with MongoDB?
Вставка
- Опубліковано 23 лип 2024
- Ktor Tutorial
Table of contents:
0:00 Introduction
1:40 Why We Should Use Ktor?
2:12 What can Ktor Do?
2:27 Client - API - Database Relationship
3:43 SQL vs NO-SQL
5:28 Ktor API coding part.
Final project link:
github.com/furkanturkn/ktor-s...
Dependencies:
github.com/furkanturkn/ktor-s...
programs used: Intellij IDEA, Postman, MongoDB Compass
Additional information about mongodb queries: litote.org/kmongo/
Contact information:
furkan@furkanturkan.com.tr
/ furkanturkan
github.com/furkanturkn
/ furkantrkn
Amazing tutorial, thanks!
best vid i've ever seen
Thanks a lot for making this available for free.
very helpfull thanks
Youre awesome man
Could you please make a tutorial on complete project with ktor like Notes App, Simple E-commerce or Hotel Management app ?
This would be very helpful!
I have no such plans at the moment. Maybe if I start live streams in the future.
Tnx for the good effort u put for makin this video,but can u pls mention the doc u've learned the mongodb stuff in the this video here?
Already found it,tnx ❤
hi furkan thanks for the video it helped me a lot. i have one doubt in my project i am trying to create a ecommerce app were i have cart in that a user can have multiple product in his cart. so i will store list of product object from product table in users cart attribute. how can i create model with a attribute named cart and list of product objects has values in model file(like name:String). can you please help me with this.
Thank you for your feedback. To answer your question, you can do it like this;
Create a new Collection for example Cart collection. In this collection hold documents like this
{
"userID": "001",
"cartItems": {
[
"prodID": "001", "prodDesc": "xxxxx",
"prodID": "005", "prodDesc": "xxxxx"
]
}
So when you add a new product to cart, you should update list for specific userID.
Also in this way you can get all products on cart for specific user.
Btw you can use List for document field for example
userID: String,
cardItems: List
also you can hold whole object like this
user: User,
cardItems: List
@@icodethis thanks :)
Thanks for the tutorial. It is very helpful.
Can ktor server run locally on mobile device?
Thank you. Yes, you can run but what is your purpose? Running a server directly on a mobile device might not be the best approach.
@@icodethis thanks for the reply
Hi how are you connecting the server with mongodb. Usually we use connection string to connect. But iam confused here how ur connecting in ktor since iam new for ktor can u please help me.
Hi, in this code ->> KMongo.createClient() we are creating client with default connection string which is ""mongodb://localhost"
if you want to change connection string, you can send it as a parameter like this KMongo.createClient("your_connection_string")
@@icodethis thanks
Hi how to use findoneandupdate to update a array of values using id of the employees. Can give me and example code please? {_id:1, project:[1,2,3,4]} i want to remove and add elements to the project array.
Hi,
I assume that you have this:
{
_id: 1,
"projects": [1,2,3,4]
}
first, get all projects with specific employee ID.
employees.findOneById(employeeID).projects
This will give you current projets that employeeID has.
Then you should add new projects like this
--> employees.updateOneById(employeeID, setValue(Employee::projects, projects + project)
For additional information about mongo queries: litote.org/kmongo/
Thank you so much. Can u suggest some documentation where i can find examples of mangoose query with ktor.
You are welcome. I think kmongo doucmentation is sufficent enough for queries.
litote.org/kmongo/
@@icodethis yeah i going through document now thanks a lot :)
Why did you not close the mongoDB connection?
Under the hood, when you use the .coroutine extension with KMongo (11:15), it configures the MongoDB client to work with Kotlin coroutines and enables connection pooling by default. Connection pooling helps manage the lifecycle of database connections efficiently, allowing the application to reuse existing connections instead of creating new ones for every operation.
So, by using .coroutine, you get the benefits of both coroutine support and connection pooling in KMongo. The connection pooling aspect is handled automatically, and you don't need to explicitly manage individual connections.
@@icodethis thank you. Now I understood it
Sir, can u make tutorial reser password by email on ktor+mongoDB
This topic is not among the videos I have planned recently. But I can give you a suggestion on how to do it.
You should research SMTP. You can send mail with this protocol.
A simple scenario:
1- User requests with forget password button.
2- With SMTP you can send unique code, token etc.
3- When user got the code, user should enter this code. If code is correct, then let user enter new password. Then set new password for this user in your database.
@@icodethis can i get ur contact/email sir for asking?