{"id":1522,"date":"2022-11-29T10:18:15","date_gmt":"2022-11-29T10:18:15","guid":{"rendered":"https:\/\/www.workfall.com\/learning\/blog\/?p=1522"},"modified":"2025-09-24T09:47:59","modified_gmt":"2025-09-24T09:47:59","slug":"how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js","status":"publish","type":"post","link":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/","title":{"rendered":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/kW_xSy0-PPMh_pqg4x0zMOmbUyE_fx1mtAuBco3TotB5zQ-pMnUjb50XJ7X94nDL9CmOJaNP4D5vdodd6VKKTBMVcbhUStsMzeSRoVlVxZvcWKuVywrjHkhzcY6l0KExs-S3AnGmmw9ydevsu6RQnYXhRelRjhaFjOAYXjl2EShXOvKCtRNidXwaOpS0Tjee\" alt=\"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">MongoDB is one of the most popular No-SQL databases in the developer community today. Instead of SQL objects, No-SQL databases allow developers to send and retrieve data as JSON documents. In this blog, we will demonstrate how to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js. Let\u2019s get started!<\/p>\n\n\n\n<p>In this blog, we will cover:<\/p>\n\n\n\n<ul><li>What is MongoDB?<\/li><li>What is MongoDB Atlas?<\/li><li>What is Mongoose &amp; why?<\/li><li>A quick comparison of MongoDB &amp; Mongoose<\/li><li>Hands-on<\/li><li>Conclusion<\/li><\/ul>\n\n\n\n<h2>What is MongoDB?<\/h2>\n\n\n\n<p class=\"has-text-align-justify\"><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-build-and-deploy-a-mern-stack-application-on-aws\/\">MongoDB<\/a> is an open-source document-oriented database that can store a large amount of data while also allowing you to work with it very efficiently. It is classified as a NoSQL (Not only SQL) database because data in MongoDB is not stored and retrieved in the form of tables.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">It is well-known among developers not only for the efficiency and smoothness with which it handles data, but also for its driver support for popular languages such as <a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-export-data-to-a-csv-file-using-multiple-npm-packages-in-node-js\/\">Node.js<\/a>, PHP, <a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-write-clean-code-with-dependency-injection-in-java\/\">Java<\/a>, <a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-use-go-modules-for-package-management\/\">Go<\/a>, .Net, Ruby, Scala, C#, C, C++,&amp; <a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-connect-snowflake-with-python-and-execute-queries\/\">Python<\/a>.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Top companies such as Facebook, Google, Adobe, Nokia, and many others have chosen MongoDB as their database management system.<\/p>\n\n\n\n<h2>What is MongoDB Atlas?<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">MongoDB Atlas is a multi-cloud database service developed by the same team that created MongoDB. Atlas simplifies database deployment and management while providing the flexibility required to build resilient and performant global applications on the cloud providers of your choice.<\/p>\n\n\n\n<h2>What is Mongoose?<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">Many people who learn MongoDB are introduced to it through the widely used Mongoose library. &#8220;<em>elegant MongoDB object modeling for Node.js<\/em>,&#8221; according to the description of Mongoose.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Mongoose is a MongoDB ODM (Object Data Modeling) library. While using an Object Data Modeling (ODM) or Object Relational Mapping (ORM) tool is not required for a great MongoDB experience, some developers prefer it. Mongoose is used by many Node.js developers to assist with data modeling, schema enforcement, model validation, and general data manipulation. And Mongoose simplifies these tasks.<\/p>\n\n\n\n<h3>Why Mongoose?<\/h3>\n\n\n\n<p class=\"has-text-align-justify\">MongoDB comes with a flexible data model by default. This makes future MongoDB database changes and updates very simple. However, many developers are accustomed to rigid schemas.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Mongoose imposes a semi-rigid schema from the start. Developers must define a Schema and Model when using Mongoose.<\/p>\n\n\n\n<h2>A Quick Comparison of MongoDB &amp; Mongoose<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/VZcrXrXSgPReGzYnNrimWhkbYUqSD4Ub2VNmsTrDO_rjKbTvgRuqH8ilzCc4o8ipmAKuTA2mP9VIAX3sQk9Rj4tSbXogcuxs7VYJ3v0C91IDjOWwdu9gtBOXNJSuTmWGL2VdLQJ4BBr62aCHqnq9sqh099TylSNV07wNalDzLe5JJSEsIKsVp_3G8cTG-LrQ\" alt=\"A Quick Comparison of MongoDB &amp; Mongoose\"\/><\/figure>\n\n\n\n<h2>Hands-on<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we will see how we can connect to MongoDB using Mongoose and MongoDB Atlas in Node.js. We will begin with creating a new directory on the local machine. Initializing the app in the directory, we will install the required packages for the implementation. Verifying the installation of the packages, we will create a code file thereby putting in the code to connect to the database. Navigating to the MongoDB Atlas, we will demonstrate how to sign up and create a new database.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">We will then configure a new user and add the IP so that we can access it in our code. Altering the parameters in our code, we will then move on to create a model which is a database schema. Exporting the schema we will then create route files with the API calls to add a record in the database and to list all the inserted records. We will then spin up the server and navigate to the postman application to hit the insert API. On successful execution, we will run the get API followed by testing out the same for multiple records.<\/p>\n\n\n\n<h3>Required Installations:<\/h3>\n\n\n\n<ul><li><strong>Express: <\/strong>It is basically a back-end web application framework for Node.js designed for building various web applications and APIs.<\/li><li><strong>Mongoose: <\/strong>It is a<strong> <\/strong>MongoDB object modeling tool designed to work in an asynchronous environment.<\/li><\/ul>\n\n\n\n<p>Create a new directory on your local machine.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/66H1Hi8MAsSzHbaQTwwir-Y_GSjaTf3gTOhmSwfE2uMPMG7kFxFT21QISKuuxuN_YBMsIchPm5GJpvm3EgBIFDLQH-t7SLDc5fKNs5NJzQSZVBujHzTmz1ReX6vWwkC6P-wbnph8aoB-_5bGG0xoHjjfjj4XbBfujm8ZPPN-viPIpnpbwGengxvJjGp61UAT\" alt=\"\"\/><\/figure>\n\n\n\n<p>Right-click in the newly created directory and click on git bash here.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/IZigdxTKAAm-ANrzsm0njkKd0G9pEetA25TBtausK9WdkYI_U2TYGeVUiYjjmMqrDTnbTthrO8RaLZKK545WvUdRTVt5z4ZYrN76d3y0o45X9_k95ozMJJtzBdJgP5LcO6kvv7XcfDECUATL39JMu5H1h5VpTay005WM4K8mIbrfOTh06jlN1Az-EqF3t_Ip\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Run the below command:<\/p>\n\n\n\n<p><code>npm init<\/code><\/p>\n\n\n\n<p>Give a name for the application and for the other options select the default configuration.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/crKVGRIwvNpBOwT97E1fo3GeGdhOtymHgJ8t7fWNuijjHu4j37CyIk3CgGv1R_LtPP859_JJpHZTsosJM6y1TQWtbe5jP_2N62fnHclbMUFATN30m8RzTCx4BLyGkXB_UXEIiNnf5tROXZFWpWgU28OFVtm4xJx_k8DXld16xv-MUWg4ZV7QgJhPVwAc7NAW\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Select yes for the final configuration. Confirm the created JSON object.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/31LaJbiDW33yxe-zdOVnc_lpm2nIJMdOYdzkl1Fq7doPXe1oBWdJr1kJmNY8LrDHljfqkcIEWtZK8MO4KfNxXu4dlnelhNucWTPeEA6kKNW14y_amL97foSJnu3FX2NzzC1y44zgb-_ABZtcMrpegFngSnvHDF-6Kh1lm3j0YbfB6RBiVqum2-S-pUQaJvNy\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, open the directory into a code editor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/humZxpbY38DM57HbdvFs2gAMbJp_VgJRlcZcxW3fSXA1Zb_pmOPwKgAJiaNLYG3okaEypO8iKPX80A-nu65yoFkUumd2GkvlF2N03u_qDy1pZdxsiBCeTBWCFducjGVJW-mpHGPJVscGAKlVx78F4Zzh1gBYXKhtBbBqZNTGKbs7YsVUuEqoV1qlBOFmH6MK\" alt=\"\"\/><\/figure>\n\n\n\n<p>You will see a <em>package.json<\/em> file created.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/NFWOMQOZFIKzL8t91QNP5X4dGp-SxnqwG2RuzdRNgIAryXB4XrmCfLD2J7Bq7iIvGOfrtEJa1vWVM2WiAQhBWYvyi5XcKGoMh9Ng9260xFhwpTzVne8ct0DVR6gpgZVZUjY9i_Y3BrntXSTeL3xc0Lc4XwhawNdqYlEYujbss_WIPd_VJlp4lmQeQ1aJnX9S\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, we need to install express. Execute the below command in the console:<\/p>\n\n\n\n<p><code>npm install express --save<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/U1I1cq0jpdnhQr20NRn8-z6GIreLwBaIn_pKu-4JQ2HZvHmzgOGMYbYJPraavxOwLnhqiEoRVA-z35G-SCalCSX8dqZlDHcWuWBkEVnqPMCeWvyeB0ypMFo-WnbJ6ENO4X7FdiBlo8UibqcXRW56XdziW-63L8jzR0rc9PYhWfh-79eEetj48Jwos9RVdws5\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the following message on the console.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/DfSRs-Hc4o4SDplaqQW3hKW5WgUmLqq2hG1lyWFZk84E9Wo4fiTBml03gJan5o610Dl0CCK-MSVKxHyFNz6sx0D_0_yAJoukGv6m6goFtbWspiIR8S-EW9DQCDw1yhM75SK7wOoBTMK-jqUMObyvo10mo7_Ufc7ItNvjrUBRpmPjTVeIHkLUvaKmh1FK7usV\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now we need to install a mongoose. To do so, run the below command:<\/p>\n\n\n\n<p><code>npm install mongoose --save<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/PQjqXLwvMjWp4Xz4tC8hN3zHJYSr90T-yxIRzqRmjjTz1ZXhVz6jMckyiHtBqLFQByFno4OIx2BqDnF0Ykx0vIMBnPduqjC5_ZwEvKYIi_gBVUhjlgZGf7dMpFf8owu2xUY2S-t8YQlWYYYG8XNlOlT0An3lOYxy01U6DMu7t8UUTPsZtBBzTFTmliNdEbgw\" alt=\"\"\/><\/figure>\n\n\n\n<p>On successful installation, you will see the following message on the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/yVXKTWExl2eBItx1DfJXbbWDsYkO8GabiLMfqmDCvM_LdD-MpzBr_nwjax-cBzaYYcmcQNEc9L7_28Ura_8nhM7Zrg6_Q9YwEYoTv_y1e0n06gPBO0WOWBqXcgbjMCvS1ujJ4rFOU7X06N51Qpm8h6iNrnnpFeAqQyC-unPTHgXwlcDKHR9_cfGpdT7P9kOo\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Open the <em>package.json<\/em> file, file installation of express and mongoose packages.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/GRxNCLFdzy0ttrfJDYAwH9rmEdym7JHnzGVPMT-NQ8n3KTD24y9DLO8FEog6dd_Fj1Es7ba8ltpgT_Ctuwhm_3dh5hliXA09uhAbjd0loBFFgmvPfUgrIrkQXGviFmQ_El4bpHAD_o2GhwztuS5j2ZBcTTjt4Whl9SFJfqVjUs9GODbapzFF-tNNSJSGA2jk\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Create a new file in the directory with the name <em>server.js<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/6CB3aHvDQkZiiApsLArAXlMWCN58KexAE45iRn3q19pvU1UFsGdtXZp7glyXyjpk114ufbh89LTyjQyMxxDtuaF3TtspUX6pw2GQ0YazEut2L35CPcYHsR_jbweFSTDtpTtv8hNV_tuXKu5ywGe_Ksf3B2aT_IOzbUW0FUG8XwetpwVQcMpjD7ot0EEBM-wp\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Import the express mongoose and router packages using the code shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/kj0vRQApTcTQsJxeT3sG0z79m5mqAlwFMveWXw5WaXymwnn4A2mhcMBb635mV14vp32TplleRZlBN1947EUyLRGAo3a4PHuE9m_1x0I6vuSldYNgG7Q25wJA76d2yjvCpFNd4zK2lVa27WvOZzy1hAIrSGGSqYOaeZczI55nyuKz5K1lskfgkw7zpz8_xvVO\" alt=\"\"\/><\/figure>\n\n\n\n<p>Create an object for express to be able to use its functionalities.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/riWZsHm_ui6oFkRl12gQhnNZGGp1jVt2ZJWKC4Dn-Ynbd_EsMB_hMS_QCcHp69NoLFVwaEwQyl9GkbT9VnnyiK6bTs8QQMN_6-akpR2aqKN862Ex92meOAcy6cn1C-hw51T_mq1c6sJjnOmmCp2w8oXJo3AFn2MbcltQ9U36Ht7P6qQqQZb_nXPBu6RkZXA1\" alt=\"\"\/><\/figure>\n\n\n\n<p>In the below code, we will be able to connect to the MongoDB database.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/0ZXZg9zI_wFGPqyLbunrcfY81Hi8pTxn_GU3qO1XIQ2UdGcezXRLT4-_IXpMWJYkZGzyycg2qkTT6eY8FMjoseXoAbxJgDAhUOoFe6fpizZFXeM43Dt0niPKjL4vNHrPm2n89QiTXCx5s4OyU_qBwjfW6ZzkE9oYSdUYMUXdNv20rtO0HX5J8XO0tZuOyLgk\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Now we need to create a cluster on mongo DB atlas. To do so click on the below link:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.mongodb.com\/cloud\/atlas\/register\">https:\/\/www.mongodb.com\/cloud\/atlas\/register<\/a><\/p>\n\n\n\n<p>Fill in the details.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/xxC_9Jklyry4xXIK6bT6Q40NDJkdUpoMxdR9Fdq-nCIVgWButxHNNGUJ-IYq-5kl-baUKsHyGH1AA9BwyDoCoHoedcxB2Nu0BcjGIxsjGGKYyFyqHlqMJtlFC9DLTh8DoQCwv4UOSW3RcsHA5ZbRixiJJDx7nMecDEVSlewqRiBh2ERV4c_Qxoi8FIGy88Y5\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Or you can scroll down and sign up with Google.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/4IqbESGy-MrQueGvQb1dfrVIfYasJrG6wrDTN-2ug3PAcKuJMm9irAyJpWsFhb3eFRGrLPuFxaOw3V764MqSu2kZ9RnsWydcVLihJpbK7RLK4RVYkyp2IOMv5OSmX7fw9XmxY15Qi6nmfZk7tzrvXXT_4rGi-RFN_PxTWXzG3UHYeXE73ZmVhGJ_lJIUZF1V\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>On successful registration, you will be navigated to the following page. Click on build a database.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/hqOyWgXghfOPdRfY3csbgPTfy42gAUDYqAlopJcOlw_cmZNdK1VCuVOJWuk3U7eIvwMd2vKkmtR1SE6dWftyxiB3hTWr-fBnu5n_Z3p6D1_gy2ouVZ3VcoDvIZaEoD1EYhBcqt3kHGlcaZV_0v5ryfe7ty04O5jlp37T-B2kfFsFMi6BkL15WEr8I_hlheTu\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">You can choose the different types as per your choice. For the demo purpose, we will be using the free one which is the shared type. Click on create below the shared type.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/FUOlUK5HaL_fQ-xLU4oQcEVJeuL6Uxzne381wed-sERPyWCOU5IBFbhwwZV4TmjGQWKUYqv6_BY0G3gDS1iLoBHJMsfQIyFhx3oEVtRqfqJYn2HuPoxPkH4RAUxzaAfVzPZ7tx42CGZEqAGJ0Hw-iiLInihw7D9tv24HbmLB2jCwAKQJRh5jCMspFAdR89sl\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Select the cloud provider as per your choice.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/2LWqi_MpQf0lnp3clsWuyj8m-b4FsoszAsHbogVIn3xxiJo1ewAM7h3DP0U4Rxk_Yj0GVJ5dVCNHa4SmQN82Snzs6tO1CCtK8ACKL9lfg_BmXW8DPK7LY7bseqUA0prrk2pnkYqKqMN0RGsHFpwXt6e7XC60iGFUFwFn4EoWDJNIIdUkukUUJEJdUTOJlF8S\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Scroll down and select the region for the database.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/0RcXYrNGWO3QH7RxFz1039G576X6Q0NgPPUrPwU7nnlW69Jh8GwWxDH404Ky70HAWrtB0JN035UxhHaOU8DxpZ0yrkyCJfpqDzTZfzJrRUzYlf6pV4CkzpvGdsSKnAt8wukrzCUT4oUzHpOXiVk8yOS-YhGIKtnBdxPNTehTPWSLbE7JaTpETcPHoaLYzMA9\" alt=\"\"\/><\/figure>\n\n\n\n<p>Leave the other settings as default and click on create a cluster.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/U3fZCjOoHo0zNIzq51JFlQNkmqbIb-EyRyQi0m71xsVgHaAehPPmeCNW3XckeEdzJi2VHa9Z-n4tuB9JBY6XprfVVJ5flNm1oRb9tRCRwsBY5adVcfF7EjFkIj2ONAsfEZmnCucrG_3sdUIJnbyBxiwqK66kfs6W8P7-qLEImWPxeg0M0TUmNNu3Rmec5_qp\" alt=\"\"\/><\/figure>\n\n\n\n<p>On successful creation, you will be navigated to the below screen.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/EUeSHJ5nuT5pfkFHAv3p4z8_yXoxNy9SMfDjAohQMWSPQrsJ11UpbLSbiP87wCtW3o-BdTN0HSJnaiGWpjK4Dnb-MITMssJYubVxo8mb7kpRurVQm_ouwYVOVQdU5zBL2mLxSFb4_wLgRqbdnbFA1vfHG0fpbAMo4Kik7zJ8WK7A4KbBrZWhoDra4nR0-w8g\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Enter a username and for the password, you can either create your own password or you can click on auto-generate secure password. Click on copy to copy the generated password. Store the password in a safe place.<\/p>\n\n\n\n<p>Click on Create User and on success, you will see the new user added.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/W7MNzYAn-j3tT8rN3RVqTW66sMTmGoEwgPnQi2ez9oCkQ2firJvuI3xdMuZLRK-snBYYY4slsI1QNKpive6K0xvf59pisnaRxpTsCh-Gxrc4-CkzbH-8WoPKg3_QRe64jj3IPTMBacKyRpDLmOmVHyQm2bbw0irNibnHsQCDmI4pGhhVfruW5eNrmOE9XkXo\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Scroll down and select my local environment. Enter the IP you require that should be able to access the cluster. Click on Add Entry.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/Lj_EVUi2vbK28_aBetwPC8G3u4xdbsoOdPKm7GcMjnKlrLwwDWpzH5bC26eCvCKCAYx7ZFgUeNDXF_IgGkZ_kZSWm72zJLUjKI6wegH_KorzpZ-UtpGUzA2Oh5CzL7q0rJ4svsiCZU2AJbwtnsF7PasNG7gHDr0-lAVDzn6i38Q0nykfoKObkYJHfTkAm0Zy\" alt=\"\"\/><\/figure>\n\n\n\n<p>Finally, scroll down and click on finish and close.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/x0FKyI51tvB_hvX3bV-HTh0uP2o_p0FBSwdaTL1dWisuoZgOj5RJIp1EV9UZ8dUbBEDtil_PvPpJAQHHB6pSWPE4pZyYDuDzcszx7bzRY4wKcC_yWQmfzhs91yW31H_emdV5V8sHVRL0lNFPJ6PdKx_wz7vipHmOp5ZAfYNGqt2gMy1RGP-VjpCD35Sup4bp\" alt=\"\"\/><\/figure>\n\n\n\n<p>You will see the new cluster created. Now, click on Connect.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/FsTUHsb3Es-fuTLD2U6vpdKOem5T0-cDsmrXnySBnnLNRRY_om5iNZx2_NIWU1G_4SSorALthhGd8Cl9nSP1hwWxKR_BCtIyYOHhos1gPP0r36dyIyI8xNAIQ2fdnSf8uj2N_9AMsWmI09OkGCPRPeS4F0C9Z9oYZIfbCtEeDdPUQOPx1QZx492hVfB2wfEl\" alt=\"\"\/><\/figure>\n\n\n\n<p>Choose Connect your application.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/nmrnLsYMfSIKJ4VlGp-t62xuhsvSg6qixFHe7ssGQVdSXJyTnxCt1X6O7i3JhuabS-fA4iV0xu76DAfFoZZ04DAlVgqZ7KH2PTSJNSXGLQvIRVDo7K1L_PbGs14VTet2Oxvi4Os6mkGqcOVdGyCZadA3ZHx7LNnQ-g5gsBOLJXkv1_5jTvZdJARGtYtQMAxJ\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p>Select the driver as Node.js and the version as required. Copy the connect string as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/_w30q6rK8zflEuPOhbNHHstrjgjLcI7U8Doj-SDt9bbkisnj1vZtC9usHyP4wJet-vzWjeiT2pYUYdvN5RHAGNt0ezT3oWTDQcHrr1kUpwrmI2lL7L1ndfs8MpYIXB0iBT1eAE6UeAyBcdAvUjPealONNUoY1fwsqrIZs8hIriYVVrww4L95Qd43Ubz7zURt\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Now, move back to the <em>server.js<\/em> file and alter the connection code with the string you copied above. Enter the username, password, cluster name, and database name for the defined variable.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/iZHmOYC3D9MWDk88q7vtCwRfhdKabIlKEH0RBZoxFKQS1ChjemaHV5pnPAR08GFwhmHbkhJcCQqnOVerd-Ua5nSqQPMJJMGkUaLhrotrcjWPTtNO2EaBQ--tAJv3IOdSsxyr0yvLZA0kQ8Ejsq14jrk5XKk5p1oWnFgK-hK5dBEHBAQPcHdCWhWvQ7UkkmBW\" alt=\"\"\/><\/figure>\n\n\n\n<p>Define variables for the username, password, cluster name, and database name as needed.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/6f0cd0bjWg8zdt7xU8Rk_zeJ6hqUv1x63Il7MK-MvxPLXetkDBq3lumnjq_kb-xdjKjB1hDxjPRbD0wMQPmqu2AqLruaak6rtIrvmWufQsf9oc9pPF4AHUXWtgUg3-RDDhPdOTWktYMRAXblVib7XbRfFKWXmxoBfulQegjtwgPAV_pqHOJ8pCeIWB3fRNUS\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Add the below code which will basically use the connection string above to connect to the database and on success, will show the result.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/yE6HdTe1tS-S2B-KmfBYAw77cSFMFCrnGu9Al-33wy6r_wUANJTvGZx3F-C7nd8Aq28UdhXU9dMHAih8Q_P0mK8c1utb22qQl07t_3PcowlIr9KUp_IBu5XK8OayfOXl07GeVJ3InAD0msaiXsf73ZPZJFz-5UJN3CGim6D1yapK0hISGrAmz9BbGrpP9PCl\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">We need to make use of a Router so we will use the below code to make it available for usage and then we will initialize the application to listen on port 3000.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/6HlFlZJgTylBm2R0_L-X7U67wRK5fwmSVoy2hbzg6OKD5cItoHVXOa6NhgdSQe9EhogSzFk073VDDJKf9udPsR9KTix1afERM9IzObsMFx-ma0GnCJY2NR7x_nMgsAu-aP2YMf--M6YlAgGwribXo00D90d-uOaKxD6DqQoin5lmiVHtgiRTH9l8opOOQyMk\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, create a <em>model.js<\/em> file. We need to create a schema in the database to be able to add values as per the schema.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/BVPiyaKNMuVUTMDi9_TOa6fXRhVxiFpGUzF4LS7WXtx5_6m6wr3pG47A4YNs2fM7og5horSCVYk0ldtADdSkWzuOc0O1jjaijsY6BeeWbFvjUx_Cbt6qELyshSZM0BMJooukHJ6K0JspGWt2le5RcQpFJydKw-BOFFupAcddAILCSyhtLtEy2p5VJbrroJat\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Add the below code to do so. We will first import the mongoose package and then define a schema with the required parameters and their corresponding types and whether they are required fields or not and the default value for the same. Then, we will export the schema for us to be able to use it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/sU88J_tspK1BbwQR6h050ne213GR4XQqv8wabV3aCQ8X3fE4SS6kPv0ih45DYZlycF2KPFrpWeZxoaVWk5O86CSi0YjNUp1knmbwJ1FktqiEdtkH2BsDMXSa-oqXSTVX-hJyPBdQmeWsXmu9G-Y5UlKxBQnDRY4e7fR_xdgphmpOQZhyEMhhJoy1kpX0PjBB\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">We need to define routes for the creation and get requests. To do so, create a new file <em>routes.js<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/EylV5UCYYukOoNfaPztwxf7jEDaAQvwOsgI_maQQmH_xXTXpw-s3NQQgX7t2ale7FCubktw5yVBsCNz62C2-IY0YmXXBy74PRBBCm4NfpM-IhcVAxKwvvtSf2cE3eEX_sRqD6QPyCD1cl2bOa7HNIzbrNtM7CtAVDOIHNjnh7uySR9Xa87VYzqNRqs31yTbv\" alt=\"\"\/><\/figure>\n\n\n\n<p>Import the following packages in the routes file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/2Q5Go-2otuJ3UU0-dPL57zd7twVYtI5gV5xTzfgjJYd2rP2nGeGiEwmmXrz21bxJftq0pIg1CPNg71SizK8uCUHPyIZ0GdDGLIJu6C4SuOY0oBf2y6Aq9xGR6EHND7Zxo2HIOaCZ0Of9R_nrvyPjV0JIH5NFEBVwXYt4QO0p8xXq7v1vIL0ynTk7Bv8dcEmQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Define a route for adding a new user to the list as shown in the image below making a post request and returning the result response.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/vFXsrm7TugG-KU5WOHEBExYdCAGyt1m2I0YRkW1KKeSFA6iWsizl4BP8x1FMqy3e1M2u6zm1TDr6HjjSpLmBIzjpF2IZztOxpoJBTRA9BuP8nxUr0LytYjFI-1_HhhEHBhFFG30x4J_TpaXMYGO3YnknATcWDQzwztZ24auzuDyhgiAA0Xwz2ex-JEsvjA_O\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Then, in the same file, define a route for fetching the created users using the get request thereby returning the result response.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/0cSyGk6b77YByjyEkDuryaQDWBWI0gh9odZklQrYJXVL8poEYyarF7Jsmi6cLFaRUef4wJ4izncTUs3dNZrsBqQmT6Mr8cOyYCE8xpJYzvyK1QEpyTG4axcgS7MZvmLL9d7IEDX2bfCTCo5LVu9T-3ccCWOSdnEjQ-mg5OQGj67aXBvmKlrsftQPIUtDNP4e\" alt=\"\"\/><\/figure>\n\n\n\n<p>Finally, export the route.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/zyiquXwNYLWVvWRUW75MW7NWtjBt6Em-qSCPXEC9hx0YCbp-EMAWSZBdEMbvisnTWbldXaBd3Wc0KkcoXPO4mrfPQwpl2BBS4jUf57bU8gZ702585Sryh8GePJxPh0Pl0Zixvl0fm3eh49_-uCm7TuMfgk8W_bsMtcUrZzrQ9qRI7ANfyVb2IImqld7WiEay\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, run the server using the below command:<\/p>\n\n\n\n<p><code>node server.js<\/code><\/p>\n\n\n\n<p>`<img loading=\"lazy\" src=\"https:\/\/lh5.googleusercontent.com\/6ZrcpQ46aigUzcwV9Y69Kam6Paw0q4JrI-6NATZWUaUn3CN18lBYy_Kuv0uZQPujhMjR4kz49LkKxDAsx7MfpzKYvG-xB1TBa1mq0Uykk40n9MDSYUKjkoZScljyoDoNypdNRk3E4LEibvMOPcHpzkhezl7FQ8gTUNcSfx-19aTXvkqxLgvBl7CdcJedmZ25\" width=\"135\" height=\"36\"><\/p>\n\n\n\n<p>On success, you will see that the server will be started on the 3000 port.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/rbBsIvZff692P8JvuHn6ylJ6pOj7sPNi3dYuNVn9Ye93sLzlkhtzIjXfdW52-yrMmKUQIpmVFNtgvvDNC92OPySVAF4yozzPd8bTe48uv3gZ3RSKMiXU8poj-jSFeBqcSHka4mYlhnd0QfEkMDRlqJGL0XvZsNbUBXzL65ZzTQFdRx6Gzi4nIs2drVhi9Ody\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">On successful connection to the database, you will see the success message as shown in the image below after some time.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/5FPKrqBSJymWhF8vRvMwO9nnqRFYkYUWwEwn91a7WPtAT8aciBoZKXNJCbtV66pXzdoqHyy-ZEZosdnB2qW-WjAyaSQoL-vc_aPYauW606RVsrcv0CItBR-nRrAC3_NmBH7Jm9X-BYZawCkJ2ttw36e_lWQUvWtn08VHBiLtLPVu6oIF7Fp2vlIgCNEuR0Qu\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, to test the API, open the postman application. Select the method as POST.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/Q18X-KoBJzs4-kf7FA1VMAqsp7oW6L6EuTH43mmr0QiOcYbpCHKGqL2u_S0tIdLuWQumEnnOWSEsnAxMd0mcizMdnFoVEgSQqWXivw3c_JcTwHIb6eUh7CA8R3ipuao80kZDsZh6LzNPDTf3gmHetitpi4NGP4levnTpCsXV_zB2q8bEmMGNWt01Xn2Cjzvy\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Enter the request URL as &#8216;http:\/\/localhost:3000\/add_user&#8217; and then select the Body tab. Under that select the raw section and the type as JSON. Create an object in the request body with the user name and age as per our schema. <\/p>\n\n\n\n<p class=\"has-text-align-justify\">Once done, hit Send.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/7Rw8nfDUE3AjrLLiL3ZXO0GsXsVud9ZLRSEDUaCPatDrgk4gi-YNSj7OdFiuJeCCRkjhh6NylZF7xj7r0b_pdDidprN35urmVozZebqUkRvgdhZ3_fZxfUeQH18L_r3yTqUPHe55czgFxMlFzenrb5QeZc6xNnrpZvjNnMU2-lmLyNZoljMcA81IawtnNozO\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">On successful creation, you will see the following message as shown in the image below with a randomly generated id for the same.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/O5xr5hankikslMGa_3wcwQQ16T2jLI5pLW2MNw7iyZDADrFQnu1gxDjsgtSzMrB6yi87bKQ77e9onaNa_1E1X6EBUuvtkk83kYscubZEyM4hQ2xLMKag13JDOiowMEZLO5ccSMi1JIwDOKScUDsje4hEM7gsC_Or6e7JQe3snESnLpnsghX5YCrZTLcA5-3X\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Now, let&#8217;s test the get API to get all the users. Change the request type to GET and hit the URL &#8216;<a href=\"http:\/\/localhost:3000\/users\">http:\/\/localhost:3000\/users<\/a>&#8216; and click on Send.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/1loP7_g30Te6rgP3sIGc8w5ycIy7_c34z8OnHhc--Wh1zAy3cYROVRlQCg-M3tGOoeLYQ8KWGSq7GKkNV-v8_nJSiRoU3M6RA2QnS3bL3FN5I9V0lpI2dSNl9e5PsBWooWZf-Ob-WmPq_Vp58e5u1YyMgDii1PF2Wo_U-e20CDvpdNJpzs2WGluD6YB5xM1G\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will get the list of created users.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/awbn8Av_KukPyackylYMs9kPQhPt0oyfmV9-OJXBL9D2dQoVJ0lQEqRvCNGVWkxJeMiEIWtePWxURZujA3qc4v3JphIw7L8Ry99znJPKtnesg4goQih4BYOUcC_EDjmGwAbt3rVZEzy5W_bAQZwAuGqfLWu0wF1NzISGIJvKOF4K_czgeTrze0VPV2SvY3tW\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Now, follow the same creation process as shown above and create one more user, and then hit the get API URL again. You will see that the response now returns both the created users.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/ehdzpLI8whYcqZ_H-dbxrr-Xr2WzR6hsrwgyFE-ZAHDPEJytJXbm_nstgtHx_ydoJRqLa9CL3MQNmyIqPeZWs-y4-ombBn1AXg2VSxQrJBaxBmDhAAaM3Y1Z0Pza0Z_dETtHsL3criTDWpcqeG5F6oJMeVzn5lXCWKDq-hEL4W1NgFxAI1hMm1CeVc59bKeA\" alt=\"connect to MongoDB using Mongoose and MongoDB Atlas in Node.js\"\/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we saw how we can connect to MongoDB using Mongoose and MongoDB Atlas in Node.js. We began with creating a new directory on the local machine. Initializing the app in the directory, we installed the required packages for the implementation.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Verifying the installation of the packages, we created a code file thereby putting in the code to connect to the database. Navigating to the MongoDB Atlas, we demonstrated how to sign up and create a new database.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">We then configured a new user and added the IP so that we can access it in our code. Altering the parameters in our code, we then moved on to creating a model which is a database schema. Exporting the schema we then created route files with the API calls to add a record in the database and to list all the inserted records.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">We then spun up the server and navigated to the postman application to hit the insert API. On successful execution, we ran the get API followed by testing out the same for multiple records. We will come up with more such use cases in our upcoming blogs.<\/p>\n\n\n\n<p><strong>Meanwhile\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">If you are an aspiring MongoDB Developer and want to explore more about the above topics, here are a few of our blogs for your reference:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-build-and-deploy-a-mern-stack-application-on-aws\/\">How to build and deploy a MERN Stack Application on AWS?<\/a><\/li><\/ul>\n\n\n\n<p>Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.<\/p>\n\n\n\n<p><strong>Keep Exploring -&gt; Keep Learning -&gt; Keep Mastering&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">At <a href=\"https:\/\/www.workfall.com\/\">Workfall<\/a>, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you\u2019re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at <a href=\"https:\/\/www.workfall.com\/partner\/\">workfall.com\/partner<\/a> today!<\/p>\n\n\n\n<p><\/p>\n\n\n<style type=\"text\/css\"><\/style><section id='' \n                class='helpie-faq accordions faq-toggle open-first groupSettings-515__enabled' \n                data-collection='' \n                data-pagination='0' \n                data-search='0' \n                data-pagination-enabled='0'\n                role='region'\n                aria-label='FAQ Section'\n                aria-live='polite'><h3 class=\"collection-title\">Frequently Asked Questions:<\/h3><article class=\"accordion \"><div class='helpie-faq-row'><div class='helpie-faq-col helpie-faq-col-12' ><ul><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2923\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2923\"\r\n                data-id=\"post-2923\" \r\n                data-item=\"hfaq-post-2923\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Why should I use Mongoose when I can use the native MongoDB driver?  Answer:<\/div><\/div><div id=\"accordion-content-post-2923\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2923\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0<\/span><b>A: <\/b><span style=\"font-weight: 400\">Mongoose provides a higher-level abstraction over the native driver. It supports schema definitions, validation, middleware (hooks), virtual fields, and easier model-based operations. If you prefer more structure and safer handling of data formats (e.g. ensuring required fields, type coercion, custom validations), Mongoose helps enforce that. But if you want maximum flexibility and minimal overhead, the native driver might suffice. <\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2924\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2924\"\r\n                data-id=\"post-2924\" \r\n                data-item=\"hfaq-post-2924\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How do I securely store my MongoDB connection URI and credentials?<\/div><\/div><div id=\"accordion-content-post-2924\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2924\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Never hardcode sensitive information in your codebase. Instead, use environment variables (e.g., via a <\/span><span style=\"font-weight: 400\">.env<\/span><span style=\"font-weight: 400\"> file) and tools like <\/span><span style=\"font-weight: 400\">dotenv<\/span><span style=\"font-weight: 400\"> or your cloud provider\u2019s secret management. Also, restrict network access on the MongoDB Atlas cluster (whitelist only required IPs or use VPC peering). Rotate credentials periodically and use roles with minimal privileges.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2925\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2925\"\r\n                data-id=\"post-2925\" \r\n                data-item=\"hfaq-post-2925\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What connection options should I include when calling mongoose.connect(...)?<\/div><\/div><div id=\"accordion-content-post-2925\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2925\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Some useful options include:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">useNewUrlParser: true<\/span><span style=\"font-weight: 400\"> and <\/span><span style=\"font-weight: 400\">useUnifiedTopology: true<\/span><span style=\"font-weight: 400\"> (to opt into newer Mongo driver behavior)<\/span>&nbsp;<\/li>\n<li><span style=\"font-weight: 400\">dbName<\/span><span style=\"font-weight: 400\"> to override default DB<\/span>&nbsp;<\/li>\n<li><span style=\"font-weight: 400\">autoIndex: false<\/span><span style=\"font-weight: 400\"> in production (to avoid performance issues building indexes automatically)<\/span>&nbsp;<\/li>\n<li><span style=\"font-weight: 400\">maxPoolSize<\/span><span style=\"font-weight: 400\">, <\/span><span style=\"font-weight: 400\">minPoolSize<\/span><span style=\"font-weight: 400\"> to tune pooling<\/span>&nbsp;<\/li>\n<li><span style=\"font-weight: 400\">serverSelectionTimeoutMS<\/span><span style=\"font-weight: 400\"> to limit how long it tries to find a server<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> Also, Mongoose buffers queries until the connection is ready by default. You can disable buffering (via <\/span><span style=\"font-weight: 400\">bufferCommands:false<\/span><span style=\"font-weight: 400\">) if needed.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><\/li>\n<\/ul>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2926\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2926\"\r\n                data-id=\"post-2926\" \r\n                data-item=\"hfaq-post-2926\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What errors might occur during connection, and how to debug them?<\/div><\/div><div id=\"accordion-content-post-2926\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2926\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Common errors include authentication failures (wrong username\/password), \u201cNetworkTimeout\u201d if Atlas is unreachable, \u201cServer selection timeout\u201d if the cluster can\u2019t be selected, or DNS\/hostname resolution errors. To debug, ensure your connection string is correct, credentials are valid, IP whitelist is configured in Atlas, and network connectivity (firewalls) is open. Logging the error object (e.g. in the <\/span><span style=\"font-weight: 400\">catch<\/span><span style=\"font-weight: 400\">) often gives a helpful message. Also, listening to connection events (error, disconnected, reconnected) on <\/span><span style=\"font-weight: 400\">mongoose.connection<\/span><span style=\"font-weight: 400\"> helps track status.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2927\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2927\"\r\n                data-id=\"post-2927\" \r\n                data-item=\"hfaq-post-2927\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. If my application uses multiple databases or tenants, how can I manage multiple Mongoose connections?<\/div><\/div><div id=\"accordion-content-post-2927\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2927\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><b>A: <\/b><span style=\"font-weight: 400\">You can use <\/span><span style=\"font-weight: 400\">mongoose.createConnection(&#8230;)<\/span><span style=\"font-weight: 400\"> to create separate connection instances (each with its own models). Another pattern is using <\/span><span style=\"font-weight: 400\">connection.useDb()<\/span><span style=\"font-weight: 400\"> to switch databases on the same connection, optionally with caching. Be careful about open connection limits (especially on Atlas). Use the \u201cschema export and model initialization per connection\u201d pattern to avoid collision.<\/span><\/p>\n<\/div><\/li><\/ul><\/div><\/div><\/article><\/section>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">10<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span> MongoDB is one of the most popular No-SQL databases in the developer community today. Instead of SQL objects, No-SQL databases allow developers to send and retrieve data as JSON documents. In this blog, we will demonstrate how to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js. Let\u2019s get started! In this blog, we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1523,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[288],"tags":[85,27,253,366,365,265,250,221,273,6],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js? - The Workfall Blog<\/title>\n<meta name=\"description\" content=\"MongoDB is one of the most popular No-SQL databases in the developer community. It allows developers to send &amp; retrieve data as JSON docs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js? - The Workfall Blog\" \/>\n<meta property=\"og:description\" content=\"MongoDB is one of the most popular No-SQL databases in the developer community. It allows developers to send &amp; retrieve data as JSON docs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/\" \/>\n<meta property=\"og:site_name\" content=\"The Workfall Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/workfall\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-29T10:18:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-24T09:47:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@workfall\" \/>\n<meta name=\"twitter:site\" content=\"@workfall\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Workfall\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\",\"name\":\"Workfall - Hire #Kickass Coders On Demand\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/workfall\/\",\"https:\/\/www.linkedin.com\/company\/workfall\/\",\"https:\/\/facebook.com\/workfall\",\"https:\/\/twitter.com\/workfall\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"contentUrl\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"width\":400,\"height\":400,\"caption\":\"Workfall - Hire #Kickass Coders On Demand\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"name\":\"The Workfall Blog\",\"description\":\"#Tech #Remote #Jobs\",\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#primaryimage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png\",\"width\":1200,\"height\":628,\"caption\":\"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#webpage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/\",\"name\":\"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js? - The Workfall Blog\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#primaryimage\"},\"datePublished\":\"2022-11-29T10:18:15+00:00\",\"dateModified\":\"2025-09-24T09:47:59+00:00\",\"description\":\"MongoDB is one of the most popular No-SQL databases in the developer community. It allows developers to send & retrieve data as JSON docs.\",\"breadcrumb\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learning.workfall.com\/learning\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#webpage\"},\"author\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\"},\"headline\":\"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?\",\"datePublished\":\"2022-11-29T10:18:15+00:00\",\"dateModified\":\"2025-09-24T09:47:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#webpage\"},\"wordCount\":1817,\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png\",\"keywords\":[\"api\",\"Database\",\"MongoDB\",\"mongoDBAtlas\",\"mongoose\",\"node\",\"nodeJS\",\"NoSQL\",\"sql\",\"workfall\"],\"articleSection\":[\"Backend Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\",\"name\":\"Workfall\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"caption\":\"Workfall\"},\"sameAs\":[\"https:\/\/www.workfall.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js? - The Workfall Blog","description":"MongoDB is one of the most popular No-SQL databases in the developer community. It allows developers to send & retrieve data as JSON docs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/","og_locale":"en_US","og_type":"article","og_title":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js? - The Workfall Blog","og_description":"MongoDB is one of the most popular No-SQL databases in the developer community. It allows developers to send & retrieve data as JSON docs.","og_url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/","og_site_name":"The Workfall Blog","article_publisher":"https:\/\/facebook.com\/workfall","article_published_time":"2022-11-29T10:18:15+00:00","article_modified_time":"2025-09-24T09:47:59+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@workfall","twitter_site":"@workfall","twitter_misc":{"Written by":"Workfall","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization","name":"Workfall - Hire #Kickass Coders On Demand","url":"https:\/\/learning.workfall.com\/learning\/blog\/","sameAs":["https:\/\/www.instagram.com\/workfall\/","https:\/\/www.linkedin.com\/company\/workfall\/","https:\/\/facebook.com\/workfall","https:\/\/twitter.com\/workfall"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","contentUrl":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","width":400,"height":400,"caption":"Workfall - Hire #Kickass Coders On Demand"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website","url":"https:\/\/learning.workfall.com\/learning\/blog\/","name":"The Workfall Blog","description":"#Tech #Remote #Jobs","publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#primaryimage","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png","width":1200,"height":628,"caption":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?"},{"@type":"WebPage","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#webpage","url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/","name":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js? - The Workfall Blog","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#primaryimage"},"datePublished":"2022-11-29T10:18:15+00:00","dateModified":"2025-09-24T09:47:59+00:00","description":"MongoDB is one of the most popular No-SQL databases in the developer community. It allows developers to send & retrieve data as JSON docs.","breadcrumb":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learning.workfall.com\/learning\/blog\/"},{"@type":"ListItem","position":2,"name":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?"}]},{"@type":"Article","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#article","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#webpage"},"author":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a"},"headline":"How to connect to MongoDB using Mongoose and MongoDB Atlas in Node.js?","datePublished":"2022-11-29T10:18:15+00:00","dateModified":"2025-09-24T09:47:59+00:00","mainEntityOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#webpage"},"wordCount":1817,"publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mongodb-using-mongoose-and-mongodb-atlas-in-node-js\/#primaryimage"},"thumbnailUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png","keywords":["api","Database","MongoDB","mongoDBAtlas","mongoose","node","nodeJS","NoSQL","sql","workfall"],"articleSection":["Backend Development"],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a","name":"Workfall","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","caption":"Workfall"},"sameAs":["https:\/\/www.workfall.com"]}]}},"jetpack_featured_media_url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1-2.png","jetpack-related-posts":[{"id":226,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-build-and-deploy-a-mern-stack-application-on-aws\/","url_meta":{"origin":1522,"position":0},"title":"How to build and deploy a MERN Stack Application on AWS?","date":"October 27, 2021","format":false,"excerpt":"Do you have a wonderful product idea that keeps springing into your head? However, not sure which technology or framework to adopt for easier and faster deployment of scalable web applications, you can go for MERN Stack.\u00a0 A MERN Stack is a collection of front-end, back-end, and database components that\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Build and Deploy a MERN Stack Application on AWS","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/10\/MERN.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":647,"url":"https:\/\/learning.workfall.com\/learning\/blog\/store-query-and-index-json-data-using-aws-documentdb\/","url_meta":{"origin":1522,"position":1},"title":"How to store, query, and index JSON data using AWS DocumentDB?","date":"November 11, 2021","format":false,"excerpt":"NoSQL databases are a great choice for many modern applications such as mobile, web, and gaming that require flexible, scalable, high-performance, and highly functional databases to provide great user experiences. In this blog, we will discuss AWS DocumentDB followed by the implementation to connect to the Amazon DocumentDB cluster from\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"AWS DocumentDB and JSON Data - Workfall","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/11\/DocumentDB01.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":198,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-set-up-a-document-database-with-amazon-documentdb-with-mongodb-compatibility-and-aws-cloud9-in-a-private-vpc\/","url_meta":{"origin":1522,"position":2},"title":"How to set up a Document Database with Amazon DocumentDB (with MongoDB compatibility) and AWS Cloud9 in a private VPC?","date":"October 27, 2021","format":false,"excerpt":"Are you utterly exhausted from thinking about your storage of data? You can get a fast, scalable, highly available, and fully managed document database service that supports MongoDB workloads and makes it easy to store, query, and index JSON data with Amazon DocumentDB (with MongoDB compatibility). In this blog, we\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Set up a Document Database with Amazon DocumentDB (with MongoDB compatibility) and AWS Cloud9 in a private VPC","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/10\/DocumentDB-1200-x-628-px.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1684,"url":"https:\/\/learning.workfall.com\/learning\/blog\/building-real-time-apps-with-nestjs-and-graphql-subscriptions\/","url_meta":{"origin":1522,"position":3},"title":"Building Real-Time Apps with NestJS and GraphQL Subscriptions","date":"April 6, 2023","format":false,"excerpt":"Real-time applications are important in several instances. Especially in a scenario whereby immediate feedback is important such as messaging apps and IoT apps. Let\u2019s imagine a case in IoT whereby a smoke detector needs to relay information to water sprinklers in a burning building. This information has to be in\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Building Real-Time Apps with NestJS and GraphQL Subscriptions","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/04\/Cover-Images_Part2-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":863,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-manage-backups-for-amazon-documentdb-cluster-with-mongodb-compatibility-using-aws-backup\/","url_meta":{"origin":1522,"position":4},"title":"How to manage backups for Amazon DocumentDB cluster (with MongoDB compatibility) using AWS Backup?","date":"January 25, 2022","format":false,"excerpt":"Amazon DocumentDB (with MongoDB compatibility) cluster backups are now supported by AWS Backup, expanding the list of AWS features that are supported across compute, storage, and database. With this release, you'll be able to maintain and restore backups for the Amazon DocumentDB database clusters via the AWS Backup service all\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Manage backups for Amazon DocumentDB cluster (with MongoDB compatibility) using AWS Backup","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/01\/Cover-Images_Part2-2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1525,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-configure-cors-in-node-js-with-express\/","url_meta":{"origin":1522,"position":5},"title":"How to Configure CORS in Node.js With Express?","date":"December 6, 2022","format":false,"excerpt":"Internet browsers typically deny access to unknown websites from your application programming interfaces and services. Doing this allows your server to share its resources only with clients that are on the same domain as yours and nobody else. However, there are times when you would like to relax this guard\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"How to Configure CORS in Node.js With Express?","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1522"}],"collection":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/comments?post=1522"}],"version-history":[{"count":4,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1522\/revisions"}],"predecessor-version":[{"id":2929,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1522\/revisions\/2929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media\/1523"}],"wp:attachment":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media?parent=1522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/categories?post=1522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/tags?post=1522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}