Last active
November 13, 2023 06:28
-
-
Save nishantbadhautiya/a2d45409ce40fd3edb0b5f8a2ce75611 to your computer and use it in GitHub Desktop.
This is for mongoDB tutorial by Thapa Technical
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongoose = require('mongoose'); | |
| const validator = require('validator') | |
| // connection creation and creation a new database | |
| mongoose.connect("mongodb://localhost:27017/ttchannel", { useNewUrlParser: true, useUnifiedTopology: true }) | |
| .then(() => console.log("connection successfull .....")) | |
| .catch(() => console.log("error in connection")); | |
| const playListSchema = new mongoose.Schema({ | |
| name: { | |
| // VALIDATION IN MONGO DB | |
| type: String, | |
| required: true, | |
| unique: true, | |
| // lowercase : true, | |
| uppercase: true, | |
| trim: true, | |
| minLength: [3, "minimum 3 letter required"], | |
| maxLength: 15, | |
| }, | |
| ctype: { | |
| type: String, | |
| lowercase: true, | |
| enum: ['frontend', 'backend', 'database'], | |
| }, | |
| videos: { | |
| type: Number, | |
| // CUSTOM VALIDATION IN MONGO DB | |
| // validate(value) { | |
| // if (value < 0) { | |
| // throw new Error("Videos count should not be negative"); | |
| // } | |
| // } | |
| // another way to use custom validation in mongo db | |
| validate : { | |
| validator : function(value){ | |
| return value > 0; | |
| }, | |
| message : "Videos count should not be negative" | |
| } | |
| }, | |
| author: String, | |
| email : { | |
| type : String, | |
| required : true, | |
| unique : true, | |
| validate(value){ | |
| if(!validator.isEmail(value)){ | |
| throw new Error("Invalid EmailID"); | |
| } | |
| } | |
| }, | |
| active: Boolean, | |
| date: { | |
| type: Date, | |
| default: Date.now() | |
| } | |
| }) | |
| // collection creation | |
| const Playlist = new mongoose.model("Playlist", playListSchema); | |
| // create single document or insert single document | |
| // const reactPlaylist = new Playlist({ | |
| // name : "React JS Tutorial", | |
| // ctype : "Front End", | |
| // videos : 80, | |
| // author : "Thapa Technical", | |
| // active : true | |
| // }) | |
| // reactPlaylist.save(); | |
| // using async await create single document | |
| const createDocument = async () => { | |
| try { | |
| const reactPlaylist = new Playlist({ | |
| name: "ruby3", | |
| ctype: "backend", | |
| videos: 15, | |
| author: "Thapa Technical", | |
| email : '[email protected]', | |
| active: true, | |
| }) | |
| const result = await reactPlaylist.save(); | |
| console.log(result); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| } | |
| // using async await create multiple document | |
| // const createDocument = async () => { | |
| // try { | |
| // const jsPlaylist = new Playlist({ | |
| // name: "JS Tutorial", | |
| // ctype: "Front End", | |
| // videos: 150, | |
| // author: "Thapa Technical", | |
| // active: true | |
| // }) | |
| // const mongoosePlaylist = new Playlist({ | |
| // name: "Mongoose Tutorial", | |
| // ctype: "Database", | |
| // videos: 4, | |
| // author: "Thapa Technical", | |
| // active: true | |
| // }) | |
| // const mongodbPlaylist = new Playlist({ | |
| // name: "mongoDB Tutorial", | |
| // ctype: "Database", | |
| // videos: 10, | |
| // author: "Thapa Technical", | |
| // active: true | |
| // }) | |
| // const expressPlaylist = new Playlist({ | |
| // name: "Express JS Tutorial", | |
| // ctype: "Back End", | |
| // videos: 20, | |
| // author: "Thapa Technical", | |
| // active: true | |
| // }) | |
| // const result = await Playlist.insertMany([jsPlaylist, mongoosePlaylist, mongodbPlaylist, expressPlaylist]); | |
| // console.log(result); | |
| // } catch (err) { | |
| // console.log(err); | |
| // } | |
| // } | |
| createDocument(); | |
| // Reading the database | |
| const getDocument = async () => { | |
| try { | |
| // comparison operators in mongoDB | |
| //********************************************************************************** | |
| // const result = await Playlist.find(); | |
| // const result = await Playlist.find({ctype : "Front End"}).select({name: 1}); | |
| // const result = await Playlist.find({ ctype: "Front End" }).select({ name: 1 }).limit(1); | |
| // const result = await Playlist.find({videos : 50}); | |
| // const result = await Playlist.find({videos : {$gt : 50}}); | |
| // const result = await Playlist.find({videos : {$gte : 50}}); | |
| // const result = await Playlist.find({videos : {$lte : 50}}); | |
| // const result = await Playlist.find({ videos: { $in: [4, 10, 20, 67] } }, {__v: 0, name : 0}); | |
| // const result = await Playlist.find({ ctype: { $nin: ["Database", "Front End"] } }); | |
| // Logical Operator in mongoDB | |
| //*********************************************************************** | |
| // const result = await Playlist.find({$or: [{author : "Thapa Technical"}, {ctype : "Back End"}]}) | |
| // const result = await Playlist.find({$and: [{author : "Thapa Technical"}, {ctype : "Back End"}]}) | |
| // const result = await Playlist.find({ctype : {$not: {$in : ["Back End"]}}}); // show all document where ctype is not back end | |
| // const result = await Playlist.find({$nor : [{ctype : "Back End"}, {ctype : "Front End"}, {videos : 10}]}); | |
| //Count and Sorting Query Methods | |
| //************************************************************************** | |
| // const result = await Playlist.find({$and : [{ctype : "Back End"}, {author : "Thapa Technical"}]}).select({name : 1}).count(); | |
| // const result = await Playlist.find({$and : [{ctype : "Back End"}, {author : "Thapa Technical"}]}).select({name : 1}).countDocuments(); | |
| // const result = await Playlist.find({author : "Thapa Technical"}).count(); | |
| // const result = await Playlist.find({author : "Thapa Technical"}).sort({name : 1}); // always capital case character are highly prioritized | |
| const result = await Playlist.find({ author: "Thapa Technical" }).sort({ name: -1 }); | |
| console.log(result); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| } | |
| // getDocument(); | |
| // Update the Document | |
| const updateDocument = async (_id) => { | |
| // using updateOne method | |
| // try{ | |
| // // filter $set update | |
| // const result = await Playlist.updateOne({_id}, {$set : {name : "Javascript"}}); | |
| // console.log(result); | |
| // }catch(err){ | |
| // console.log(err); | |
| // } | |
| // using findByIdAndUpdate method | |
| try { | |
| // filter update option | |
| const result = await Playlist.findByIdAndUpdate({ _id }, { | |
| $set: { | |
| name: "Javascript Thapa" | |
| } | |
| }, { | |
| new: true, | |
| useFindAndModify: false | |
| }) | |
| console.log(result); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| } | |
| // updateDocument("641d8b4936d2f6f55352f202"); | |
| // Delete the Document | |
| const deleteDocument = async (_id) => { | |
| // using deleteOne method this method does not gives all the detail of deleted document in the console | |
| // try { | |
| // const result = await Playlist.deleteOne({ _id }); | |
| // console.log(result); | |
| // } catch (error) { | |
| // console.log(error); | |
| // } | |
| // using findByIdAndDelete method gives all the detail of deleted document in the console | |
| // try { | |
| // const result = await Playlist.findByIdAndDelete({_id}); | |
| // console.log(result); | |
| // } catch (error) { | |
| // console.log(error); | |
| // } | |
| // using only promise | |
| const result = Playlist.findByIdAndDelete({ _id }); | |
| result.then((value) => { | |
| console.log(value); | |
| }) | |
| } | |
| // deleteDocument("641d8b4936d2f6f55352f202"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment