{"id":1006,"date":"2021-02-02T16:52:28","date_gmt":"2021-02-02T16:52:28","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1006"},"modified":"2021-02-03T18:49:58","modified_gmt":"2021-02-03T18:49:58","slug":"mongoose-cheat-sheet","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/javascript\/mongoose-cheat-sheet\/","title":{"rendered":"mongoose cheat sheet"},"content":{"rendered":"\n<p><strong>Insert<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(\"mongo db connection\");\n\nconst mongoose = require(\"mongoose\");\n\nconst conn_str = \"mongodb:\/\/localhost:27017\/tcet\";\nmongoose.connect(conn_str, { useNewUrlParser: true, useUnifiedTopology: true })\n\t.then( () => console.log(\"Connected Successfully...\"))\n\t.catch( (error) => console.log(error));\n\t\n\/\/schema defining for collection\nconst teacherSchema = new mongoose.Schema({\n\tname: {\n\t\ttype: String,\n\t\trequired: true\n\t},\n\tage: Number,\n\tcity: String\n})\n\n\/\/ create collection\nconst Teacher = new mongoose.model(\"Teacher\", teacherSchema);\nconsole.log(Teacher);\n\t\n\/*insertOne\nconst t1 = new Teacher({name: \"Shailesh\", age: 32, city: \"Nagpur\"});\nconsole.log(t1.save());\n\n\n\/\/insertOne\nconst insertOne = async () => {\n\ttry {\n\t\tconst t2 = new Teacher({name: \"Amy\", age: 32, city: \"Nagpur\"});\n\t\tconst result = await t2.save();\n\t\tconsole.log(result);\n\t} catch(e) {\n\t\tconsole.log(e);\n\t}\n};\n\ninsertOne();\n\n\n\/\/insertMany\nconst insertMany = async () => {\n\ttry {\n\t\tconst t1 = new Teacher({name: \"Amy1\", age: 32, city: \"Nagpur\"});\n\t\tconst t2 = new Teacher({name: \"Amy2\", age: 32, city: \"Nagpur\"});\n\t\tconst t3 = new Teacher({name: \"Amy3\", age: 32, city: \"Nagpur\"});\n\t\tconst result = await Teacher.insertMany(&#91;t1,t2,t3]);\n\t\tconsole.log(result);\n\t} catch(e) {\n\t\tconsole.log(e);\n\t}\n};\n\ninsertMany();\n*\/<\/code><\/pre>\n\n\n\n<p><strong>Connection Module<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const mongoose = require(\"mongoose\");\n\nconst conn_str = \"mongodb:\/\/localhost:27017\/tcet\";\nmongoose.connect(conn_str, { useNewUrlParser: true, useUnifiedTopology: true })\n\t.then( () => console.log(\"Connected Successfully...\"))\n\t.catch( (error) => console.log(error));\n\t\n\/\/schema defining for collection\nconst teacherSchema = new mongoose.Schema({\n\tname: {\n\t\ttype: String,\n\t\trequired: true\n\t},\n\tage: Number,\n\tcity: String\n})\n\n\/\/ create collection\nconst Teacher = new mongoose.model(\"Teacher\", teacherSchema);\n\nexports.Teacher = Teacher;<\/code><\/pre>\n\n\n\n<p><strong>Read<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var teacher = require(\".\/teacher\");\nconst Teacher = teacher.Teacher;\n\n\/*\nconst t1 = new Teacher({name: \"Penny\", age: 25, city: \"Nagpur\"});\nconsole.log(t1.save());\n*\/\n\nconst getData = async () => {\nconst data = await Teacher.find({name: \/l\/i}).select({_id: 0, __v: 0}).limit(2).sort({name: -1});\n\tconsole.log(data);\n}\n\ngetData();\n<\/code><\/pre>\n\n\n\n<p><strong>Update<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var teacher = require(\".\/teacher\");\nconst Teacher = teacher.Teacher;\n\n\/*\nconst updateOne = async (oname, nname) => {\n\ttry {\n\t\tvar res = await Teacher.updateOne({name: oname}, {\n\t\t\t$set : { name : nname }\n\t\t});\n\t\tconsole.log(res);\n\t} catch (e) {\n\t\tconsole.log(e);\n\t}\n}\n\nupdateOne(\"Howa\", \"Kuwa.....\");\n\nconst updateMany = async (city) => {\n\ttry {\n\t\tvar res = await Teacher.updateMany({age: {$lt: 30}}, {\n\t\t\t$set : { city : city }\n\t\t});\n\t\tconsole.log(res);\n\t} catch (e) {\n\t\tconsole.log(e);\n\t}\n}\n\nupdateMany(\"Mumbai\");\n\n*\/<\/code><\/pre>\n\n\n\n<p><strong>Delete<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var teacher = require(\".\/teacher\");\nconst Teacher = teacher.Teacher;\n\n\/*\nconst deleteMany = async (where) => {\n\ttry {\n\t\tvar res = await Teacher.deleteMany(where);\n\t\tconsole.log(res);\n\t} catch(error) {\n\t\tconsole.log(error);\n\t}\n}\n\ndeleteMany({age : {$in: &#91;25, 37]}});\n*\/<\/code><\/pre>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container\"><\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Insert Connection Module Read Update Delete<\/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\/1006"}],"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=1006"}],"version-history":[{"count":2,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1006\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1006\/revisions\/1008"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}