fix(curriculum) Replace <code> with <blockquote>for code sections (#35442)
* fix: reformatted code and moved instructions * fix: replaced code with blockquotes * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: made a few recommended changes of text * fix: moved </blockquote> to new line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: removed extra space Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: added the word The before code tag section
This commit is contained in:
@ -6,13 +6,12 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
If you don’t pass the callback as the last argument to Model.find() (or to the other search methods), the query is not executed. You can store the query in a variable for later use. This kind of object enables you to build up a query using chaining syntax. The actual db search is executed when you finally chain the method .exec(). Pass your callback to this last method. There are many query helpers, here we’ll use the most ‘famous’ ones.
|
||||
Find people who like "burrito". Sort them by name, limit the results to two documents, and hide their age. Chain .find(), .sort(), .limit(), .select(), and then .exec(). Pass the done(err, data) callback to exec().
|
||||
If you don’t pass the callback as the last argument to <code>Model.find()</code> (or to the other search methods), the query is not executed. You can store the query in a variable for later use. This kind of object enables you to build up a query using chaining syntax. The actual db search is executed when you finally chain the method <code>.exec()</code>. You always need to pass your callback to this last method. There are many query helpers, here we’ll use the most ‘famous’ ones.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Find people who like <code>burrito</code>. Sort them by name, limit the results to two documents, and hide their age. Chain <code>.find()</code>, <code>.sort()</code>, <code>.limit()</code>, <code>.select()</code>, and then <code>.exec()</code>. Pass the <code>done(err, data)</code> callback to <code>exec()</code>.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,32 +6,38 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<b>C</b>RUD Part I - CREATE
|
||||
|
||||
First of all we need a Schema. Each schema maps to a MongoDB collection. It defines the shape of the documents within that collection.
|
||||
Schemas are building block for Models. They can be nested to create complex models, but in this case we’ll keep things simple.
|
||||
A model allows you to create instances of your objects, called documents.
|
||||
Create a person having this prototype :
|
||||
<code>- Person Prototype -</code>
|
||||
<code>--------------------</code>
|
||||
<code>name : string [required]</code>
|
||||
<code>age : number</code>
|
||||
<code>favoriteFoods : array of strings (*) </code>
|
||||
Use the mongoose basic schema types. If you want you can also add
|
||||
more fields, use simple validators like required or unique,
|
||||
and set default values. See the <a href='http://mongoosejs.com/docs/guide.html'>mongoose docs</a>.
|
||||
[C]RUD Part I - CREATE
|
||||
Note: Glitch is a real server, and in real servers the interactions with the db happen in handler functions. These function are executed when some event happens (e.g. someone hits an endpoint on your API). We’ll follow the same approach in these exercises. The done() function is a callback that tells us that we can proceed after completing an asynchronous operation such as inserting, searching, updating or deleting. It’s following the Node convention and should be called as done(null, data) on success, or done(err) on error.
|
||||
Warning - When interacting with remote services, errors may occur !
|
||||
<code>/* Example */</code>
|
||||
<code>var someFunc = function(done) {</code>
|
||||
<code> //... do something (risky) ...</code>
|
||||
<code> if(error) return done(error);</code>
|
||||
<code> done(null, result);</code>
|
||||
<code>};</code>
|
||||
|
||||
Glitch is a real server, and in real servers the interactions with the db happen in handler functions. These function are executed when some event happens (e.g. someone hits an endpoint on your API). We’ll follow the same approach in these exercises. The <code>done()</code> function is a callback that tells us that we can proceed after completing an asynchronous operation such as inserting, searching, updating or deleting. It’s following the Node convention and should be called as <code>done(null, data)</code> on success, or <code>done(err)</code> on error.
|
||||
Warning - When interacting with remote services, errors may occur!
|
||||
<blockquote>
|
||||
/* Example */<br><br>
|
||||
var someFunc = function(done) {<br>
|
||||
//... do something (risky) ...<br>
|
||||
if(error) return done(error);<br>
|
||||
done(null, result);<br>
|
||||
};
|
||||
</blockquote>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a person having this prototype :
|
||||
<blockquote>
|
||||
- Person Prototype -<br>
|
||||
--------------------<br>
|
||||
name : string [required]<br>
|
||||
age : number<br>
|
||||
favoriteFoods : array of strings (*)
|
||||
</blockquote>
|
||||
|
||||
Use the mongoose basic schema types. If you want you can also add
|
||||
more fields, use simple validators like required or unique,
|
||||
and set default values. See the <a href='http://mongoosejs.com/docs/guide.html'>mongoose docs</a>.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,17 +6,19 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Create a document instance using the Person constructor you built before. Pass an object to the constructor with the fields name, age, and favoriteFoods. Their types must conform to the ones in the Person Schema. Then call the method document.save() on the returned document instance. Pass it a callback using the Node convention you saw before. This is a common pattern, all the following CRUD methods take a callback function like this as the last argument.
|
||||
<code>/* Example */</code>
|
||||
<code>// ...</code>
|
||||
<code>person.save(function(err, data) {</code>
|
||||
<code>// ...do your stuff here...</code>
|
||||
<code>});</code>
|
||||
In this challenge you will have to create and save a record of a model.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create a document instance using the <code>Person</code> constructor you built before. Pass to the constructor an object having the fields <code>name</code>, <code>age</code>, and <code>favoriteFoods</code>. Their types must be conformant to the ones in the Person Schema. Then call the method <code>document.save()</code> on the returned document instance. Pass to it a callback using the Node convention. This is a common pattern, all the following CRUD methods take a callback function like this as the last argument.
|
||||
<blockquote>
|
||||
/* Example */<br><br>
|
||||
// ...<br>
|
||||
person.save(function(err, data) {<br>
|
||||
// ...do your stuff here...<br>
|
||||
});
|
||||
</blockquote>
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,12 +6,12 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sometimes you need to create many instances of your models, e.g. when seeding a database with initial data. Model.create() takes an array of objects like [{name: 'John', ...}, {...}, ...] as the first argument, and saves them all in the db. Create many people with Model.create(), using the function argument arrayOfPeople.
|
||||
Sometimes you need to create many instances of your models, e.g. when seeding a database with initial data. <code>Model.create()</code> takes an array of objects like <code>[{name: 'John', ...}, {...}, ...]</code> as the first argument, and saves them all in the db.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create many people with <code>Model.create()</code>, using the function argument <code>arrayOfPeople</code>.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,13 +6,13 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Model.remove() is useful to delete all the documents matching given criteria. Delete all the people whose name is “Mary”, using Model.remove(). Pass it to a query document with the “name” field set, and of course a callback.
|
||||
Note: Model.remove() doesn’t return the deleted document, but a JSON object containing the outcome of the operation, and the number of items affected. Don’t forget to pass it to the done() callback, since we use it in tests.
|
||||
<code>Model.remove()</code> is useful to delete all the documents matching given criteria.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Delete all the people whose name is “Mary”, using <code>Model.remove()</code>. Pass it to a query document with the <code>name</code> field set, and of course a callback.
|
||||
<strong>Note:</strong> The <code>Model.remove()</code> doesn’t return the deleted document, but a JSON object containing the outcome of the operation, and the number of items affected. Don’t forget to pass it to the <code>done()</code> callback, since we use it in tests.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,7 +6,7 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Delete one person by her _id. You should use one of the methods findByIdAndRemove() or findOneAndRemove(). They are like the previous update methods. They pass the removed document to the cb. As usual, use the function argument personId as search key.
|
||||
Delete one person by the person's <code>_id</code>. You should use one of the methods <code>findByIdAndRemove()</code> or <code>findOneAndRemove()</code>. They are like the previous update methods. They pass the removed document to the cb. As usual, use the function argument <code>personId</code> as the search key.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,10 +6,11 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Add mongodb and mongoose to the project’s package.json. Then require mongoose. Store your mLab database URI in the private .env file as MONGO_URI. Connect to the database using mongoose.connect(<Your URI>)
|
||||
Add mongodb and mongoose to the project’s package.json. Then require mongoose. Store your mLab database URI in the private <code>.env</code> file as MONGO_URI. Connect to the database using <code>mongoose.connect(<Your URI>)</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
Add mongodb and mongoose to the project’s <code>package.json</code>. Then require mongoose. Store your mLab database URI in the private <code>.env</code> file as <code>MONGO_URI</code>. Connect to the database using <code>mongoose.connect(<Your URI>)</code>
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
@ -6,13 +6,14 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In the good old days this was what you needed to do if you wanted to edit a document and be able to use it somehow e.g. sending it back in a server response. Mongoose has a dedicated updating method : Model.update(). It is bound to the low-level mongo driver. It can bulk edit many documents matching certain criteria, but it doesn’t send back the updated document, only a ‘status’ message. Furthermore it makes model validations difficult, because it just directly calls the mongo driver.
|
||||
Find a person by _id ( use any of the above methods ) with the parameter personId as search key. Add “hamburger” to the list of her favoriteFoods (you can use Array.push()). Then - inside the find callback - save() the updated Person.
|
||||
[*] Hint: This may be tricky if in your Schema you declared favoriteFoods as an Array, without specifying the type (i.e. [String]). In that casefavoriteFoods defaults to Mixed type, and you have to manually mark it as edited using document.markModified('edited-field'). (http://mongoosejs.com/docs/schematypes.html - #Mixed )
|
||||
In the good old days this was what you needed to do if you wanted to edit a document and be able to use it somehow e.g. sending it back in a server response. Mongoose has a dedicated updating method : <code>Model.update()</code>. It is bound to the low-level mongo driver. It can bulk edit many documents matching certain criteria, but it doesn’t send back the updated document, only a ‘status’ message. Furthermore it makes model validations difficult, because it just directly calls the mongo driver.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Find a person by <code>_id</code> ( use any of the above methods ) with the parameter <code>personId</code> as search key. Add "hamburger" to the list of the person's <code>favoriteFoods</code> (you can use <code>Array.push()</code>). Then - inside the find callback - <code>save()</code> the updated <code>Person</code>.
|
||||
|
||||
<strong>Note:</strong> This may be tricky, if in your Schema, you declared <code>favoriteFoods</code> as an Array, without specifying the type (i.e. <code>[String]</code>). In that <code>casefavoriteFoods</code> defaults to Mixed type, and you have to manually mark it as edited using <code>document.markModified('edited-field')</code>. See Mongoose documentation at https://mongoosejs.com/docs/schematypes.html#Mixed
|
||||
|
||||
</section>
|
||||
|
||||
|
@ -6,14 +6,13 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Recent versions of mongoose have methods to simplify documents updating. Some more advanced features (i.e. pre/post hooks, validation) behave differently with this approach, so the Classic method is still useful in many situations. findByIdAndUpdate() can be used when searching by Id.
|
||||
Find a person by Name and set her age to 20. Use the function parameter personName as search key.
|
||||
Hint: We want you to return the updated document. To do that you need to pass the options document { new: true } as the 3rd argument to findOneAndUpdate(). By default these methods return the unmodified object.
|
||||
Recent versions of mongoose have methods to simplify documents updating. Some more advanced features (i.e. pre/post hooks, validation) behave differently with this approach, so the Classic method is still useful in many situations. <code>findByIdAndUpdate()</code> can be used when searching by Id.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Find a person by <code>Name</code> and set the person's age to 20. Use the function parameter <code>personName</code> as search key.
|
||||
<strong>Note:</strong> You should return the updated document. To do that you need to pass the options document <code>{ new: true }</code> as the 3rd argument to <code>findOneAndUpdate()</code>. By default these methods return the unmodified object.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,8 +6,8 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Find all the people having a given name, using Model.find() -> [Person]
|
||||
In its simplest usage, Model.find() accepts a query document (a JSON object ) as the first argument, then a callback. It returns an array of matches. It supports an extremely wide range of search options. Check it in the docs. Use the function argument personName as search key.
|
||||
Find all the people having a given name, using <code>Model.find() -> [Person]</code>
|
||||
In its simplest usage, <code>Model.find()</code> accepts a query document (a JSON object) as the first argument, then a callback. It returns an array of matches. It supports an extremely wide range of search options. Check it in the docs. Use the function argument <code>personName</code> as search key.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,12 +6,12 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
When saving a document, mongodb automatically adds the field _id, and set it to a unique alphanumeric key. Searching by _id is an extremely frequent operation, so mongoose provides a dedicated method for it. Find the (only!!) person who has the given _id using Model.findById() -> Person. Use the function argument personId as search key.
|
||||
When saving a document, mongodb automatically adds the field <code>_id</code>, and set it to a unique alphanumeric key. Searching by <code>_id</code> is an extremely frequent operation, so mongoose provides a dedicated method for it.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Find the (only!!) person having a given <code>_id</code>, using <code>Model.findById() -> Person</code>. Use the function argument <code>personId</code> as the search key.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,12 +6,12 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Model.findOne() behaves like .find(), but it returns only one document (not an array), even if there are multiple items. It is especially useful when searching by properties that you have declared as unique. Find just one person which has a certain food in her favorites, using Model.findOne() -> Person. Use the function argument food as search key.
|
||||
<code>Model.findOne()</code> behaves like <code>.find()</code>, but it returns only one document (not an array), even if there are multiple items. It is especially useful when searching by properties that you have declared as unique.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Find just one person which has a certain food in the person's favorites, using <code>Model.findOne() -> Person</code>. Use the function argument food as search key.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
Reference in New Issue
Block a user