How to connect your NextJs app to MongoDB

Поділитися
Вставка
  • Опубліковано 26 гру 2024

КОМЕНТАРІ •

  • @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.

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

    love your camera set up

  • @poet9638
    @poet9638 2 роки тому +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  2 роки тому +1

      Welcome to NextJs, glad you're learning something.

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

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

  • @G3-fm9ik
    @G3-fm9ik 2 місяці тому +2

    Wonderful video, can you please put the code snippets at a place where we can see them in detail? Once again thank you!

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

    Intuitive content. Appreciate that 👍

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

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

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

      I guess you can call it directly as well 🙂

    • @EnnBlack
      @EnnBlack Рік тому +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  Рік тому +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.

  • @licokr
    @licokr Рік тому +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  Рік тому

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

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

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

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

    You saved me thanks, appreciate your hard work.

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

    9 minutes in but I was wondering , once you made that connect to DB clientPromise do you need to import it wherever you are doing db actions? is there no way to import it in one place and have it run only once? I have multiple next api routes so I was wondering if there is a way to not call for the connect to db function for each CRUD function.

    • @hamedbahram
      @hamedbahram  Місяць тому

      good question, we're sharing the connection and only recreating it if it doesn't exist. reference the docs for any update on mongo.

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

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

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

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

  • @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

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

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

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

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

  • @LePr3z
    @LePr3z 2 роки тому +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 роки тому +2

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

  • @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!

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

    awesome video! please upload the mongodb video soon. Thanks

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

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

  • @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 ;)

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

    Thank you hamid, great video

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

      My pleasure! welcome to the channel.

  • @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?

  • @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  Рік тому +2

      @@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

  • @yo1414
    @yo1414 2 роки тому +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  2 роки тому +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.

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

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

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

      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 Рік тому

      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 Рік тому +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  Рік тому

      @@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 Рік тому

      @@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.

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

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

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

      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.

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

    What font you used in this video??

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

    Can I connect nextjs to mongo compass locally

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

      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.

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

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

  • @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.

  • @h.fernandes
    @h.fernandes 2 роки тому +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  2 роки тому +3

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

    • @h.fernandes
      @h.fernandes 2 роки тому +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 роки тому +2

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

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

    you forgot to show env file
    🧐

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

      Sorry! It would be the MongoDB URL.

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

    thanks

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

    font size too much small.

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

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

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

    thanks 🫡🫡