{"activeVersionTag":"latest","latestAvailableVersionTag":"latest","collection":{"info":{"_postman_id":"8f86f5c7-545b-4076-beca-40fbd911782c","name":"MongoDB CRUD API w Node n Express","description":"**Beginner's Guide to Building a RESTful API with Node.js, Express, and MongoDB**  \n**Introduction:**\n\nThis contains a beginner-friendly guide and implementation of a RESTful API built with Node.js,  \nExpress, and MongoDB. The API allows you to manage data, including creating,  \nreading, updating, and deleting records.\n\n**JavaScript:**  \nJavaScript is a versatile programming language primarily used for web  \ndevelopment. It enables interactive web pages and is essential for building  \ndynamic and responsive web applications.\n\n**Node.js:**  \nNode.js is a runtime environment that allows you to run JavaScript code outside  \nof a web browser. It uses an event-driven, non-blocking I/O model, making it  \nlightweight and efficient for building scalable network applications.\n\n**Express.js:**  \nExpress.js is a minimal and flexible Node.js web application framework that  \nprovides a robust set of features for building web and mobile applications. It  \nsimplifies the process of creating APIs and handling HTTP requests.\n\n**MongoDB:**  \nMongoDB is a NoSQL database that stores data in flexible, JSON-like documents.  \nIt is designed for scalability, high availability, and performance, and it  \nfacilitates rapid development and iteration of database schemas.\n\n**RESTful API:**  \nRESTful API (Representational State Transfer) is an architectural style for  \ndesigning networked applications. It uses standard HTTP methods (GET, POST,  \nPUT, DELETE) to perform CRUD (Create, Read, Update, Delete) operations on  \nresources. RESTful APIs are widely used for building web services and  \ninteracting with data-driven applications.\n\n**Prerequisites**\n\nBasic knowledge of JavaScript, Node.js,  \nand MongoDB\n\nInstalled software: [Node.js](https://nodejs.org/en/download/package-manager) ,  [&nbsp;MongoDB](https://www.mongodb.com/try/download/community) , [Postman](https://www.postman.com/downloads/) ( click the text for download )  \nIDE: Visual Studio ( as per your choice)\n\n**Setup:**\n\nSetting up the project directory\n\nInitializing npm and installing  \ndependencies, open Terminal:\n\nnpm init -y\n\nnpm install express cors mongodb\n\nconst express = require('express');  \nconst cors = require('cors');  \nconst bodyParser = require('body-parser');  \nconst { MongoClient, ObjectId } = require('mongodb');\n\nconst app = express();  \napp.use(cors());  \napp.use(bodyParser.json());\n\nconst port = 1100; // Change port if it's already in use  \n//Sample Web- [http://localhost:1100/get](http://localhost:1100/get)  \n//[http://localhost:portnumber/](http://localhost:portnumber/endpoint)  \nDatabase creation in MongoDB\n\nOn the Command Prompt or any terminal add these commands:\n\nmongod\n\nmongosh\n\nFor creating new database  \nand collection in MongoDB:\n\nuse playerDB _//i named my database name as playerDB you can name it whatever__you want_\n\nNow you have to create  \na collection in this database:\n\ndb.createCollection('playerCollection') _// name__it as per your choice_\n\n// MongoDB connection  \nconst uri = 'mongodb://localhost:27017';  \nconst client = new MongoClient(uri);\n\n// Connect to MongoDB  \nasync function connectToMongoDB() {  \ntry {  \nawait client.connect();  \nconsole.log('Connected to MongoDB');  \n} catch (err) {  \nconsole.error('Failed to connect to MongoDB', err);  \nprocess.exit(1);  \n}  \n}\n\nconnectToMongoDB(); // Must call this function\n\nconst database = client.db('playerDB'); // As per database name  \nconst collection = database.collection('playerCollection'); // As per collection name\n\napp.listen(port, () =>  \nconsole.log(`Server running on ${port}`)  \n);\n\n//Sample MongoDB Data\n\n// db.playerCollection.insertOne({ name: 'Neymar Jr', position: 'LW', number: 10 })  \n// db.playerCollection.insertOne({ name: 'Lionel Messi', position: 'RW', number: 10 })  \n// db.playerCollection.insertOne({ name: 'Cristiano Ronaldo', position: 'ST', number: 7 })  \n// db.playerCollection.insertOne({ name: 'Sergio Ramos', position: 'CB', number: 4 })  \n// db.playerCollection.insertOne({ name: 'Toni Kross', position: 'CM', number: 8 })\n\n// MongoDB Command  \n// test> use playerDB  \n// playerDB> db.createCollection('playerCollection')\n\n<u>Connect with me:</u>\n\n**Abtahi Md. Mahib Uddin**\n\n[GitHub](https://github.com/abtaaahi/) || [Portfolio](https://abtaaahi.github.io/portfolio/) || [LinkedIn ](https://www.linkedin.com/in/abtahi-md-mahib-uddin-6739aa236/) || [Email ](https://mailto:abtahimahib@gmail.com) || [Facebook](https://www.facebook.com/abtaaahi/)","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","isPublicCollection":false,"owner":"36920253","collectionId":"8f86f5c7-545b-4076-beca-40fbd911782c","publishedId":"2sA3kPo46s","public":true,"publicUrl":"https://documenter-api.postman.tech/view/36920253/2sA3kPo46s","privateUrl":"https://go.postman.co/documentation/36920253-8f86f5c7-545b-4076-beca-40fbd911782c","customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"FF6C37"},"documentationLayout":"classic-double-column","customisation":{"metaTags":[{"name":"description","value":"This documentation contains a beginner-friendly guide and implementation of a RESTful API built with Node.js, Express, and MongoDB. The API allows you to manage data, including creating, reading, updating, and deleting records."},{"name":"title","value":""}],"appearance":{"default":"dark","themes":[{"name":"dark","logo":null,"colors":{"top-bar":"212121","right-sidebar":"303030","highlight":"FF6C37"}},{"name":"light","logo":null,"colors":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"FF6C37"}}]}},"version":"8.10.0","publishDate":"2024-07-14T11:02:10.000Z","activeVersionTag":"latest","documentationTheme":"light","metaTags":{"title":"","description":"This documentation contains a beginner-friendly guide and implementation of a RESTful API built with Node.js, Express, and MongoDB. The API allows you to manage data, including creating, reading, updating, and deleting records."},"logos":{"logoLight":null,"logoDark":null}},"statusCode":200},"environments":[],"user":{"authenticated":false,"permissions":{"publish":false}},"run":{"button":{"js":"https://run.pstmn.io/button.js","css":"https://run.pstmn.io/button.css"}},"web":"https://www.getpostman.com/","team":{"logo":"https://res.cloudinary.com/postman/image/upload/t_team_logo_pubdoc/v1/team/768118b36f06c94b0306958b980558e6915839447e859fe16906e29d683976f0","favicon":""},"isEnvFetchError":false,"languages":"[{\"key\":\"csharp\",\"label\":\"C#\",\"variant\":\"HttpClient\"},{\"key\":\"csharp\",\"label\":\"C#\",\"variant\":\"RestSharp\"},{\"key\":\"curl\",\"label\":\"cURL\",\"variant\":\"cURL\"},{\"key\":\"dart\",\"label\":\"Dart\",\"variant\":\"http\"},{\"key\":\"go\",\"label\":\"Go\",\"variant\":\"Native\"},{\"key\":\"http\",\"label\":\"HTTP\",\"variant\":\"HTTP\"},{\"key\":\"java\",\"label\":\"Java\",\"variant\":\"OkHttp\"},{\"key\":\"java\",\"label\":\"Java\",\"variant\":\"Unirest\"},{\"key\":\"javascript\",\"label\":\"JavaScript\",\"variant\":\"Fetch\"},{\"key\":\"javascript\",\"label\":\"JavaScript\",\"variant\":\"jQuery\"},{\"key\":\"javascript\",\"label\":\"JavaScript\",\"variant\":\"XHR\"},{\"key\":\"c\",\"label\":\"C\",\"variant\":\"libcurl\"},{\"key\":\"nodejs\",\"label\":\"NodeJs\",\"variant\":\"Axios\"},{\"key\":\"nodejs\",\"label\":\"NodeJs\",\"variant\":\"Native\"},{\"key\":\"nodejs\",\"label\":\"NodeJs\",\"variant\":\"Request\"},{\"key\":\"nodejs\",\"label\":\"NodeJs\",\"variant\":\"Unirest\"},{\"key\":\"objective-c\",\"label\":\"Objective-C\",\"variant\":\"NSURLSession\"},{\"key\":\"ocaml\",\"label\":\"OCaml\",\"variant\":\"Cohttp\"},{\"key\":\"php\",\"label\":\"PHP\",\"variant\":\"cURL\"},{\"key\":\"php\",\"label\":\"PHP\",\"variant\":\"Guzzle\"},{\"key\":\"php\",\"label\":\"PHP\",\"variant\":\"HTTP_Request2\"},{\"key\":\"php\",\"label\":\"PHP\",\"variant\":\"pecl_http\"},{\"key\":\"powershell\",\"label\":\"PowerShell\",\"variant\":\"RestMethod\"},{\"key\":\"python\",\"label\":\"Python\",\"variant\":\"http.client\"},{\"key\":\"python\",\"label\":\"Python\",\"variant\":\"Requests\"},{\"key\":\"r\",\"label\":\"R\",\"variant\":\"httr\"},{\"key\":\"r\",\"label\":\"R\",\"variant\":\"RCurl\"},{\"key\":\"ruby\",\"label\":\"Ruby\",\"variant\":\"Net::HTTP\"},{\"key\":\"shell\",\"label\":\"Shell\",\"variant\":\"Httpie\"},{\"key\":\"shell\",\"label\":\"Shell\",\"variant\":\"wget\"},{\"key\":\"swift\",\"label\":\"Swift\",\"variant\":\"URLSession\"}]","languageSettings":[{"key":"csharp","label":"C#","variant":"HttpClient"},{"key":"csharp","label":"C#","variant":"RestSharp"},{"key":"curl","label":"cURL","variant":"cURL"},{"key":"dart","label":"Dart","variant":"http"},{"key":"go","label":"Go","variant":"Native"},{"key":"http","label":"HTTP","variant":"HTTP"},{"key":"java","label":"Java","variant":"OkHttp"},{"key":"java","label":"Java","variant":"Unirest"},{"key":"javascript","label":"JavaScript","variant":"Fetch"},{"key":"javascript","label":"JavaScript","variant":"jQuery"},{"key":"javascript","label":"JavaScript","variant":"XHR"},{"key":"c","label":"C","variant":"libcurl"},{"key":"nodejs","label":"NodeJs","variant":"Axios"},{"key":"nodejs","label":"NodeJs","variant":"Native"},{"key":"nodejs","label":"NodeJs","variant":"Request"},{"key":"nodejs","label":"NodeJs","variant":"Unirest"},{"key":"objective-c","label":"Objective-C","variant":"NSURLSession"},{"key":"ocaml","label":"OCaml","variant":"Cohttp"},{"key":"php","label":"PHP","variant":"cURL"},{"key":"php","label":"PHP","variant":"Guzzle"},{"key":"php","label":"PHP","variant":"HTTP_Request2"},{"key":"php","label":"PHP","variant":"pecl_http"},{"key":"powershell","label":"PowerShell","variant":"RestMethod"},{"key":"python","label":"Python","variant":"http.client"},{"key":"python","label":"Python","variant":"Requests"},{"key":"r","label":"R","variant":"httr"},{"key":"r","label":"R","variant":"RCurl"},{"key":"ruby","label":"Ruby","variant":"Net::HTTP"},{"key":"shell","label":"Shell","variant":"Httpie"},{"key":"shell","label":"Shell","variant":"wget"},{"key":"swift","label":"Swift","variant":"URLSession"}],"languageOptions":[{"label":"C# - HttpClient","value":"csharp - HttpClient - C#"},{"label":"C# - RestSharp","value":"csharp - RestSharp - C#"},{"label":"cURL - cURL","value":"curl - cURL - cURL"},{"label":"Dart - http","value":"dart - http - Dart"},{"label":"Go - Native","value":"go - Native - Go"},{"label":"HTTP - HTTP","value":"http - HTTP - HTTP"},{"label":"Java - OkHttp","value":"java - OkHttp - Java"},{"label":"Java - Unirest","value":"java - Unirest - Java"},{"label":"JavaScript - Fetch","value":"javascript - Fetch - JavaScript"},{"label":"JavaScript - jQuery","value":"javascript - jQuery - JavaScript"},{"label":"JavaScript - XHR","value":"javascript - XHR - JavaScript"},{"label":"C - libcurl","value":"c - libcurl - C"},{"label":"NodeJs - Axios","value":"nodejs - Axios - NodeJs"},{"label":"NodeJs - Native","value":"nodejs - Native - NodeJs"},{"label":"NodeJs - Request","value":"nodejs - Request - NodeJs"},{"label":"NodeJs - Unirest","value":"nodejs - Unirest - NodeJs"},{"label":"Objective-C - NSURLSession","value":"objective-c - NSURLSession - Objective-C"},{"label":"OCaml - Cohttp","value":"ocaml - Cohttp - OCaml"},{"label":"PHP - cURL","value":"php - cURL - PHP"},{"label":"PHP - Guzzle","value":"php - Guzzle - PHP"},{"label":"PHP - HTTP_Request2","value":"php - HTTP_Request2 - PHP"},{"label":"PHP - pecl_http","value":"php - pecl_http - PHP"},{"label":"PowerShell - RestMethod","value":"powershell - RestMethod - PowerShell"},{"label":"Python - http.client","value":"python - http.client - Python"},{"label":"Python - Requests","value":"python - Requests - Python"},{"label":"R - httr","value":"r - httr - R"},{"label":"R - RCurl","value":"r - RCurl - R"},{"label":"Ruby - Net::HTTP","value":"ruby - Net::HTTP - Ruby"},{"label":"Shell - Httpie","value":"shell - Httpie - Shell"},{"label":"Shell - wget","value":"shell - wget - Shell"},{"label":"Swift - URLSession","value":"swift - URLSession - Swift"}],"layoutOptions":[{"value":"classic-single-column","label":"Single Column"},{"value":"classic-double-column","label":"Double Column"}],"versionOptions":[],"environmentOptions":[{"value":"0","label":"No Environment"}],"canonicalUrl":"https://documenter.gw.postman.com/view/metadata/2sA3kPo46s"}