Now that we have some idea of how Mongoose works, we can build an interface our application will use to access the database. Before we can write the schemas, though, we still need to know how we're going to associate question and answer objects in Mongo itself.
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:
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 = …).
[MUSIC] 0:00 Now that we have an idea of how Mongoose works, let's add it to our application. 0:04 Before we can write the schemas we still need to know how we're going to associate 0:09 question and answer objects in Mongo itself. 0:13 Mungo is optimized for storing infection single documents quickly, while working 0:17 with multiple documents is possible it can come at a performance cost. 0:23 To work with Mongo and not against it, it's best to associate the data 0:28 together by wrapping all the data in a single document and 0:32 handing it over to Mongo for storage. 0:36 For our question and answer objects, that will mean we're only creating 0:39 one collection, a questions collection, concerning question objects, 0:43 along with an additional property and a rate that holds the child answer objects. 0:48 That way, a question and it's entire collection of answers 0:55 is encapsulated together and can be retrieved with one database call. 0:59 While this is often the preferred way to structure a parent child 1:04 relationship in Mongo, there are reasons you may want to break up parents and 1:08 children into separate collections. 1:12 One reason would be if there are more than a few hundred children, 1:16 Mongo documents have a sixteen megabyte limit and 1:21 a few hundred children documents could start to stretch the parent to the limit. 1:25 At that point, it might make sense to create a separate collection for 1:30 the children and store their ID's in an array in the parent. 1:34 While this eases the size of the parent document, 1:38 it also results in more transactions to the database. 1:42 Now that the data has been broken up into several documents. 1:46 ID transaction numbers reduced performance. 1:50 Keep such tradeoffs in mind as you structure your applications data. 1:53 If you expect the number of children to be in the thousands, it may be possible that 1:58 the parent will reach the 16 megabyte limit with the IDs alone. 2:02 In this case, you'd want to remove the array of children from the parent document 2:07 and store the parent's ID in each of its children. 2:12 For a deeper look at this topic, 2:16 I've supplied some links in the teacher's notes. 2:18 In our API, we won't be expecting more than 20 answers per question. 2:21 We can store the entire array for each question in a single question document. 2:25
You need to sign up for Treehouse in order to download course files.Sign up