Summary
Keywords
Full Transcript
Full-stack web development Tutorial in Just 3 Weeks Week 3: Day 2 Section 12: Intro to Node, Mongo, & REST APIs Tutorial 115: Swag Shop API: Creating the Models In this video tutorial, we are going to create a model that defines the data we are gonna store. Models are constructors compiled from schema definitions. An instance of a model is called a document. Models are responsible for creating and reading documents from the MongoDB database. Compiling your first model When you call mongoose.model() on a schema, mongoose compiles model for you. In this video for example: Var mongoose= require( 'mongoose'); Var schema=mongoose. Schema; Var product = new schema({ Title: string, Price: number, Likes:number;}) Module.exports=mongoose. Model('Product',product); The Model() function makes a copy of a schema. Make sure to add everything into the schema before calling .model(). Constructing documents An instance of the model is known as a document. It’s easy to create and store a document in the database. Querying Using the query syntax of MongoDB we can easily find documents in mongoose. Find, FindOne, where are some of the static methods used. Deleting DeleteOne() and deleteMany() are used for deleting documents. Updating Update() is used to modify any document.
