How to connect your NextJs app to MongoDB

Поділитися
Вставка
  • Опубліковано 8 вер 2024
  • Learn how to connect your NextJs app to MongoDB without exhausting your connection pool and organize your database query functions.
    ➡️ The Ultimate NextJs Course
    🔗 www.hamedbahra...

КОМЕНТАРІ • 82

  • @eloualielouali4949
    @eloualielouali4949 Рік тому +3

    man ur content is life saving , keep teaching us sensei

    • @hamedbahram
      @hamedbahram  Рік тому +1

      My pleasure Elouali! I'm glad you found it helpful.

  • @user-jo8gq9nj2q
    @user-jo8gq9nj2q 9 місяців тому +1

    مرسی بابت ویدیوت و تایمی ک گذاشتی خیلی ممنونت میشدیم کد هم میزاشتی واسمون مچکر

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Eraadatmand! You can check out the code for this video → ua-cam.com/video/qAgwDGCrzgE/v-deo.html

  • @poet9638
    @poet9638 Рік тому +1

    first time, i join in nextjs, so i like the your lecture ~ it's very nice~ it's getting ability up to me

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Welcome to NextJs, glad you're learning something.

  • @parijatbest
    @parijatbest 3 місяці тому

    You saved me thanks, appreciate your hard work.

  • @meowmeowhatam
    @meowmeowhatam 10 місяців тому +1

    به ارور خورده بودم یهو دیدم لهجه تون فارسیه :))))) خیلی بامزه بود این اتفاق

    • @hamedbahram
      @hamedbahram  10 місяців тому

      Lahjaro nemishe karish kard 😅

  • @milad8436
    @milad8436 Рік тому +1

    Intuitive content. Appreciate that 👍

  • @LePr3z
    @LePr3z Рік тому +3

    Hey ! Thanks for your videos on nextjs 13 ! I really appreciate it !
    There is mistake in the api folder. You're returning a 425 status code. I think it's a 405 status code instead.
    Keep going, cheers from France !

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

      Hey Flo! welcome to the channel. That's right, it should be 405 instead, thanks.

  • @alberteinstein7231
    @alberteinstein7231 Рік тому

    Thanks for the help! I like the way you teach.

  • @alirezaghadyani16
    @alirezaghadyani16 Рік тому +1

    Awesome content bro. Your speaking is so awli 😅.
    Damet garm 👌🏻.

  • @mojtabayam
    @mojtabayam Рік тому

    مرسی سلطان عالی بود

  • @programmingholic
    @programmingholic Рік тому

    Thank you hamid, great video

    • @hamedbahram
      @hamedbahram  Рік тому

      My pleasure! welcome to the channel.

  • @Renzo-of3yn
    @Renzo-of3yn Рік тому

    awesome video! please upload the mongodb video soon. Thanks

    • @hamedbahram
      @hamedbahram  Рік тому

      Welcome to the channel Renzo. That's coming soon.

  • @JohnsonREll
    @JohnsonREll 5 місяців тому +2

    That part you cut just to speed up the process was very important :(

  • @licokr
    @licokr 10 місяців тому +1

    Thank you for the video. 6:55 singletoe pattern is used to share the existing instance of connection for better performance but why is that not used in production then?

    • @hamedbahram
      @hamedbahram  10 місяців тому

      Good question! The pattern in development is to prevent hot module reload from creating multiple connections.

  • @yo1414
    @yo1414 Рік тому +1

    Another awesome next13 video!
    Question what if I wanted to do a grid display and wrap each movie edit / delete button? For example,
    Robin Hood
    Go West
    JAWS
    It is my understanding you would need to add a "use client" component to add the and functionality?

    • @hamedbahram
      @hamedbahram  Рік тому +1

      That's right, to add any interactivity to your components, you need react hooks which are only available in client components denoted with the 'use client' directive.

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

    Thanks for this great video. What if we want to implement models and use them instead of "months" or populate fields?

    • @hamedbahram
      @hamedbahram  Рік тому +1

      You're welcome. You can use Prisma, which is a type-safe ORM, you can use this quick start guide to set it up: www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/mongodb-typescript-mongodb

    • @Vittokm
      @Vittokm Рік тому +1

      @@hamedbahram thanks for your fast answer 🙌

    • @hamedbahram
      @hamedbahram  Рік тому

      @@Vittokm Anytime!

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

    thanks

  • @juunis5854
    @juunis5854 Рік тому +3

    Hi, I just found this video while starting to work with nextjs and mongodb together. I've put everything according to the video including the MONGODB_URI string, and all the code is the same down to the letter, but I keep getting a certificate validation failed error no matter what I do. I'm working with nextjs 13. Do you perhaps know why that would be? Thanks! love your videos btw super easy to follow and clear :D

    • @hamedbahram
      @hamedbahram  Рік тому

      Glad you found the videos helpful. Can you tell me more about the error?

  • @bibooo7507
    @bibooo7507 Рік тому

    Hi, thanks for the video, can you put out a video of the deployment? Much appreciated.

    • @hamedbahram
      @hamedbahram  Рік тому

      Sure, will have that in mind. What do you wish to see in deployment?

    • @bibooo7507
      @bibooo7507 Рік тому +1

      @@hamedbahram Thank you. I would like to see mongdb in the deployment and it would be nice to see how to use cdn.

    • @hamedbahram
      @hamedbahram  Рік тому

      @@bibooo7507 I use mongodb atlas which is hosted service, but I'll have that in mind for a future video. Can you expand on what you mean by using cdn?

    • @bibooo7507
      @bibooo7507 Рік тому

      ​@@hamedbahram

    • @bibooo7507
      @bibooo7507 Рік тому

      @@hamedbahram Well, nothing to do with cdn. I'm just emmm messed up in the head and looking forward to your mongdb video

  • @EnnBlack
    @EnnBlack 11 місяців тому +1

    in 9:20, why do you need to wrap init() in an async arrow function, but not just call init() directly?

    • @hamedbahram
      @hamedbahram  11 місяців тому +1

      I guess you can call it directly as well 🙂

    • @EnnBlack
      @EnnBlack 11 місяців тому +1

      @@hamedbahram Thanks for the reply:). I just started learning, so I was wondering if the two codes are doing things differently.
      Also thank you for the video. It's super helpful!

    • @hamedbahram
      @hamedbahram  11 місяців тому +1

      @@EnnBlack the only difference is that I'm awaiting the init function, whereas if you just call it directly, you can't await it. Other than that it'd be the same.

  • @h.fernandes
    @h.fernandes Рік тому +1

    Awesome content. I have a request to you if possible. Can you do a tutorial using the new NextJS 13 with i18n like next-translate or something similar?

    • @hamedbahram
      @hamedbahram  Рік тому +3

      Thanks, Henrique, and welcome to the channel. Interesting topic, I'll definitely have that in mind for future videos.

    • @h.fernandes
      @h.fernandes Рік тому +3

      @@hamedbahram I'm asking that, because its a thing all need (non native english persons) and with NextJS13 with App folder is kinda weird to implement

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

      @@h.fernandes yes of course, I'll definitely look into it.

  • @patite3103
    @patite3103 Рік тому +1

    Thank you for the video! Do you have the code in Github available?

    • @hamedbahram
      @hamedbahram  Рік тому

      Unfortunately not! What parts do you need a reference for? maybe I can help here ;)

  • @user-eo5qi4wy7i
    @user-eo5qi4wy7i Рік тому +2

    What font you used in this video??

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

    Noob question, but when do you close the database connection?

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

      That's a good question! That's one of the problems with serverless functions, you can close it once you're done processing the request, or keep it alive to reduce the response time, which'll then exhausts your connection pool. You can use Prisma connection pooling to avoid that problem or query your db over HTTP.

  • @user-wp5iw1zh1q
    @user-wp5iw1zh1q 8 місяців тому +1

    Can I connect nextjs to mongo compass locally

    • @hamedbahram
      @hamedbahram  8 місяців тому

      You can connect NextJs to a mongo server running on your local machine. MongoDB compass however is just an interface (GUI) to interact with your DBs.

  • @junkim9450
    @junkim9450 Рік тому +1

    How did you fetch the data not specifying the db name? It didn't work for me until I specified the db name `db = await client.db("sample_mflix");`.

    • @hamedbahram
      @hamedbahram  Рік тому

      The db name can be in your connection string (MONGO_URI) you set in your `.env`

    • @spencersnygg6966
      @spencersnygg6966 Рік тому

      @@hamedbahram How? where do you put it exactly?

    • @hamedbahram
      @hamedbahram  Рік тому +1

      @@spencersnygg6966 It would go at the end of your connection string, where I put DATABASE in the example below:
      mongodb+srv://:@your.cluster.mongodb.net/DATABASE?retryWrites=true&w=majority

  • @sanalms1601
    @sanalms1601 6 місяців тому +1

    you forgot to show env file
    🧐

    • @hamedbahram
      @hamedbahram  6 місяців тому

      Sorry! It would be the MongoDB URL.

  • @wizardlabs1422
    @wizardlabs1422 11 місяців тому +1

    is storing a db connection on global safe for production!?

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

      That's a good question! This is the recommended way from the official docs. I'm not sure if and how it'd be unsafe to set a global.

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

      After further research I believe this is the correct method. Storing on global has other implications like potential memory leaks or namespace colliding but in this case you handle both of those well. Using global is fine for this case, thanks.@@hamedbahram

    • @TheProOfficial
      @TheProOfficial 11 місяців тому +1

      It is not safe for production, global is meant to be used only for development, I am currently looking for a safe and efficient way to handle production myself.

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

      @@TheProOfficial Yes global is meant to prevent recreating the conenciton during Hot Module Reload (HMR) in development. Please do share if you found a soloution for connection pooling in produdction.

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

      @@TheProOfficial Clarification here... I am storing the connection in global on my nextJS server side. So client never sees this. This should be safe for production.

  • @margosdesarian
    @margosdesarian Рік тому

    Hi There, why did you not use the api routes in the "App" folder?

    • @hamedbahram
      @hamedbahram  Рік тому

      Hey! the api routes (route handlers) in the `app` folder wasn't introduced at the time of recording.

  • @jahirrana8879
    @jahirrana8879 Рік тому +1

    font size too much small.

    • @hamedbahram
      @hamedbahram  Рік тому

      Thanks for the feedback, I've tried to make it bigger in later videos.

  • @user-yn9yr8hq8v
    @user-yn9yr8hq8v 10 місяців тому +1

    thanks 🫡🫡