In this lesson we will write the two schemas that will be responsible for creating our MongoDB documents.
Blog series about Modelling one-to-many (AKA parent-child) relationships in MongoDB:
6 Rules of Thumb for MongoDB Schema Design, By William Zola, Lead Technical Support Engineer at MongoDB: Part 1
MDN page detailing Array.prototype.sort
Also, studying source code is a great way to learn! Here's jQuery's implementation of $.extend.
Skip down to line 124 (the line may move, so you can also use the browser's find in page and type in 'extend' to see where it is defined
(jQuery.extend = …).
Let's connect to our question and answer service application to Mongoose, and 0:01 build the schemas for our API. 0:05 The code for 0:08 our database connection will be similar to the code we wrote in our sandbox. 0:08 You can just copy and paste it over to the app.js file. 0:13 Let's open up mongoose_sandbox.js. 0:19 And copy the mongoose require and the connection code. 0:24 Paste it into the app.js right above the router. 0:37 Let's also change the database name to QA for question and answer. 0:42 Notice the call to the db.once method is still open. 0:50 So let's close it with a curly brackets, parentheses and a semicolon. 0:53 Before when we working in our sandbox, we put all of the database interaction 1:06 code inside the callback in this once method. 1:10 That would ensure that the code would only run after the database connection was open 1:14 and ready for business. 1:19 Here in the Express app we wouldn't need to worry about that. 1:21 All we want to do is log out once the open event occurs. 1:25 Setting up the Mongo connection is still asynchronous and 1:31 in fact the express app will normally finish setting up and start listening for 1:34 requests before the database is ready. 1:39 While it would be easy to assume that the requests made during this time 1:42 would be dropped, Mongoose actually stores them and 1:46 then runs them once the database is ready, which is extremely helpful. 1:49 Now that the connection code is in place, 1:54 let's set up the schemas in a separate file called models.js 1:57 Create the file in the root of the folder called models.js, 2:08 and at the top of the file let's put use strict, 2:14 And require Mongoose 2:22 And store the schema constructor as a local variable. 2:35 Now we're ready to write the schemas. 2:46 We'll need two schemas, a question and an answer schema. 2:49 Let's create a question schema. 2:53 As previously decided, we'll need a text property to hold the text of the question, 3:08 As well as a time for creation. 3:21 And then a rate to store the answers in. 3:26 The text and date properties are straightforward. 3:32 To let Mongoose know about the relationship between answers and 3:36 questions, we'll need to place the answer schema 3:40 as the only elements of an array on the question schema. 3:44 This is a signal to Mongoose that we want answer documents to be nested inside 3:48 question documents. 3:53 We haven't created an answer schema yet. 3:54 But when we do, let's call it AnswerSchema. 3:57 Before we create the answer schema, let's improve the createdAt property. 4:04 We know that we want to set this to the current date and 4:09 time when every question is created. 4:12 Mongoose can take care of that using an object to specify the default value. 4:15 Now whenever a question is created using this schema, the current dates and 4:32 time will automatically be supplied for this field. 4:36 Right now, 4:39 AnswerSchema is undefined, since we haven't declared the variable yet. 4:40 We should do this above the QuestionSchema so 4:45 it will be defined by the time QuestionSchema sees it. 4:48 We know we'll need a text attribute and a createdAt time. 5:04 We'll also put in updated app time. 5:21 Which will initially match createdAt. 5:29 Let's also provide votes which is a number, 5:31 And the default value should be 0. 5:40 Now we have the two schemas that are connected together. 5:48 At the bottom of the file we can create and export a question model. 5:52 Let's first create the model using Mongoose's model method. 5:58 We can then later import the question into our routes file to make use of it there. 6:22 Our API is coming along nicely in the next video we'll look at ways to make it 6:29 even better. 6:33
You need to sign up for Treehouse in order to download course files.Sign up