{"id":15190609,"url":"https://github.com/sibeeshvenu/node-with-mongodb-and-mongoose","last_synced_at":"2026-03-03T18:03:19.881Z","repository":{"id":88166719,"uuid":"127714106","full_name":"SibeeshVenu/Node-With-MongoDB-And-Mongoose","owner":"SibeeshVenu","description":"The detailed article can be found here","archived":false,"fork":false,"pushed_at":"2018-04-02T06:42:02.000Z","size":157,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-11T02:17:49.434Z","etag":null,"topics":["article","database","mlab","mongodb","mongoose","mongoose-model","nodejs"],"latest_commit_sha":null,"homepage":"https://sibeeshpassion.com/using-mongodb-on-node-js-application-using-mongoose/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SibeeshVenu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-02T06:33:36.000Z","updated_at":"2018-04-10T06:53:14.000Z","dependencies_parsed_at":"2023-03-16T17:15:23.302Z","dependency_job_id":null,"html_url":"https://github.com/SibeeshVenu/Node-With-MongoDB-And-Mongoose","commit_stats":{"total_commits":4,"total_committers":1,"mean_commits":4.0,"dds":0.0,"last_synced_commit":"5bf0b80bf28b53220c981d72f67d90c1de13a51e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SibeeshVenu%2FNode-With-MongoDB-And-Mongoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SibeeshVenu%2FNode-With-MongoDB-And-Mongoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SibeeshVenu%2FNode-With-MongoDB-And-Mongoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SibeeshVenu%2FNode-With-MongoDB-And-Mongoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SibeeshVenu","download_url":"https://codeload.github.com/SibeeshVenu/Node-With-MongoDB-And-Mongoose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241110754,"owners_count":19911391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["article","database","mlab","mongodb","mongoose","mongoose-model","nodejs"],"created_at":"2024-09-27T20:43:02.709Z","updated_at":"2026-03-03T18:03:19.833Z","avatar_url":"https://github.com/SibeeshVenu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node-With-MongoDB-And-Mongoose\n\n## Introduction\n\nIn this post, we are going to see how we can use a MongoDB on our Node JS application with the help of the package Mongoose. We will also be covering some facts about MongoDB so that as a reader, you will understand why we had chosen MongoDB as our backend. We will be going through some steps to install the required packages using a node package manager terminal, so please be with me. At the end of this article, I guarantee that you will be proficient on how to connect a MongoDB in our Node JS application. I hope you will like this article.\n\n## Why MongoDB?\n\nHere, I am going to list down the reasons why I had chosen MongoDB.\n\n- MongoDB is a NoSQL database when I say NoSQL, it means that it doesn’t have any structure to be followed.\n- There are no relationships, we can perform the relation using separate packages like Mongoose\n- Everything is JSON, even the data and collections are stored as JSON\n- Since the data is stored as JSON, no more conversions are needed. We can directly use it in our application.\n\n## Background\n\nNode JS has become a trend now, thus some most used databases. One of the recommended database for a Node application is MongoDB. As we discussed a MongoDB doesn’t have any structure inbuilt, here we are going to use a package called Mongoose, with that we can create some structure for our Database. Let’s just skip the talking and start developing.\n\n## Setting up Node application\n\nTo get started with the Node JS, create a folder and name as per your wish, this is going to be our project directory. Here I am going to use one of my favorite editor, Visual Studio Code. Now please point to your project container, and run the command below.\n\n`npm init\nNPM Init\nNPM Init`\n\nThis will create a file named “Package.json” in your directory. We will be adding all of our dependencies in this file very soon. The command may ask you some default questions which you need to answer. If you need to create the package.json file with default values in it, you may consider running the below command.\n\n`npm init --yes`\n \nLet’s review our package.json file.\n\n`{  \n\"name\": \"node-mongodb-mongoose\", \n\"version\": \"1.0.0\", \n\"description\": \"A Node application with MongoDB and Mongoose\", \n\"main\": \"index.js\", \n\"scripts\": { \n\"test\": \"echo \\\"Error: no test specified\\\" \u0026\u0026 exit 1\" \n}, \n\"keywords\": [ \n\"Node\", \n\"Mongoose\", \n\"MongoDB\" \n], \n\"author\": \"Sibeesh Venu\", \n\"license\": \"ISC\" \n}`\n \n## Getting the required packages\n\nNow that we have the application ready, let’s get the required packages ready.\n\n`npm install --save mongoose`\n\nThis will create a new folder npm_modules on your project directory where you can see the package Mongoose in it.\n\n`npm install --save express`\n\nThe above command will add the package express to our application.\n\n## Creating the Node App\n\nNow we can create our server.js file where we are going to write most of our application code.\n\n`var express = require(\"express\") \nvar app = express() \napp.listen(\"3010\",()=\u003e{ \nconsole.log(\"I just started listening!.\") \n})`\n\nNow run the command node server.jsand make sure that you are getting a console as ” I just started listening!.”.\n\nIf you are getting an error as preceding, please make sure that you are running the application on an unused port.\n\n`PS F:\\Visual Studio\\Node.JS\\Node-MongoDB-Mongoose\u003e node server.js\nevents.js:136\nthrow er; // Unhandled ‘error’ event\n^\n\nError: listen EADDRINUSE :::3000\nat Object._errnoException (util.js:1031:13)\nat _exceptionWithHostPort (util.js:1052:20)\nat Server.setupListenHandle [as _listen2] (net.js:1367:14)\nat listenInCluster (net.js:1408:12)\nat Server.listen (net.js:1496:7)\nat Function.listen (F:\\Visual Studio\\Node.JS\\Node-MongoDB-Mongoose\\node_modules\\express\\lib\\application.js:618:24)\nat Object.\u003canonymous\u003e (F:\\Visual Studio\\Node.JS\\Node-MongoDB-Mongoose\\server.js:3:5)\nat Module._compile (module.js:641:30)\nat Object.Module._extensions..js (module.js:652:10)\nat Module.load (module.js:560:32)”`\n\n## Setting up database\n\nNow that we have our application ready, let’s go and create our database. I am going to use mLab for creating the database. If you have not installed MongoDB on your machine, I strongly recommend creating a database in mLab. I had created a database there and have got my connection string as preceding.\n\n`mongodb://\u003cdbuser\u003e:\u003cdbpassword\u003e@ds038319.mlab.com:38319/mylearning`\n\nWe will be updating the connection string with actual DB user and password later. Please make sure that you are creating a user for your DB and remember the password.\n\n## Creating a Mongoose model\n\nLet’s create a Mongoose model and set up our connection string now, which is going to be our collection. No worries, it is just a JavaScript model.\n\n`var conString = \"mongodb://admin:admin@ds038319.mlab.com:38319/mylearning\" \n/** \n* Models \n*/ \nvar User = mongoose.model(\"User\", { \nfirstName: String, \nlastName: String \n})`\n \nPlease be noted that, when you move the codes to production, make sure that your password and user fields on the connection string are separated from this file and encrypted. Now let’s connect our database. \n\n`mongoose.connect(conString, { useMongoClient: true }, () =\u003e { \nconsole.log(\"DB is connected\") \n})`\n \n## Setup the data\n\nWe have our model ready, what else is needed now? Yes, you are right, we need data.\n\n`var dummyUser = { \nfirstName: \"Sibeesh\", \nlastName: \"Venu\" \n}`\n\n## Inserting the data into MongoDB\n\nWe have got everything ready, we can insert the model now. Let’s do the saving logic now.\n\n`mongoose.connect(conString, { useMongoClient: true }, () =\u003e { \nconsole.log(\"DB is connected\") \nsaveData() \n}) \nfunction saveData() { \nvar user = new User(dummyUser); \nuser.save(); \n}`\n \n## Verify the data\n\nOnce you run your application, go to your database and check for the entries there. I am sure there will be a new collection “User” and our inserted data. As I am using mLab database, here is how my data saved.\n\nIf you have noticed, you can see there are an another property names “_id” in our database collection. This is a unique id generated by MongoDB for us, so no need to think about it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsibeeshvenu%2Fnode-with-mongodb-and-mongoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsibeeshvenu%2Fnode-with-mongodb-and-mongoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsibeeshvenu%2Fnode-with-mongodb-and-mongoose/lists"}