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...
man ur content is life saving , keep teaching us sensei
My pleasure Elouali! I'm glad you found it helpful.
مرسی بابت ویدیوت و تایمی ک گذاشتی خیلی ممنونت میشدیم کد هم میزاشتی واسمون مچکر
Eraadatmand! You can check out the code for this video → ua-cam.com/video/qAgwDGCrzgE/v-deo.html
first time, i join in nextjs, so i like the your lecture ~ it's very nice~ it's getting ability up to me
Welcome to NextJs, glad you're learning something.
You saved me thanks, appreciate your hard work.
Glad to hear that!
به ارور خورده بودم یهو دیدم لهجه تون فارسیه :))))) خیلی بامزه بود این اتفاق
Lahjaro nemishe karish kard 😅
Intuitive content. Appreciate that 👍
Thanks Milad!
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 !
Hey Flo! welcome to the channel. That's right, it should be 405 instead, thanks.
Thanks for the help! I like the way you teach.
Glad to hear that!
Awesome content bro. Your speaking is so awli 😅.
Damet garm 👌🏻.
Thank you 😁
مرسی سلطان عالی بود
Mokhlesim Mojtaba jan
Thank you hamid, great video
My pleasure! welcome to the channel.
awesome video! please upload the mongodb video soon. Thanks
Welcome to the channel Renzo. That's coming soon.
That part you cut just to speed up the process was very important :(
Which part is that?
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?
Good question! The pattern in development is to prevent hot module reload from creating multiple connections.
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?
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.
Thanks for this great video. What if we want to implement models and use them instead of "months" or populate fields?
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
@@hamedbahram thanks for your fast answer 🙌
@@Vittokm Anytime!
thanks
You're welcome!
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
Glad you found the videos helpful. Can you tell me more about the error?
Hi, thanks for the video, can you put out a video of the deployment? Much appreciated.
Sure, will have that in mind. What do you wish to see in deployment?
@@hamedbahram Thank you. I would like to see mongdb in the deployment and it would be nice to see how to use cdn.
@@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?
@@hamedbahram
@@hamedbahram Well, nothing to do with cdn. I'm just emmm messed up in the head and looking forward to your mongdb video
in 9:20, why do you need to wrap init() in an async arrow function, but not just call init() directly?
I guess you can call it directly as well 🙂
@@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!
@@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.
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?
Thanks, Henrique, and welcome to the channel. Interesting topic, I'll definitely have that in mind for future videos.
@@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
@@h.fernandes yes of course, I'll definitely look into it.
Thank you for the video! Do you have the code in Github available?
Unfortunately not! What parts do you need a reference for? maybe I can help here ;)
What font you used in this video??
I think its Operator Mono.
Noob question, but when do you close the database connection?
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.
Can I connect nextjs to mongo compass locally
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.
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");`.
The db name can be in your connection string (MONGO_URI) you set in your `.env`
@@hamedbahram How? where do you put it exactly?
@@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
you forgot to show env file
🧐
Sorry! It would be the MongoDB URL.
is storing a db connection on global safe for production!?
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.
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
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.
@@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.
@@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.
Hi There, why did you not use the api routes in the "App" folder?
Hey! the api routes (route handlers) in the `app` folder wasn't introduced at the time of recording.
font size too much small.
Thanks for the feedback, I've tried to make it bigger in later videos.
thanks 🫡🫡
My pleasure 🫡