title
Node.js Crash Course Tutorial #9 - MongoDB

description
Hey gang, in this Node.js tutorial, we'll hook our node express app up with mongodb, a NoSQL database. Well use mongodb atlas to set up our database online - https://www.mongodb.com/cloud/atlas/ 0:00 -- intro to mongodb 2:24 -- mongodb setup with atlas 6:51 -- mongoose 19:26 -- getting & saving data 30:33 -- outputting documents in views 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/node-crash-course 🐱‍💻 🐱‍💻 Other Related Free Courses: + Modern JavaScript Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G 🐱‍💻 🐱‍💻 Node.js links + Download here - https://nodejs.org/en/download/

detail
{'title': 'Node.js Crash Course Tutorial #9 - MongoDB', 'heatmap': [{'end': 347.4, 'start': 213.292, 'weight': 0.939}], 'summary': 'This tutorial covers mongodb in web development, including its structure, use of collections and documents, setup with mongodb atlas. it also explains setting up mongodb with mongoose, creating schema and model for blog data, handling promises and errors in node.js, and interacting with mongodb using mongoose for saving, retrieving, and displaying blog documents.', 'chapters': [{'end': 176.462, 'segs': [{'end': 47.859, 'src': 'embed', 'start': 22.066, 'weight': 0, 'content': [{'end': 28.188, 'text': 'The first type is a SQL database like MySQL, and the second type is a NoSQL database.', 'start': 22.066, 'duration': 6.122}, {'end': 30.729, 'text': 'And both of those structure data differently.', 'start': 28.288, 'duration': 2.441}, {'end': 42.035, 'text': 'So, SQL databases use tables, rows and columns to store records of data and NoSQL databases instead make use of collections and documents.', 'start': 31.329, 'duration': 10.706}, {'end': 47.859, 'text': "Now, the one that we'll be using is called MongoDB and that is a NoSQL database.", 'start': 42.596, 'duration': 5.263}], 'summary': 'Mysql and mongodb are two types of databases; mongodb is a nosql database.', 'duration': 25.793, 'max_score': 22.066, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ22066.jpg'}, {'end': 135.545, 'src': 'embed', 'start': 99.475, 'weight': 2, 'content': [{'end': 102.577, 'text': 'So a block document represents a single block.', 'start': 99.475, 'duration': 3.102}, {'end': 108.482, 'text': 'Now, these documents are stored in a format that looks very similar to JSON or JavaScript objects.', 'start': 103.516, 'duration': 4.966}, {'end': 112.887, 'text': 'They basically hold a series of key and value pairs.', 'start': 109.002, 'duration': 3.885}, {'end': 118.533, 'text': 'For example, a blog document could have a title, a snippet and a body property,', 'start': 113.467, 'duration': 5.066}, {'end': 123.739, 'text': 'and it would also have this auto-generated unique ID to identify it as well.', 'start': 118.533, 'duration': 5.206}, {'end': 135.545, 'text': 'So from our code we could connect to a collection like the blogs in MongoDB and then we could either save, read, update or delete documents inside it.', 'start': 124.62, 'duration': 10.925}], 'summary': 'Block documents in mongodb store key-value pairs, and can be connected to collections for saving, reading, updating, or deleting documents.', 'duration': 36.07, 'max_score': 99.475, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ99475.jpg'}, {'end': 176.462, 'src': 'embed', 'start': 151.328, 'weight': 1, 'content': [{'end': 157.332, 'text': 'We can either install MongoDB on our computer locally and use and then deploy that,', 'start': 151.328, 'duration': 6.004}, {'end': 164.276, 'text': 'or we could use a cloud database which is already hosted for us and it can be quite a lot easier for us to manage.', 'start': 157.332, 'duration': 6.944}, {'end': 165.998, 'text': "So we're going to do that.", 'start': 164.757, 'duration': 1.241}, {'end': 174.241, 'text': "Now, the cloud service we're going to be using to make this database is called MongoDB Atlas, and you can find that at this web address,", 'start': 166.678, 'duration': 7.563}, {'end': 176.462, 'text': 'which I will leave down below.', 'start': 174.241, 'duration': 2.221}], 'summary': 'Explore mongodb deployment options: local installation or cloud-based mongodb atlas.', 'duration': 25.134, 'max_score': 151.328, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ151328.jpg'}], 'start': 2.099, 'title': 'Introducing mongodb in web development', 'summary': 'Introduces mongodb, a nosql database, in web development, covering its structure, use of collections and documents, and setup options including mongodb atlas for easier management.', 'chapters': [{'end': 176.462, 'start': 2.099, 'title': 'Introducing mongodb in web development', 'summary': 'Discusses the introduction of mongodb, a nosql database, in web development, explaining its structure, the use of collections and documents, and the setup options including mongodb atlas for easier management.', 'duration': 174.363, 'highlights': ['MongoDB is a NoSQL database, using collections and documents instead of tables, rows, and columns in SQL databases.', 'Document in MongoDB is similar to a record in a SQL database, holding a series of key and value pairs, like a JSON or JavaScript object.', 'MongoDB Atlas is a cloud database service for MongoDB, offering an easier management option than installing MongoDB locally.']}], 'duration': 174.363, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ2099.jpg', 'highlights': ['MongoDB is a NoSQL database, using collections and documents instead of tables, rows, and columns in SQL databases.', 'MongoDB Atlas is a cloud database service for MongoDB, offering an easier management option than installing MongoDB locally.', 'Document in MongoDB is similar to a record in a SQL database, holding a series of key and value pairs, like a JSON or JavaScript object.']}, {'end': 795.257, 'segs': [{'end': 347.4, 'src': 'heatmap', 'start': 213.292, 'weight': 0.939, 'content': [{'end': 215.814, 'text': 'then we want to choose a provider and region.', 'start': 213.292, 'duration': 2.522}, {'end': 218.116, 'text': "i'm going to stick with the defaults right there.", 'start': 215.814, 'duration': 2.302}, {'end': 220.297, 'text': 'we can give our cluster a name if you want.', 'start': 218.116, 'duration': 2.181}, {'end': 224.881, 'text': "i'm just going to call this node tuts, like, so you can leave it as the default if you prefer.", 'start': 220.297, 'duration': 4.584}, {'end': 232.728, 'text': "it really doesn't matter at the minute, especially for this tutorial, And then it's gonna just take a couple of minutes to create that cluster.", 'start': 224.881, 'duration': 7.847}, {'end': 238.394, 'text': "So then, once that's done, the next step is to create a database and a collection inside that database.", 'start': 232.728, 'duration': 5.666}, {'end': 244.24, 'text': "So let's click on collections to do that, And then we wanna add our own data.", 'start': 238.394, 'duration': 5.846}, {'end': 245.941, 'text': 'We need to give this database a name.', 'start': 244.24, 'duration': 1.701}, {'end': 246.922, 'text': 'call it what you will.', 'start': 245.941, 'duration': 0.981}, {'end': 250.587, 'text': "I'm gonna call it NodeTuts, and then we need a collection name.", 'start': 246.922, 'duration': 3.665}, {'end': 255.213, 'text': "remember, a collection is where we're going to store a load of documents of a certain type.", 'start': 250.587, 'duration': 4.626}, {'end': 259.039, 'text': "so we're going to store blog documents for our blog in there.", 'start': 255.213, 'duration': 3.826}, {'end': 263.525, 'text': "so we'll call this blogs make sense and i'm going to create this collection.", 'start': 259.039, 'duration': 4.486}, {'end': 266.507, 'text': 'so this is our collection right here.', 'start': 264.346, 'duration': 2.161}, {'end': 269.149, 'text': "there's no documents in it at the minute.", 'start': 266.507, 'duration': 2.642}, {'end': 276.953, 'text': "what I'm going to do now is create a user so that we can use that user for database access from our code over here.", 'start': 269.149, 'duration': 7.804}, {'end': 282.376, 'text': "so let's go to database access and create a new database user.", 'start': 276.953, 'duration': 5.423}, {'end': 286.738, 'text': "I'm going to call this net ninja and the password is going to be test1234.", 'start': 282.376, 'duration': 4.362}, {'end': 290.36, 'text': "please create something that's more unique than that and more secure.", 'start': 286.738, 'duration': 3.622}, {'end': 296.183, 'text': "we need to give, read and write access to any database and i'm going to add user right here.", 'start': 290.84, 'duration': 5.343}, {'end': 303.928, 'text': "so i'm going to use that user later on the username and the password i created from our code so that we can connect to the database.", 'start': 296.183, 'duration': 7.745}, {'end': 310.292, 'text': 'that way, not anyone can just connect to the database, only a valid user that we declare right here.', 'start': 303.928, 'duration': 6.364}, {'end': 313.134, 'text': 'so some kind of admin user all right.', 'start': 310.292, 'duration': 2.842}, {'end': 319.839, 'text': 'so now we need to go back to clusters and we need to figure out a way to connect to this database.', 'start': 313.134, 'duration': 6.705}, {'end': 330.147, 'text': "so i'm gonna connect right here and then we want to connect from our application, like so, and this connection string we're gonna need later on.", 'start': 319.839, 'duration': 10.308}, {'end': 340.556, 'text': "so i'm gonna copy this right here and i'm gonna go back over to our app file and i'm gonna create for now just a new variable, so const,", 'start': 330.147, 'duration': 10.409}, {'end': 347.4, 'text': "And I'll call this dburi and set it equal to this string, Like so.", 'start': 341.176, 'duration': 6.224}], 'summary': 'Setting up a database cluster with a collection and user, creating a connection string for application usage.', 'duration': 134.108, 'max_score': 213.292, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ213292.jpg'}, {'end': 500.998, 'src': 'embed', 'start': 431.972, 'weight': 0, 'content': [{'end': 442.356, 'text': 'now it does this by allowing us to create simple data models which have database query methods to create, get, delete and update database documents.', 'start': 431.972, 'duration': 10.384}, {'end': 446.417, 'text': 'now, under the hood, mongoose does all the heavy lifting for us.', 'start': 442.356, 'duration': 4.061}, {'end': 451.899, 'text': 'it queries the correct database collection for us based on the name of the model we use,', 'start': 446.417, 'duration': 5.482}, {'end': 455.461, 'text': 'and then it performs the action required and returns us a response.', 'start': 451.899, 'duration': 3.562}, {'end': 461.927, 'text': "now you don't have to use Mongoose, you can just use the standalone MongoDB library if you prefer,", 'start': 456.121, 'duration': 5.806}, {'end': 465.431, 'text': 'but sometimes it can feel a bit more clunky and verbose.', 'start': 461.927, 'duration': 3.504}, {'end': 470.977, 'text': "so we're going to use Mongoose, and let me break down exactly how we're going to be using it in our project.", 'start': 465.431, 'duration': 5.546}, {'end': 479.121, 'text': 'so the way we generally work with mongodb and mongoose is to create schemas and models.', 'start': 472.034, 'duration': 7.087}, {'end': 483.586, 'text': 'now, this is quite similar to how we typically work with any kind of data resource.', 'start': 479.121, 'duration': 4.465}, {'end': 487.189, 'text': "we'd make a model to represent that data resource.", 'start': 483.586, 'duration': 3.603}, {'end': 490.573, 'text': 'now, in mongoose, though, we make a schema first.', 'start': 487.189, 'duration': 3.384}, {'end': 497.036, 'text': 'Now the schema defines the structure of a data type or document stored in a database collection.', 'start': 491.133, 'duration': 5.903}, {'end': 500.998, 'text': 'It describes what properties it should have, the type of those properties, et cetera.', 'start': 497.476, 'duration': 3.522}], 'summary': 'Mongoose simplifies database operations with data models and query methods, providing a more efficient approach compared to standalone mongodb library.', 'duration': 69.026, 'max_score': 431.972, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ431972.jpg'}, {'end': 667.405, 'src': 'embed', 'start': 639.988, 'weight': 3, 'content': [{'end': 646.715, 'text': 'into this connect method and then Mongoose is going to go out and connect to that database for us now.', 'start': 639.988, 'duration': 6.727}, {'end': 651.978, 'text': 'if i was to save this right now and run it, we might get a deprecation warning.', 'start': 646.715, 'duration': 5.263}, {'end': 658.401, 'text': 'so let me say nodemon app to run this and you can see right here we get a deprecation warning.', 'start': 651.978, 'duration': 6.423}, {'end': 660.001, 'text': 'now you can leave that if you want to.', 'start': 658.401, 'duration': 1.6}, {'end': 667.405, 'text': "it's not really going to affect how we use the code, but what i'm going to do is pass in a second argument, which is an options object,", 'start': 660.001, 'duration': 7.404}], 'summary': 'Using mongoose to connect to the database, might get a deprecation warning.', 'duration': 27.417, 'max_score': 639.988, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ639988.jpg'}, {'end': 719.329, 'src': 'embed', 'start': 697.416, 'weight': 4, 'content': [{'end': 705.921, 'text': "so this then method takes in a callback function which is going to fire when this connection is complete, after it's connected to the database,", 'start': 697.416, 'duration': 8.505}, {'end': 710.244, 'text': 'and we get a result in this callback function which we can use if we want to.', 'start': 705.921, 'duration': 4.323}, {'end': 719.329, 'text': "I'm just going to say in here console.log, connected to DB, and we also get a catch method in case there's an error.", 'start': 710.244, 'duration': 9.085}], 'summary': 'The method takes a callback function to fire after connecting to the database, with a catch method for errors.', 'duration': 21.913, 'max_score': 697.416, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ697416.jpg'}, {'end': 774.453, 'src': 'embed', 'start': 743.18, 'weight': 5, 'content': [{'end': 745.302, 'text': 'connected to the database.', 'start': 743.18, 'duration': 2.122}, {'end': 746.263, 'text': 'awesome, so that works.', 'start': 745.302, 'duration': 0.961}, {'end': 757.968, 'text': "now. then, if you think about it, we don't really want our server to be listening for requests until this connection has been made, because, for example,", 'start': 747.324, 'duration': 10.644}, {'end': 764.009, 'text': 'if a user requests the home page and that home page lists a load of data dependent on the database,', 'start': 757.968, 'duration': 6.041}, {'end': 768.851, 'text': "then we can't show that until the connection to the database has been established.", 'start': 764.009, 'duration': 4.842}, {'end': 774.453, 'text': "so we really don't want to be listening for requests until this is completed.", 'start': 768.851, 'duration': 5.602}], 'summary': 'Delay listening for requests until database connection is established.', 'duration': 31.273, 'max_score': 743.18, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ743180.jpg'}], 'start': 176.963, 'title': 'Mongodb setup and usage with mongoose', 'summary': 'Covers setting up mongodb with mongoose, including creating a cluster, database, collection, user, and connecting to the database. it further explains the process of connecting to the database using mongoose, including handling deprecation warnings and ensuring complete database connection before server request listening.', 'chapters': [{'end': 567.89, 'start': 176.963, 'title': 'Setting up mongodb with mongoose', 'summary': 'Guides through setting up mongodb with mongoose, including creating a cluster, database, collection, user, and connecting to the database, as well as explaining how mongoose simplifies interaction with mongodb and the process of creating schemas and models for data storage.', 'duration': 390.927, 'highlights': ['The chapter guides through setting up MongoDB with Mongoose, including creating a cluster, database, collection, user, and connecting to the database.', 'Mongoose simplifies interaction with MongoDB and provides an easier way to connect and communicate with the database through simple data models with database query methods.', 'Explains the process of creating schemas and models for data storage in MongoDB using Mongoose.']}, {'end': 795.257, 'start': 567.89, 'title': 'Connecting to database with mongoose', 'summary': 'Covers the process of connecting to a database using mongoose, including installing the package, using the connect method, handling deprecation warnings, and ensuring the server does not listen for requests until the database connection is complete.', 'duration': 227.367, 'highlights': ['The process of connecting to the database using Mongoose involves installing the package, using the connect method, and handling deprecation warnings.', 'The connection to the database using Mongoose is an asynchronous task that returns a promise, which can be handled using the then method.', 'The server should only listen for requests after the database connection is complete to ensure that data-dependent pages are displayed accurately.']}], 'duration': 618.294, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ176963.jpg', 'highlights': ['Mongoose simplifies interaction with MongoDB and provides an easier way to connect and communicate with the database through simple data models with database query methods.', 'The chapter guides through setting up MongoDB with Mongoose, including creating a cluster, database, collection, user, and connecting to the database.', 'Explains the process of creating schemas and models for data storage in MongoDB using Mongoose.', 'The process of connecting to the database using Mongoose involves installing the package, using the connect method, and handling deprecation warnings.', 'The connection to the database using Mongoose is an asynchronous task that returns a promise, which can be handled using the then method.', 'The server should only listen for requests after the database connection is complete to ensure that data-dependent pages are displayed accurately.']}, {'end': 1100.563, 'segs': [{'end': 820.901, 'src': 'embed', 'start': 795.257, 'weight': 0, 'content': [{'end': 800.258, 'text': 'so the next thing to do now is to create a schema and a model for our blog data.', 'start': 795.257, 'duration': 5.001}, {'end': 808.688, 'text': "So I'm going to create a new folder for all of my models and schemas over here, And I'm going to call that folder Models.", 'start': 800.738, 'duration': 7.95}, {'end': 812.453, 'text': 'Now we want to create a model and a schema for a blog.', 'start': 808.688, 'duration': 3.765}, {'end': 815.016, 'text': "So let's create a new file called blog.js.", 'start': 812.453, 'duration': 2.563}, {'end': 816.998, 'text': 'all right.', 'start': 816.678, 'duration': 0.32}, {'end': 820.901, 'text': 'so inside here the first thing we need to do is require mongoose.', 'start': 816.998, 'duration': 3.903}], 'summary': 'Creating schema and model for blog data using mongoose.', 'duration': 25.644, 'max_score': 795.257, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ795257.jpg'}, {'end': 862.75, 'src': 'embed', 'start': 839.734, 'weight': 3, 'content': [{'end': 848.02, 'text': "okay. so remember, schema is the thing that is gonna define the structure documents that we're gonna like to store inside a collection.", 'start': 839.734, 'duration': 8.286}, {'end': 852.143, 'text': "it's the thing that model wraps around, okay.", 'start': 848.02, 'duration': 4.123}, {'end': 855.825, 'text': 'so this thing right here is actually a constructor function.', 'start': 852.143, 'duration': 3.682}, {'end': 858.887, 'text': "I'm gonna use it to create a new schema.", 'start': 855.825, 'duration': 3.062}, {'end': 860.729, 'text': "I'm gonna store that in a constant.", 'start': 858.887, 'duration': 1.842}, {'end': 862.75, 'text': 'now you can call your schema whatever you want.', 'start': 860.729, 'duration': 2.021}], 'summary': 'Schema defines structure for storing documents in a collection.', 'duration': 23.016, 'max_score': 839.734, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ839734.jpg'}, {'end': 962.231, 'src': 'embed', 'start': 929.631, 'weight': 1, 'content': [{'end': 936.155, 'text': "we also want our blogs to have a snippet property, and again let's open up an object so we can add the type,", 'start': 929.631, 'duration': 6.524}, {'end': 942.539, 'text': "which is also going to be of type string and it's also going to be required, right.", 'start': 936.155, 'duration': 6.384}, {'end': 951.345, 'text': "so this is mongoose creating a schema for us, and we're allowed to say what type of data each property should be and whether it's required or not.", 'start': 942.539, 'duration': 8.806}, {'end': 962.231, 'text': "so let's just add one more, which is the body, and this also is going to be of type string, and We also want to make this required.", 'start': 951.345, 'duration': 10.886}], 'summary': 'Using mongoose to create a blog schema with properties like snippet, type, and body.', 'duration': 32.6, 'max_score': 929.631, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ929631.jpg'}, {'end': 998.263, 'src': 'embed', 'start': 979.097, 'weight': 2, 'content': [{'end': 990.301, 'text': 'now what I want to do is set timestamps in here to true, and this automatically generates timestamp properties for us on our blog documents as well.', 'start': 979.097, 'duration': 11.204}, {'end': 998.263, 'text': 'so, like, i created that and updated that property, and every time we therefore in the future, update or create a blog document,', 'start': 990.301, 'duration': 7.962}], 'summary': 'Enabling timestamps automatically generates properties for blog documents.', 'duration': 19.166, 'max_score': 979.097, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ979097.jpg'}, {'end': 1051.488, 'src': 'embed', 'start': 1020.629, 'weight': 4, 'content': [{'end': 1025.532, 'text': 'so remember, the schema is the thing that defines the structure of our documents.', 'start': 1020.629, 'duration': 4.903}, {'end': 1034.82, 'text': 'the model is the thing that surrounds that and then provides us with an interface by which to communicate with a database collection for that document type.', 'start': 1025.532, 'duration': 9.288}, {'end': 1044.325, 'text': "so I'm going to create a new constant down here to store this model in, and typically the names of these models are given a capital letter.", 'start': 1035.601, 'duration': 8.724}, {'end': 1051.488, 'text': "so I'm going to call it blog with a capital B, and I'm going to set that equal to mongoose and then use a method called model.", 'start': 1044.325, 'duration': 7.163}], 'summary': "Defines schema and creates model 'blog' using mongoose", 'duration': 30.859, 'max_score': 1020.629, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1020629.jpg'}], 'start': 795.257, 'title': 'Creating schema, mongoose schema, and model', 'summary': 'Covers creating a schema and model for blog data using mongoose, specifying properties like title, snippet, and body as strings and marking them as required, and setting timestamps to auto-generate properties for blog documents, providing a detailed insight into the schema creation process and model definition.', 'chapters': [{'end': 884.303, 'start': 795.257, 'title': 'Creating schema and model for blog data', 'summary': 'Covers creating a schema and model for blog data using mongoose, including defining the structure of documents and the purpose of the schema constructor function.', 'duration': 89.046, 'highlights': ['The chapter covers creating a schema and model for blog data using Mongoose, including defining the structure of documents and the purpose of the schema constructor function.', 'Explains the purpose of the schema, which is to define the structure of documents to be stored inside a collection.', 'Demonstrates the use of the mongoose package and the schema constructor function to create a new instance of a schema object.']}, {'end': 979.097, 'start': 884.303, 'title': 'Creating mongoose schema', 'summary': 'Explains the process of creating a mongoose schema for a blog, specifying properties like title, snippet, and body, setting their types as strings, and marking them as required, providing a detailed insight into the schema creation process.', 'duration': 94.794, 'highlights': ['The chapter focuses on creating a Mongoose schema for a blog, setting properties like title, snippet, and body, and specifying their type as string and marking them as required.', 'It explains the process of opening up an object to add extra validation and setting the required field for blog documents, providing a detailed insight into the schema creation process.', "It mentions that Mongoose allows specifying the type of data for each property and whether it's required or not, providing an in-depth explanation of the schema creation process."]}, {'end': 1100.563, 'start': 979.097, 'title': 'Setting timestamps and creating a model', 'summary': 'Discusses setting timestamps to auto-generate properties for blog documents and creating a model to define the structure of documents and communicate with a database collection.', 'duration': 121.466, 'highlights': ['The model is the thing that surrounds the schema and provides an interface to communicate with a database collection for that document type.', 'Setting timestamps to true automatically generates timestamp properties for blog documents, auto-assigning values to those properties when creating or updating a blog document.', "The model name 'blog' is important as it is used to look for the corresponding collection inside the database and automatically pluralizes the name to find the collection."]}], 'duration': 305.306, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ795257.jpg', 'highlights': ['The chapter covers creating a schema and model for blog data using Mongoose, including defining the structure of documents and the purpose of the schema constructor function.', 'The chapter focuses on creating a Mongoose schema for a blog, setting properties like title, snippet, and body, and specifying their type as string and marking them as required.', 'Setting timestamps to true automatically generates timestamp properties for blog documents, auto-assigning values to those properties when creating or updating a blog document.', 'Explains the purpose of the schema, which is to define the structure of documents to be stored inside a collection.', 'The model is the thing that surrounds the schema and provides an interface to communicate with a database collection for that document type.']}, {'end': 1360.137, 'segs': [{'end': 1199.619, 'src': 'embed', 'start': 1147.272, 'weight': 0, 'content': [{'end': 1148.352, 'text': 'so remember to do that.', 'start': 1147.272, 'duration': 1.08}, {'end': 1154.434, 'text': 'we can say module dot exports and set that equal to the blog.', 'start': 1148.352, 'duration': 6.082}, {'end': 1160.756, 'text': 'there we go there, my friends, we have created our blog schema and our blog model, and now we have this model,', 'start': 1154.434, 'duration': 6.322}, {'end': 1164.937, 'text': 'we can use it to save some new blog documents to our database collection.', 'start': 1160.756, 'duration': 4.181}, {'end': 1170.332, 'text': 'okay then.', 'start': 1169.972, 'duration': 0.36}, {'end': 1172.853, 'text': 'so now we have our blog model right here.', 'start': 1170.332, 'duration': 2.521}, {'end': 1176.333, 'text': "let's try using this in the code to interact with the database.", 'start': 1172.853, 'duration': 3.48}, {'end': 1182.775, 'text': "so then inside app.js, what i'm going to do is create just a handful of sandbox routes, if you like,", 'start': 1176.333, 'duration': 6.442}, {'end': 1186.196, 'text': 'just to test out this interaction with the database.', 'start': 1182.775, 'duration': 3.421}, {'end': 1195.638, 'text': "so what i'm going to do first of all is a get handler, app.get, which is going to respond to requests to forward slash, add hyphen blog,", 'start': 1186.196, 'duration': 9.442}, {'end': 1199.619, 'text': 'and this is going to be used to add a blog to the collection.', 'start': 1196.398, 'duration': 3.221}], 'summary': 'Created blog schema and model, now using it to save new blog documents to database.', 'duration': 52.347, 'max_score': 1147.272, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1147272.jpg'}, {'end': 1302.887, 'src': 'embed', 'start': 1273.147, 'weight': 2, 'content': [{'end': 1276.529, 'text': 'Mongoose automatically takes care of the timestamps for us.', 'start': 1273.147, 'duration': 3.382}, {'end': 1280.573, 'text': 'So just these three things right here that we need to worry about.', 'start': 1276.91, 'duration': 3.663}, {'end': 1295.944, 'text': "So let's add in a title property, and that is going to be new blog, very original right, and the snippet is going to be about oops, about my new blog,", 'start': 1280.573, 'duration': 15.371}, {'end': 1302.887, 'text': 'all right, and then the body is going to be more about my new blog.', 'start': 1295.944, 'duration': 6.943}], 'summary': 'Mongoose handles timestamps, adding a title, snippet, and body to a new blog entry.', 'duration': 29.74, 'max_score': 1273.147, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1273147.jpg'}, {'end': 1354.574, 'src': 'embed', 'start': 1325.367, 'weight': 4, 'content': [{'end': 1332.649, 'text': 'so when we get a new instance of the blog model, it gives us a load of different methods that we can use,', 'start': 1325.367, 'duration': 7.282}, {'end': 1336.57, 'text': "and we're using one called save to save this to the database.", 'start': 1332.649, 'duration': 3.921}, {'end': 1341.351, 'text': "now, under the hood, mongoose is going to say well, okay, we've used the blog model.", 'start': 1336.57, 'duration': 4.781}, {'end': 1354.574, 'text': "therefore i'm going to look for the blogs collection based on this name and i'm going to take this document that you created with this information and i'm going to save it to the blogs collection and it goes out and it does that for us.", 'start': 1341.351, 'duration': 13.223}], 'summary': "Mongoose's blog model provides methods like save to store data in the blogs collection.", 'duration': 29.207, 'max_score': 1325.367, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1325367.jpg'}], 'start': 1100.563, 'title': 'Creating and using blog model and saving blog data', 'summary': 'Explains the process of creating a blog schema and model, including defining the structure, creating the model, and exporting it, along with creating and saving blog data to a database using mongoose, including creating a new instance of a blog document with title, snippet, and body properties, and using the save method to store it in the blogs collection.', 'chapters': [{'end': 1199.619, 'start': 1100.563, 'title': 'Creating and using blog model', 'summary': 'Explains the steps involved in creating a blog schema and model, including defining the structure, creating the model, and exporting it, with a demonstration of using the model to interact with the database in the app.js file.', 'duration': 99.056, 'highlights': ['The process of creating a blog schema and model involves defining the structure and creating the model based on the schema, then exporting the model for use elsewhere in the project.', 'Demonstration of using the blog model to interact with the database is shown through creating sandbox routes in the app.js file, including a get handler to add a blog to the collection.']}, {'end': 1360.137, 'start': 1199.619, 'title': 'Creating and saving blog data', 'summary': 'Explains how to create and save blog data to a database using mongoose, including creating a new instance of a blog document with title, snippet, and body properties, and using the save method to store it in the blogs collection.', 'duration': 160.518, 'highlights': ['The chapter explains how to create a new instance of a blog document with title, snippet, and body properties for storing in the database using Mongoose.', 'Mongoose automatically handles the timestamps, and only the title, snippet, and body properties need to be specified when creating a new blog instance.', 'The save method is used to store the new blog instance in the database, which is an asynchronous task with a time delay.']}], 'duration': 259.574, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1100563.jpg', 'highlights': ['The process of creating a blog schema and model involves defining the structure and creating the model based on the schema, then exporting the model for use elsewhere in the project.', 'Demonstration of using the blog model to interact with the database is shown through creating sandbox routes in the app.js file, including a get handler to add a blog to the collection.', 'The chapter explains how to create a new instance of a blog document with title, snippet, and body properties for storing in the database using Mongoose.', 'Mongoose automatically handles the timestamps, and only the title, snippet, and body properties need to be specified when creating a new blog instance.', 'The save method is used to store the new blog instance in the database, which is an asynchronous task with a time delay.']}, {'end': 2221.074, 'segs': [{'end': 1392.688, 'src': 'embed', 'start': 1360.137, 'weight': 1, 'content': [{'end': 1366.301, 'text': 'it might take a second to do, probably less, but nonetheless it takes some time and it returns us a promise,', 'start': 1360.137, 'duration': 6.164}, {'end': 1372.526, 'text': 'so we can tack on a then method which is going to fire a callback function when this promise resolves.', 'start': 1366.301, 'duration': 6.225}, {'end': 1380.071, 'text': "okay, so inside this callback function we get the results and what we're going to do is just send back a response.", 'start': 1372.526, 'duration': 7.545}, {'end': 1383.493, 'text': "so response.send, and we're going to send back the results.", 'start': 1380.071, 'duration': 3.422}, {'end': 1392.688, 'text': 'so we can see that in the browser and we also want to attach a catch method to this, just in case there was any kind of error with this action,', 'start': 1383.493, 'duration': 9.195}], 'summary': 'Using a promise with a then method to send back results and attaching a catch method for errors.', 'duration': 32.551, 'max_score': 1360.137, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1360137.jpg'}, {'end': 1453.897, 'src': 'embed', 'start': 1428.106, 'weight': 0, 'content': [{'end': 1432.33, 'text': 'We also get these timestamps that Mongoose created for us right here as well.', 'start': 1428.106, 'duration': 4.224}, {'end': 1436.855, 'text': "So this thing right here, this object, kind of thing that we're seeing,", 'start': 1432.85, 'duration': 4.005}, {'end': 1444.226, 'text': 'is actually the new document object that is being sent back to us in the code once this action is complete.', 'start': 1436.855, 'duration': 7.371}, {'end': 1453.897, 'text': 'So once Mongo saves it to the database, the database then sends us back object version of that new document, so not this object right here.', 'start': 1444.266, 'duration': 9.631}], 'summary': 'Mongoose provides timestamps for new document objects.', 'duration': 25.791, 'max_score': 1428.106, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1428106.jpg'}, {'end': 1500.799, 'src': 'embed', 'start': 1477.353, 'weight': 6, 'content': [{'end': 1485.563, 'text': "Now we don't, because I've just noticed I made a spelling error over here, but it's actually connected to the NodeTuts database,", 'start': 1477.353, 'duration': 8.21}, {'end': 1488.526, 'text': 'which is what I specified in my string right here.', 'start': 1485.563, 'duration': 2.963}, {'end': 1490.709, 'text': "So that database didn't exist.", 'start': 1488.566, 'duration': 2.143}, {'end': 1497.677, 'text': "It's going to created that for me because I was a bit of a plonker before and created one called NoteTuts.", 'start': 1491.149, 'duration': 6.528}, {'end': 1500.799, 'text': "but anyway it's created this database for me.", 'start': 1498.217, 'duration': 2.582}], 'summary': 'A database named nodetuts was created due to a spelling error in the database name specified in the string.', 'duration': 23.446, 'max_score': 1477.353, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1477353.jpg'}, {'end': 1553.518, 'src': 'embed', 'start': 1526.08, 'weight': 2, 'content': [{'end': 1529.384, 'text': 'so that is how we add a new blog.', 'start': 1526.08, 'duration': 3.304}, {'end': 1533.55, 'text': 'if i wanted to add another one, let me say new blog 2, save it.', 'start': 1529.384, 'duration': 4.166}, {'end': 1541.18, 'text': "if i go to this url again, add blog, then it's going to add another one and we should see new blog too over here.", 'start': 1533.55, 'duration': 7.63}, {'end': 1549.612, 'text': "now that's the new document it's sending back and if we go to the database and refresh over here, we should see two documents inside here.", 'start': 1541.18, 'duration': 8.432}, {'end': 1553.518, 'text': 'now we can see new blog and new blog too, awesome.', 'start': 1549.612, 'duration': 3.906}], 'summary': 'Adding two new blogs resulted in two documents in the database.', 'duration': 27.438, 'max_score': 1526.08, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1526080.jpg'}, {'end': 1620.499, 'src': 'embed', 'start': 1596.083, 'weight': 3, 'content': [{'end': 1602.286, 'text': "so down here i'm going to say blog with a capital b, and then i'm going to use the method find,", 'start': 1596.083, 'duration': 6.203}, {'end': 1606.468, 'text': 'and that gets us all of the documents inside the blogs collection.', 'start': 1602.286, 'duration': 4.182}, {'end': 1608.289, 'text': 'it goes out and gets all of them.', 'start': 1606.468, 'duration': 1.821}, {'end': 1610.37, 'text': "now again it's asynchronous.", 'start': 1608.829, 'duration': 1.541}, {'end': 1611.812, 'text': 'it takes a little while to do so.', 'start': 1610.37, 'duration': 1.442}, {'end': 1620.499, 'text': "we can tack on a then method to it which fires a callback function with the results from the database when it's finished.", 'start': 1611.812, 'duration': 8.687}], 'summary': 'Using the method find retrieves all documents in the blogs collection, asynchronous process.', 'duration': 24.416, 'max_score': 1596.083, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1596083.jpg'}, {'end': 1753.272, 'src': 'embed', 'start': 1729.187, 'weight': 4, 'content': [{'end': 1738.435, 'text': 'we can just pass in a string here and then mongoose is going to handle that conversion into an object id to compare it to one of these documents and retrieve that single document.', 'start': 1729.187, 'duration': 9.248}, {'end': 1749.568, 'text': 'so if i go to all blogs, i could just get this one right here, for example, which is the first one new blog, and if i paste that right in here,', 'start': 1738.895, 'duration': 10.673}, {'end': 1753.272, 'text': "like so, it's going to go out and it's going to find the blog with that id.", 'start': 1749.568, 'duration': 3.704}], 'summary': 'Mongoose can convert string to object id for document retrieval.', 'duration': 24.085, 'max_score': 1729.187, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1729187.jpg'}, {'end': 2200.089, 'src': 'embed', 'start': 2170.712, 'weight': 5, 'content': [{'end': 2175.655, 'text': 'now i also want to show you one more quick thing right here, and that is the sort method right here.', 'start': 2170.712, 'duration': 4.943}, {'end': 2180.378, 'text': 'so we can sort by a particular field inside our documents.', 'start': 2175.655, 'duration': 4.723}, {'end': 2183.48, 'text': "now i'm going to sort by created at.", 'start': 2180.378, 'duration': 3.102}, {'end': 2188.644, 'text': 'remember, these were the time stamps that mongoose automatically added on for us.', 'start': 2183.48, 'duration': 5.164}, {'end': 2193.626, 'text': "I'm going to say minus one right here, and that means descending order.", 'start': 2189.244, 'duration': 4.382}, {'end': 2200.089, 'text': "so it's going to go from the newest to the oldest, and the newest now will be at the top of the page instead of the oldest.", 'start': 2193.626, 'duration': 6.463}], 'summary': "Demonstrated sorting documents by 'created at' field in descending order.", 'duration': 29.377, 'max_score': 2170.712, 'thumbnail': ''}], 'start': 1360.137, 'title': 'Handling promise and error in node.js', 'summary': 'Discusses handling promises and errors in node.js, covering sending a response, attaching a catch method for error handling, and receiving the new document object from the database. it also demonstrates interacting with mongodb using mongoose, covering saving, retrieving, and displaying blog documents, adding new blogs, retrieving all blogs, finding a single blog, and sorting blogs by created date.', 'chapters': [{'end': 1500.799, 'start': 1360.137, 'title': 'Handling promise and error in node.js', 'summary': 'Discusses handling promises and errors in node.js when sending a response, attaching a catch method for error handling, and receiving the new document object from the database after an action is complete.', 'duration': 140.662, 'highlights': ['Handling promises with then and catch methods', 'Receiving new document object from the database', 'Creating a database in Node.js']}, {'end': 2221.074, 'start': 1500.799, 'title': 'Interacting with mongodb using mongoose', 'summary': 'Demonstrates how to save, retrieve, and display blog documents in a mongodb database using mongoose. it covers adding new blogs, retrieving all blogs, finding a single blog, and sorting blogs by created date.', 'duration': 720.275, 'highlights': ['Demonstrates how to save new blog documents to the database', 'Explains retrieving all blog documents from the collection', 'Discusses finding a single blog based on its unique ID', 'Introduces sorting blog documents by created date']}], 'duration': 860.937, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/bxsemcrY4gQ/pics/bxsemcrY4gQ1360137.jpg', 'highlights': ['Receiving new document object from the database', 'Handling promises with then and catch methods', 'Demonstrates how to save new blog documents to the database', 'Explains retrieving all blog documents from the collection', 'Discussing finding a single blog based on its unique ID', 'Introduces sorting blog documents by created date', 'Creating a database in Node.js']}], 'highlights': ['Mongoose simplifies interaction with MongoDB and provides an easier way to connect and communicate with the database through simple data models with database query methods.', 'MongoDB Atlas is a cloud database service for MongoDB, offering an easier management option than installing MongoDB locally.', 'Document in MongoDB is similar to a record in a SQL database, holding a series of key and value pairs, like a JSON or JavaScript object.', 'The chapter covers creating a schema and model for blog data using Mongoose, including defining the structure of documents and the purpose of the schema constructor function.', 'The process of creating a blog schema and model involves defining the structure and creating the model based on the schema, then exporting the model for use elsewhere in the project.', 'The chapter focuses on creating a Mongoose schema for a blog, setting properties like title, snippet, and body, and specifying their type as string and marking them as required.', 'Setting timestamps to true automatically generates timestamp properties for blog documents, auto-assigning values to those properties when creating or updating a blog document.', 'Demonstration of using the blog model to interact with the database is shown through creating sandbox routes in the app.js file, including a get handler to add a blog to the collection.', 'The model is the thing that surrounds the schema and provides an interface to communicate with a database collection for that document type.', 'The process of connecting to the database using Mongoose involves installing the package, using the connect method, and handling deprecation warnings.', 'The connection to the database using Mongoose is an asynchronous task that returns a promise, which can be handled using the then method.', 'The server should only listen for requests after the database connection is complete to ensure that data-dependent pages are displayed accurately.', 'Mongoose automatically handles the timestamps, and only the title, snippet, and body properties need to be specified when creating a new blog instance.', 'The save method is used to store the new blog instance in the database, which is an asynchronous task with a time delay.', 'Receiving new document object from the database', 'Handling promises with then and catch methods', 'Demonstrates how to save new blog documents to the database', 'Explains retrieving all blog documents from the collection', 'Discussing finding a single blog based on its unique ID', 'Introduces sorting blog documents by created date', 'Creating a database in Node.js']}