
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
Complete source code available till this point of lesson is available at
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, the differences between SQL and NoSQL databases, specifically MongoDB and Postgres, are explored. The instructor explains the reasons for choosing MongoDB in this course and provides a detailed walkthrough on setting up MongoDB using Atlas and Mongoose for backend development.
00:00:02 I want to take a second and share my honest thoughts on why we're using MongoDB in this course and not an SQL database like Postgres.
00:00:10 You totally could use an SQL database for this project as well.
00:00:14 As a matter of fact, you could use either SQL or non-SQL for any kind of project.
00:00:19 But when making that decision, you have to ask yourself a couple of questions.
00:00:23 I'd actually choose Postgres over MongoDB for our Devflow application because it's designed for data with complex relationships.
00:00:32 like questions, answers, and comments, and it ensures consistency when users interact, such as voting or commenting.
00:00:41 SQL's powerful querying makes it easy to find top posts, manage user scores, and filter by tags.
00:00:48 But in our case, we'll be using MongoDB for the flexibility in data structure that it provides, like changing fields often or storing related info.
00:00:58 like questions with answers together in one place.
00:01:01 MongoDB is also great if you expect massive traffic, since it handles scaling very easily.
00:01:07 Plus, if you're dealing with a mix of structured and unstructured data, MongoDB's document model makes storing and retrieving data super simple.
00:01:16 It's also easier to set up compared to SQL, and many huge companies actually prefer using MongoDB, as you can see on their page.
00:01:24 So with that in mind, choosing either of the two options for database is totally fine.
00:01:29 In this course, I've decided to go with MongoDB, but as an extra challenge, after you complete the course, you can try making it work with Postgres.
00:01:37 And if you want to, after you finish the course, you could try replicating this.
00:01:41 You can try replicating our database structure and the entire project using Postgres.
00:01:46 That's totally up to you.
00:01:47 But I just wanted you to know the differences between the two.
00:01:50 In most cases, there is no right and wrong answer.
00:01:53 Both databases will do more than fine and will never cause you any optimization issues.
00:01:58 Proper querying, on the other hand, will.
00:02:00 So with either of the two databases, it is super important to write optimized and well-structured queries to get the highest output.
00:02:08 To get started creating our backend, head over to mongodb.com, a NoSQL database that we'll use for this project.
00:02:16 MongoDB is just a general database, but in this case, we'll be using one of their products called Atlas.
00:02:23 So head over to products and then Atlas and see what it is all about.
00:02:27 It is a cloud developer data platform.
00:02:29 What does that mean?
00:02:30 Well, it means that they will host your database for you completely for free.
00:02:36 That's pretty cool, right?
00:02:38 So let's head over to Try Free and you can create an account or sign up with Google.
00:02:43 Once you're in, you should be able to see something that looks like this if you already have some projects or organizations created.
00:02:50 I have a single organization here.
00:02:52 and I have two projects on it, and I think I can add another project.
00:02:56 You can do the same.
00:02:57 If it asks you, feel free to create a new organization, and within that organization, create a new project.
00:03:06 You can give that project a name.
00:03:07 In this case, I'll do JSM underscore Devflow, and simply click Next.
00:03:12 Once you do that, you can invite some people if you want to, or just make yourself as the project owner that you are, and click Create.
00:03:20 This will now spin up your project for you, but within that project, you'll have to create a new cluster.
00:03:27 A cluster is a group of MongoDB servers that work together to store your data, manage queries, and handle requests.
00:03:35 They're designed for high availability and scalability.
00:03:39 Let's go ahead and create a new cluster.
00:03:41 It's going to ask you to choose a version.
00:03:43 In this case, we're going to go with a completely free one, M0.
00:03:46 You can upgrade anytime.
00:03:48 but it'll be completely free forever if you decide not to upgrade.
00:03:51 Let's choose a name.
00:03:53 I'll do something like Devflow, and you can choose the region that is closest to you, and click Create Deployment.
00:03:59 Next, you'll have to create a user.
00:04:00 I'll call mine Adrian, and the auto-generated password, which I will copy.
00:04:06 Make sure to copy it, please, or change it to a password that you use more often, and click Create Database User.
00:04:14 Once you do that, choose a connection method.
00:04:16 In this case, let's say MongoDB for VS Code, or if you're using WebStorm, I believe you can choose Drivers right here, which is going to allow you to set
00:04:24 it up with whatever system you're using.
00:04:26 In this case, I'll go with MongoDB for VS Code, and it's going to ask you to install a VS Code extension.
00:04:32 MongoDB for VS Code.
00:04:34 So let's go back to our VS Code.
00:04:36 I'll head over to Extensions and choose MongoDB.
00:04:40 for VS Code.
00:04:43 We can see it right here.
00:04:45 Let's install it.
00:04:46 There we go.
00:04:48 And it has this nice interface that allows you to connect with the connection string.
00:04:52 So let's click Connect, and it wants you to add this connection string right here.
00:04:57 So back in our browser, it's going to ask you to connect with the connection string and you'll be able to do that right here.
00:05:03 But right now it says that devflow is provisioning and typically it takes a couple of seconds or up to a couple of minutes to actually provision a MongoDB cluster.
00:05:13 So let me actually reload this page and now a string has been generated for me.
00:05:17 So before you copy, make sure to save your password somewhere else because you'll have to add it within this string.
00:05:25 Then paste this, and you'll have to replace this part right here, including the code signs, with the password that you chose for your account.
00:05:33 Make sure to do this right.
00:05:35 Press Enter, and there we go.
00:05:37 A MongoDB connection has been successful.
00:05:41 This new extension for VS Code makes it super simple.
00:05:44 But if you've been following through some other platform, just connecting it to your MongoDB drivers is not gonna be a problem at all.
00:05:52 You just have to add your connection string into the application code.
00:05:56 Here is an example on how you would do that.
00:05:59 But we'll go through some of these parts ourselves as well once we set up the new MongoDB client.
00:06:03 So don't worry about this too much.
00:06:05 Just continue following the video.
00:06:07 What we'll also do is head over to our file explorer.
00:06:12 and head over to our env.local and I'll actually add a mongodb underscore uri is equal to and I'll paste not the password but rather the connection string
00:06:25 that was given to me including the right password so make sure to remove this part everything before the add sign and after your username And in there,
00:06:34 you can paste your password just so we have it here as well.
00:06:38 Perfect.
00:06:39 Also, another thing that I always like to do is head over to network access.
00:06:44 We currently have one active IP address list, but sometimes for whatever reason, your app can block this address.
00:06:52 So just head to add IP address and click allow access from anywhere.
00:06:58 and confirm.
00:07:00 This will now allow you to access your database without any issues.
00:07:03 Now that we've talked about MongoDB, let's talk about Mongoose.
00:07:07 Whereas MongoDB is a non-SQL database, and MongoDB Atlas is a simple way to deploy that database on the cloud.
00:07:18 As you can see, we have access to it right here within clusters, Devflow, and then collections.
00:07:23 It is just ready to start accepting the data from our application.
00:07:26 But in that case, what is Mongoose?
00:07:29 And by the way, sorry for this blinding screen from dark to light.
00:07:33 It looks like Mongoose doesn't have a dark mode.
00:07:35 But let me just show you one cool trick.
00:07:38 If you're using Arc, you can actually go ahead and use Zaps.
00:07:41 Check this out.
00:07:42 You can customize the page, which is going to bring this editor, and you can then manually change the color of the background as well as the text of this
00:07:51 specific website.
00:07:52 Pretty cool thing, right?
00:07:53 I'll try to make sure to go to something like dark, like this.
00:07:58 And I'll try to change the text to just be white.
00:08:01 There we go.
00:08:02 This is pretty cool, right?
00:08:03 And now, check this out.
00:08:05 We have a complete dark mode.
00:08:07 It might have some issues now that we are manually changing the website's code, but overall, it looks good to me.
00:08:13 Just another cool Arc feature.
00:08:15 But with that said, let me get to the actual point.
00:08:18 What is Mongoose?
00:08:21 Mongoose is an elegant MongoDB object modeling for Node.js.
00:08:26 Okay, sounds interesting, but what is it really?
00:08:30 Well, it's an object data modeling library for MongoDB and Node.js.
00:08:36 Basically, it provides you with a structured way of defining schemas, which are data structures for MongoDB collections,
00:08:44 making it easier to work with your database by defining your models within the application code.
00:08:51 It says right here, you can write MongoDB validation, casting, and business logic easier using Mongoose.
00:08:57 How to use it?
00:08:58 Well, you just require it as a package.
00:09:01 You then connect it to your MongoDB cluster.
00:09:04 You use it to define different models, like a model of a cat, and then you provide a structure for how that cat looks like.
00:09:12 Once you do that, you can actually generate new instances of that cat.
00:09:17 and give it different names and even different methods, like this meow right here.
00:09:22 And if you didn't know this about me, I love animals.
00:09:25 We have a couple of dogs and a couple of cats right here in the house, and they're always there to help me debug some stuff.
00:09:31 I don't have my rubber ducky, I actually have my cats around me, which I explain the bugs to, and then sometimes they don't even meow back to me,
00:09:39 but still, as I'm explaining the errors to them, I then come up with a solution on my own.
00:09:44 So with that said, Mongoose is a straightforward schema-based solution to model your application data.
00:09:51 Is it the only solution for MongoDB databases?
00:09:54 Not at all.
00:09:55 You can use other ORMs like Prisma, Drizzle, or even MongoDB's native driver, which is this one right here, that allows you to model your database as well.
00:10:05 But as you can see, it integrates with other platforms.
00:10:08 Oh, and let's switch this to dark mode as well.
00:10:10 So there we go.
00:10:11 Now that's better.
00:10:12 But in this case, what should we use for our specific application?
00:10:15 Use Mongoose if you want to add structure to MongoDB schemas, validation, and an object data model.
00:10:22 Use the MongoDB native driver for high flexibility and direct control over MongoDB without added layers.
00:10:29 It is simple and has the best performance, perfect for lightweight or high-performance apps that don't need strict data rules.
00:10:37 But you will have to write a bit more boilerplate code, which Mongoose takes care of for you.
00:10:42 Use Prisma for SQL databases.
00:10:45 Even though it supports MongoDB, I would still use it only for SQL.
00:10:49 And finally, Drizzle.
00:10:51 Well, it doesn't support MongoDB yet, so maybe it does soon.
00:10:55 In this case, your best choice, if you need a structured schema validation in a mature ecosystem, Mongoose will still be the way to go.
00:11:03 So let's actually install it by going to read the docs, defining your schema, and it looks like they don't have the installation.
00:11:11 Oh, maybe right here in the quick start.
00:11:12 There we go.
00:11:13 So what we can do is just run npm install mongoose.
00:11:17 I'll do that right here within my VS code by running npm install mongoose.
00:11:23 Next, let's see what the second step says.
00:11:25 And it says, now say we like fuzzy kittens and we want to record every kitten we ever meet in MongoDB.
00:11:31 We need to include our mongoose in our project and create a test database.
00:11:36 Okay, I like how the code block doesn't even have to do anything with kittens, but they added it right here.
00:11:42 So what we can do is basically create a new connection to the MongoDB database.
00:11:48 So even if you didn't connect using the VS code setting, now we're going to connect it like this.
00:11:52 So not a problem at all.
00:11:53 Let's go ahead and do it together.
00:11:55 I'll actually do it within the file explorer and I'll do it within a new file called lib.
00:12:02 mongoose.ts.
00:12:05 Think of this as the database setup file.
00:12:07 So let's import mongoose as well as mongoose like this, coming from mongoose.
00:12:16 Next, let's define our MongoDB URI, which is our special connection string.
00:12:21 And it's going to come from process dot env dot mongodb underscore URI.
00:12:28 And we'll say to TypeScript that it will be of a type string.
00:12:32 Let's make sure that it is actually there by heading over to our env local mongodb URI.
00:12:38 Here it is.
00:12:40 Now I'll check.
00:12:41 If there is no mongodb URI, I'll throw a new error saying mongodb URI is not defined.
00:12:48 After that, we want to play with cache a bit.
00:12:51 That's because we'll be using server actions to make all of this work, and server actions don't really remember about the previous server action call that
00:13:00 was made, unlike the typical server that always keeps track of the MongoDB connection.
00:13:05 For that reason, we actually want to cache the connection, which will allow us to simply connect to it on every server action call.
00:13:12 Let's first define a TypeScript interface by saying interface mongoose cache, It's going to have a connection equal to mongoose and it's going to have
00:13:23 a promise equal to mongoose or null like this.
00:13:28 Once we have it, we can declare a global variable where we can define the var mongoose as mongoose cache like this.
00:13:41 This is just for Tabscript not to bother us.
00:13:44 Next, we can define the cached variable and make it equal to global.mongus.
00:13:52 Finally, if a cached version doesn't exist, so if no cached, then we're going to set the cached variable to be equal to global.mongus equal to an object
00:14:03 of a con, which stands for connection set to null, and promise set to null as well.
00:14:09 We're doing this because Next.js functions can be invoked multiple times.
00:14:13 especially in development.
00:14:15 So without caching, each invocation could create a new database connection, leading to resource exhaustion and potential connection limits being reached.
00:14:24 This singleton pattern, which allows us to declare the connection once and maintains that single instance of the connection,
00:14:31 improves the performance and reliability.
00:14:35 And in this case, I purposefully used va right here so we can have a global version of the mongoose variable.
00:14:42 ESLint is saying that's not ideal.
00:14:44 So what we can do is we can simply disable novar for this file.
00:14:49 So it won't complain.
00:14:51 And here the connection is complaining that at the start it is set to null.
00:14:54 So we can say that null is one of the possible types in case we don't yet have a real connection established.
00:15:02 But with that said, let's establish a real connection by specifying a new function const dbconnect.
00:15:12 is equal to an async arrow function that's going to return a mongoose promise.
00:15:17 So we can define the type right here, promise of mongoose.
00:15:22 And first it's going to have an if statement and check if there is a cached connection.
00:15:28 So if there is a cached connection, we're going to simply return that connection.
00:15:33 Else, we need to create a new promise for connecting to the database.
00:15:37 So let's say if there is no cached.promise, in that case, we'll set the cached.promise variable to be equal to mongoose.connect.
00:15:49 And we're going to connect to a MongoDB URI.
00:15:53 And we need to pass some options.
00:15:55 Specifically, we want to connect to a DB name of, and then here you have to pass the name of your database, or rather the database that it'll generate
00:16:04 for you.
00:16:05 In this case, I'll say just Devflow.
00:16:07 I think you can just say Devflow too.
00:16:09 After that, we can call a .then where we get the result.
00:16:15 And we can simply console log connected to MongoDB and return the result.
00:16:20 And we can also run a dot catch on it where we get an error and we can console log error connected to MongoDB and we can throw a new error.
00:16:31 Let's close this properly.
00:16:34 And finally, we have to go a bit down outside of this if statement and the wait for the promise to resolve by saying cache.con is equal to a wait cache.promise.
00:16:47 And finally, once that happens, we can return a cached connection.
00:16:52 So this is going to be the simplest way to always have access to our MongoDB database without having to reestablish connection upon every server action call.
00:17:03 Great.
00:17:04 So now let's export default this dbconnect function so we can use it in other files.
00:17:11 And I think I have to put this part right here after this function.
00:17:15 That way it knows what it is returning.
00:17:17 I just use the wrong syntax right here.
00:17:20 There we go.
00:17:21 So now we have the functionality that establishes a MongoDB and Mongoose connection.
00:17:26 I think that's enough for this lesson.
00:17:28 We'll put this connection to use very soon.
00:17:30 But for now, we can just say setup MongoDB and Mongoose.
00:17:37 Commit it and sync it.
00:17:40 Great work.