{"id":1021,"date":"2021-02-03T18:51:48","date_gmt":"2021-02-03T18:51:48","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1021"},"modified":"2021-08-02T11:11:50","modified_gmt":"2021-08-02T11:11:50","slug":"mongoose-express-crud","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/javascript\/mongoose-express-crud\/","title":{"rendered":"Mongoose Express CRUD"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>const mongoose = require(\"mongoose\");\nconst conn_str = \"mongodb:\/\/localhost:27017\/tcet\"\nmongoose.connect(conn_str, { useNewUrlParser: true , useUnifiedTopology: true})\n\t.then( () =&gt; console.log(\"Connected successfully...\") )\n\t.catch( (err) =&gt; console.log(err) );\n\nconst userSchema = new mongoose.Schema({\n\tname: String,\n\tage: Number,\n\tcity: String,\n\tstate: String\n});\n\nconst user = new mongoose.model(\"user\", userSchema);\n\n\/** Express Mongoose Integration **\/\n\nconst express = require(\"express\");\nconst app = express();\n\napp.route(\"\/user\")\n.get(async (req, res) =&gt; {\n\tlet data = await user.find();\n\tres.send(data);\n})\n.post(async (req, res) =&gt; {\n\treq_data = req.query;\n\tlet obj = new user(req.query)\n\tlet result = await obj.save();\n\tres.send(result);\n})\n.put(async (req, res) =&gt; {\n\treq_data = req.query;\n\tlet result = await user.updateOne({_id: req.query.id}, {$set : {city: req.query.city}})\n\tres.send(result);\n})\n.delete(async (req, res) =&gt; {\n\tlet result = await user.deleteOne({_id: req.query.id})\n\tres.send(result);\n})\n\napp.listen(8080, () =&gt; {\n\tconsole.log(\"listening 8080...\");\n});<\/code><\/pre>\n\n\n\n<p>Connect to MongoDB Atlas Cluster<\/p>\n\n\n\n<p>Whitelist your ip \/ or allow from all 0.0.0.0\/0<\/p>\n\n\n\n<p>Get Your Current IP Address from google &gt; what is my ip<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Atlas &gt; Security &gt; Network Access &gt; ADD IP ADDRESS<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-10.png\"><img loading=\"lazy\" width=\"1024\" height=\"444\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-10-1024x444.png\" alt=\"\" class=\"wp-image-1923\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-10-1024x444.png 1024w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-10-300x130.png 300w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-10-768x333.png 768w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-10.png 1034w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>First get the connection string or URI from Atlas<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Atlas &gt; Deployment &gt; Databases &gt; Connect &gt; Connect your application &gt; Driver Node.js &gt; Version 2.2.12 or later<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-9.png\"><img loading=\"lazy\" width=\"841\" height=\"453\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-9.png\" alt=\"\" class=\"wp-image-1920\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-9.png 841w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-9-300x162.png 300w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-9-768x414.png 768w\" sizes=\"(max-width: 841px) 100vw, 841px\" \/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>const port = 8080;\nconst mongoose = require(\"mongoose\");\nconst uri =\"<strong>get_uri_from_atlas<\/strong>\"\nmongoose.connect(uri, { useNewUrlParser: true , useUnifiedTopology: true})\n\t.then( () =&gt; console.log(\"Connected successfully...\") )\n\t.catch( (err) =&gt; console.log(err) );\n\nconst userSchema = new mongoose.Schema({\n\tname: String,\n\tage: Number,\n\tcity: String\n});\n\nconst user = new mongoose.model(\"users\", userSchema);\n\n\/** Express Mongoose Integration **\/\n\nconst express = require(\"express\");\nconst app = express();\n\napp.route(\"\/user\")\n.get(async (req, res) =&gt; {\n\tlet data = await user.find();\n\tres.send(data);\n})\n.post(async (req, res) =&gt; {\n\treq_data = req.query;\n\tlet obj = new user(req.query)\n\tlet result = await obj.save();\n\tres.send(result);\n})\n.put(async (req, res) =&gt; {\n\treq_data = req.query;\n\tlet result = await user.updateOne({_id: req.query.id}, {$set : {city: req.query.city}})\n\tres.send(result);\n})\n.delete(async (req, res) =&gt; {\n\tlet result = await user.deleteOne({_id: req.query.id})\n\tres.send(result);\n})\n\napp.listen(process.env.PORT || port, () =&gt; {\n\tconsole.log(\"listening 8080...\");\n});<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connect to MongoDB Atlas Cluster Whitelist your ip \/ or allow from all 0.0.0.0\/0 Get Your Current IP Address from google &gt; what is my ip First get the connection string or URI from Atlas<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1021"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=1021"}],"version-history":[{"count":7,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1021\/revisions"}],"predecessor-version":[{"id":1953,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1021\/revisions\/1953"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}