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

КОМЕНТАРІ • 29

  • @thiagotoazza
    @thiagotoazza 4 місяці тому

    Amazing tutorial, thanks!

  • @IamTellingStories
    @IamTellingStories 2 роки тому +2

    best vid i've ever seen

  • @eliasshemsu5853
    @eliasshemsu5853 4 місяці тому

    Thanks a lot for making this available for free.

  • @youssefhachicha-nj6wf
    @youssefhachicha-nj6wf Рік тому

    very helpfull thanks

  • @commenter-cl4gl
    @commenter-cl4gl 2 роки тому

    Youre awesome man

  • @Hrit
    @Hrit Рік тому +2

    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!

    • @icodethis
      @icodethis  Рік тому +2

      I have no such plans at the moment. Maybe if I start live streams in the future.

  • @mohamadborna3678
    @mohamadborna3678 2 роки тому

    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?

  • @sarankumaar6009
    @sarankumaar6009 2 роки тому

    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.

    • @icodethis
      @icodethis  2 роки тому +1

      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

    • @sarankumaar6009
      @sarankumaar6009 2 роки тому

      @@icodethis thanks :)

  • @best_iyke
    @best_iyke 2 місяці тому

    Thanks for the tutorial. It is very helpful.
    Can ktor server run locally on mobile device?

    • @icodethis
      @icodethis  2 місяці тому +1

      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.

    • @best_iyke
      @best_iyke 2 місяці тому

      @@icodethis thanks for the reply

  • @amuamu1903
    @amuamu1903 2 роки тому

    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.

    • @icodethis
      @icodethis  2 роки тому +1

      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")

    • @amuamu1903
      @amuamu1903 2 роки тому +1

      @@icodethis thanks

  • @amuamu1903
    @amuamu1903 2 роки тому

    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.

    • @icodethis
      @icodethis  2 роки тому +1

      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/

    • @amuamu1903
      @amuamu1903 2 роки тому

      Thank you so much. Can u suggest some documentation where i can find examples of mangoose query with ktor.

    • @icodethis
      @icodethis  2 роки тому

      You are welcome. I think kmongo doucmentation is sufficent enough for queries.
      litote.org/kmongo/

    • @amuamu1903
      @amuamu1903 2 роки тому

      @@icodethis yeah i going through document now thanks a lot :)

  • @khalidbinhida
    @khalidbinhida 11 місяців тому

    Why did you not close the mongoDB connection?

    • @icodethis
      @icodethis  11 місяців тому

      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.

    • @khalidbinhida
      @khalidbinhida 11 місяців тому

      @@icodethis thank you. Now I understood it

  • @commenter-cl4gl
    @commenter-cl4gl 2 роки тому

    Sir, can u make tutorial reser password by email on ktor+mongoDB

    • @icodethis
      @icodethis  2 роки тому

      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.

    • @commenter-cl4gl
      @commenter-cl4gl 2 роки тому

      @@icodethis can i get ur contact/email sir for asking?