feat(i18n/Chinese): add translation of install and set up mongoose (1/4) (#39539)

This commit is contained in:
Xing Liu
2020-09-17 03:53:54 -07:00
committed by GitHub
parent 7865d0d0f0
commit 3c223c2c7a
12 changed files with 200 additions and 101 deletions

View File

@ -1,17 +1,19 @@
---
id: 587d7fb9367417b2b2512c12
title: Chain Search Query Helpers to Narrow Search Results
localeTitle: 链搜索查询帮助缩小搜索结果
challengeType: 2
forumTopicId: 301533
localeTitle: 通过链式调用搜索查询辅助函数来缩小搜索结果
---
## Description
<section id='description'> <code>0</code>如果未将回调作为Model.find或其他搜索方法的最后一个参数传递则不执行查询。您可以将查询存储在变量中以供以后使用。这种对象使您可以使用链接语法构建查询。当您最终链接方法.exec将执行实际的数据库搜索。将回调传递给最后一个方法。有很多查询助手这里我们将使用最“着名”的助手。 <code>0</code>找到的人喜欢“burrito”。按名称对它们进行排序将结果限制为两个文档并隐藏它们的年龄。链.find,. sort,. limit。select然后是.exec。将done错误数据回调传递给exec
<section id='description'>
如果不给 <code>Model.find()</code>(或者别的搜索方法)的最后一个参数传入回调函数, 查询将不会执行。你可以将查询条件存储在变量中供以后使用,我们也可以通过链式调用这类变量来构建新的查询字段。实际的数据库操作会在最终调用 <code>.exec()</code> 方法时执行。需要注意的是,你必须给 <code>exec()</code> 传一个回调方法。Mongoose 为我们提供了许多查询辅助函数, 这里我们使用最著名的一种。
</section>
## Instructions
<section id='instructions'>
找出喜欢 <code>burrito</code> 的人并按 <code>name</code> 进行排序。同时,我们需要隐藏他们的 <code>age</code> 属性。结果应限制在两个 document 内。请链式调用 <code>.find()</code><code>.sort()</code><code>.limit()</code><code>.select()</code>。最后调用 <code>.exec()</code>,并传入给它传入 <code>done(err, data)</code> 回调函数。
</section>
## Tests
@ -19,7 +21,7 @@ challengeType: 2
```yml
tests:
- text: 链接查询助手应该成功
- text: 链接查询辅助函数应该成功
testString: 'getUserInput => $.ajax({url: getUserInput(''url'') + ''/_api/query-tools'', type: ''POST'', contentType:''application/json'', data: JSON.stringify([{name: ''Pablo'', age: 26, favoriteFoods: [''burrito'', ''hot-dog'']}, {name: ''Bob'', age: 23, favoriteFoods: [''pizza'', ''nachos'']}, {name: ''Ashley'', age: 32, favoriteFoods: [''steak'', ''burrito'']}, {name: ''Mario'', age: 51, favoriteFoods: [''burrito'', ''prosciutto'']} ]) }).then(data => { assert.isArray(data, ''the response should be an Array''); assert.equal(data.length, 2, ''the data array length is not what expected''); assert.notProperty(data[0], ''age'', ''The returned first item has too many properties''); assert.equal(data[0].name, ''Ashley'', ''The returned first item name is not what expected''); assert.notProperty(data[1], ''age'', ''The returned second item has too many properties''); assert.equal(data[1].name, ''Mario'', ''The returned second item name is not what expected'');}, xhr => { throw new Error(xhr.responseText); })'
```
@ -35,7 +37,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,29 +1,45 @@
---
id: 587d7fb6367417b2b2512c07
title: Create a Model
localeTitle: 创建一个模型
challengeType: 2
forumTopicId: 301535
localeTitle: 创建一个模型
---
## Description
<section id='description'> <code>0</code>首先我们需要一个Schema。每个模式都映射到MongoDB集合。它定义了该集合中文档的形状。 <code>0</code>模式是模型的构建块。它们可以嵌套来创建复杂的模型,但在这种情况下,我们会保持简单。 <code>0</code>模型允许您创建对象的实例,称为文档。 <code>0</code>创建一个拥有此原型的人:
<code>- Person Prototype -</code>
<code>--------------------</code>
<code>name : string [required]</code>
<code>age : number</code>
<code>favoriteFoods : array of strings (*)</code> <code>0</code>使用mongoose基本模式类型。如果需要还可以添加<code>0</code>个字段使用简单的验证器如required或unique <code>0</code>并设置默认值。请参阅<a href='http://mongoosejs.com/docs/guide.html'>mongoose文档</a>
[C] RUD第一部分 - 创建<code>0</code>注意Glitch是一个真实的服务器在真实服务器中与db的交互发生在处理函数中。当某些事件发生时执行这些功能例如某人在您的API上命中端点。我们将在这些练习中遵循相同的方法。 done函数是一个回调告诉我们在完成插入搜索更新或删除等异步操作后我们可以继续。它遵循Node约定应该在成功时调用donenulldata或者在出错时调用err<code>0</code>警告 - 与远程服务交互时,可能会发生错误!
<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>
<section id='description'>
<b>C</b>RUD 第一小节—CREATE
首先,我们需要一个 Schema架构、模式或纲要详情请参阅<a href='https://en.wikipedia.org/wiki/Database_schema'>维基百科</a>),每一个 Schema 都对应一个 MongoDB 的 <a href='https://docs.mongodb.com/manual/reference/glossary/#term-collection'>collection</a>,并且在相应的 collection 里定义 <a href='https://docs.mongodb.com/manual/reference/glossary/#term-document'>documents</a> 的“样子”。
Schema 用于组成 Model模型我们甚至可以通过嵌套 Schema 来创建复杂的模型。但是这里我们只学习 Schema 的基础用法。
我们可以根据模型创建实例,模型实例化后的对象称为 documents。
Repl.it 是一个真实的服务器。习惯上,我们会编写函数来处理外界与服务器中数据库的交互,这些函数会在特定事件发生(比如有人调用了我们的服务器 API时执行。接下来的挑战题目即是以此为基础。<code>done()</code> 是一个回调函数,它的作用是在一个异步操作(比如对数据库进行插入、搜索、更新或删除)执行完成时,通知我们可以继续执行后续的其它代码。这与 Node.js 中的处理方式十分类似。在 Node.js 中,我们会在(异步操作)成功时调用 <code>done(null, data)</code>,在失败时调用 <code>done(err)</code>
注意:与远程服务器进行交互时,我们需要考虑到发生错误的可能
```js
/* 示例 */
var someFunc = function(done) {
//... do something (risky) ...
if(error) return done(error);
done(null, result);
};
```
</section>
## Instructions
<section id='instructions'>
以此为原型创建一个 Person
<blockquote>
- Person 的原型 -<br>
--------------------<br>
name : string [required]<br>
age : number<br>
favoriteFoods : array of strings (*)
</section>
## Tests
@ -31,7 +47,7 @@ challengeType: 2
```yml
tests:
- text: 从mongoose模式创建实例应该会成功
- text: 应当成功地通过 Mongoose schema 创建实例
testString: 'getUserInput => $.post(getUserInput(''url'') + ''/_api/mongoose-model'', {name: ''Mike'', age: 28, favoriteFoods: [''pizza'', ''cheese'']}).then(data => { assert.equal(data.name, ''Mike'', ''"model.name" is not what expected''); assert.equal(data.age, ''28'', ''"model.age" is not what expected''); assert.isArray(data.favoriteFoods, ''"model.favoriteFoods" is not an Array''); assert.include(data.favoriteFoods, ''pizza'', ''"model.favoriteFoods" does not include the expected items''); assert.include(data.favoriteFoods, ''cheese'', ''"model.favoriteFoods" does not include the expected items''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -47,7 +63,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,21 +1,28 @@
---
id: 587d7fb6367417b2b2512c09
title: Create and Save a Record of a Model
localeTitle: 创建并保存模型记录
challengeType: 2
forumTopicId: 301536
localeTitle: 创建并保存模型记录
---
## Description
<section id='description'> <code>0</code>使用之前构建的Person构造函数创建文档实例。将具有字段名称年龄和favoriteFoods的对象传递给构造函数。它们的类型必须符合Person Schema中的类型。然后在返回的文档实例上调用方法document.save。使用Node约定传递回调。这是一种常见模式以下所有CRUD方法都将这样的回调函数作为最后一个参数。
<code>/* Example */</code>
<code>// ...</code>
<code>person.save(function(err, data) {</code>
<code>// ...do your stuff here...</code>
<code>});</code>
<section id='description'>
在这个挑战中,你需要给之前创建的模型添加一条数据。
</section>
## Instructions
<section id='instructions'>
用我们在上一个挑战中写好的 Person 构造函数创建 document 实例:将包含 <code>name</code><code>age</code><code>favoriteFoods</code> 的对象传给构造函数。它们的数据类型必须符合我们在 Person 原型中定义的类型。然后在返回的 document 实例上调用方法 <code>document.save()</code>。同时,我们需要像之前提到过的那样,为它传一个回调函数。在 Mongoose 中,所有的 CRUD 方法接收的最后一个参数都是回调函数。
```js
/* 示例 */
// ...
person.save(function(err, data) {
// ...do your stuff here...
});
```
</section>
@ -24,7 +31,7 @@ challengeType: 2
```yml
tests:
- text: 创建和保存数据库项应该会成功
- text: 应成功地创建数据,并保存数据库
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/create-and-save-person'').then(data => { assert.isString(data.name, ''"item.name" should be a String''); assert.isNumber(data.age, ''28'', ''"item.age" should be a Number''); assert.isArray(data.favoriteFoods, ''"item.favoriteFoods" should be an Array''); assert.equal(data.__v, 0, ''The db item should be not previously edited''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -40,7 +47,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,17 +1,20 @@
---
id: 587d7fb7367417b2b2512c0a
title: Create Many Records with model.create()
localeTitle: 使用model.create创建许多记录
forumTopicId: 301537
challengeType: 2
localeTitle: 使用model.create创建许多记录
---
## Description
<section id='description'> <code>0</code>有时您需要创建模型的许多实例,例如,在使用初始数据为数据库播种时。 Model.create接受一个对象数组如[{name'John'...}{...}...]作为第一个参数并将它们全部保存在db中。使用函数参数arrayOfPeople使用Model.create创建许多人。
<section id='description'>
在一些情况下,比如进行数据库初始化,你会需要创建很多 model 实例来用作初始数据。<code>Model.create()</code> 接受一组像 <code>[{name:'John', ...}, {...}, ...]</code> 的数组作为第一个参数,并将其保存到数据库。
</section>
## Instructions
<section id='instructions'>
修改 <code>createManyPeople</code> 方法,使用 <code>arrayOfPeople</code> 作为 <code>Model.create()</code> 的参数来创建多个 people 实例。
<strong>注意:</strong>你可以使用在上一个挑战中创建的 model 来完成当前挑战。
</section>
## Tests
@ -19,7 +22,7 @@ challengeType: 2
```yml
tests:
- text: 一次创建多个数据库项应该会成功
- text: 应当成功地一次创建多个数据
testString: 'getUserInput => $.ajax({url: getUserInput(''url'') + ''/_api/create-many-people'', type: ''POST'', contentType:''application/json'', data: JSON.stringify([{name: ''John'', age: 24, favoriteFoods: [''pizza'', ''salad'']}, {name: ''Mary'', age: 21, favoriteFoods: [''onions'', ''chicken'']}])}).then(data => { assert.isArray(data, ''the response should be an array''); assert.equal(data.length, 2, ''the response does not contain the expected number of items''); assert.equal(data[0].name, ''John'', ''The first item is not correct''); assert.equal(data[0].__v, 0, ''The first item should be not previously edited''); assert.equal(data[1].name, ''Mary'', ''The second item is not correct''); assert.equal(data[1].__v, 0, ''The second item should be not previously edited''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -35,7 +38,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,18 +1,20 @@
---
id: 587d7fb8367417b2b2512c11
title: Delete Many Documents with model.remove()
localeTitle: 使用model.remove删除许多文档
challengeType: 2
forumTopicId: 301538
localeTitle: 使用 model.remove() 删除多个 document
---
## Description
<section id='description'>
Model.remove用于删除与给定条件匹配的所有文档。使用Model.remove删除名称为“Mary”的所有人员。将其传递给查询文档其中设置了“name”字段当然还有回调。 <code>0</code>注意Model.remove不返回已删除的文档而是返回包含操作结果和受影响项目数的JSON对象。不要忘记将它传递给done回调因为我们在测试中使用它。
<code>Model.remove()</code> 可以用于删除符合给定匹配条件的所有 document。
</section>
## Instructions
<section id='instructions'>
如果想要删除所有叫 "Mary" 的人,我们可以使用 <code>Model.remove()</code>,并给它传入一个包含 <code>name</code> 字段的对象作为查询条件。当然,我们还需要给它传入一个回调函数。
<strong>注意:</strong><code>Model.remove()</code> 不会返回被删除的 document而是会返回一个包含操作结果以及受影响的数据数量的 JSON 对象。不要忘记将它传入 <code>done()</code> 回调,因为我们需要在挑战的测试中调用它。
</section>
## Tests
@ -20,7 +22,8 @@ Model.remove用于删除与给定条件匹配的所有文档。使用Model
```yml
tests:
- text: 一次删除多个项目应该会成功
- text: Deleting many items at once should succeed
- text: 应成功地删除多条数据
testString: 'getUserInput => $.ajax({url: getUserInput(''url'') + ''/_api/remove-many-people'', type: ''POST'', contentType:''application/json'', data: JSON.stringify([{name: ''Mary'', age: 16, favoriteFoods: [''lollipop'']}, {name: ''Mary'', age: 21, favoriteFoods: [''steak'']}])}).then(data => { assert.isTrue(!!data.ok, ''The mongo stats are not what expected''); assert.equal(data.n, 2, ''The number of items affected is not what expected''); assert.equal(data.count, 0, ''the db items count is not what expected''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -36,7 +39,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,12 +1,14 @@
---
id: 587d7fb8367417b2b2512c10
title: Delete One Document Using model.findByIdAndRemove
localeTitle: 使用model.findByIdAndRemove删除一个文档
challengeType: 2
forumTopicId: 301539
localeTitle: 使用 model.findByIdAndRemove 删除一个 document
---
## Description
<section id='description'> <code>0</code>通过她的_id删除一个人。您应该使用findByIdAndRemove或findOneAndRemove方法之一。它们与之前的更新方法类似。他们将删除的文件传递给cb。像往常一样使用函数参数personId作为搜索关键字。
<section id='description'>
如果想在我们的数据库中通过 <code>_id</code> 删除一个人的数据,我们可以使用 <code>findByIdAndRemove()</code><code>findOneAndRemove()</code> 方法,它的使用方式和上面的更新方法很像。在这个挑战中,请使用 <code>personId</code> 找到对应的数据并把它删除。
</section>
## Instructions
@ -19,7 +21,7 @@ challengeType: 2
```yml
tests:
- text: 删除项目应该会成功
- text: 应当成功地删除一条数据
testString: 'getUserInput => $.post(getUserInput(''url'') + ''/_api/remove-one-person'', {name:''Jason Bourne'', age: 36, favoriteFoods:[''apples'']}).then(data => { assert.equal(data.name, ''Jason Bourne'', ''item.name is not what expected''); assert.equal(data.age, 36, ''item.age is not what expected''); assert.deepEqual(data.favoriteFoods, [''apples''], ''item.favoriteFoods is not what expected''); assert.equal(data.__v, 0); assert.equal(data.count, 0, ''the db items count is not what expected''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -35,7 +37,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,15 +1,21 @@
---
id: 587d7fb6367417b2b2512c06
title: Install and Set Up Mongoose
localeTitle: 安装和设置Mongoose
challengeType: 2
forumTopicId: 301540
localeTitle: 安装和设置 Mongoose
---
## Description
<section id='description'> <code>0</code>将mongodb和mongoose添加到项目的package.json中。然后需要猫鼬。将mLab数据库URI作为MONGO_URI存储在私有.env文件中。使用mongoose.connect连接到数据库 <Your URI>
<section id='description'>
在 package.json 文件中添加 mongodb 和 mongoose 作为项目依赖,然后引入 Mongoose。之后将 MongoDB Atlas 的 URI 作为 MONGO_URI 字段存储在私有的 <code>.env</code> 文件中。然后使用单引号或双引号包裹 URI最后通过以下的代码片段来连接数据库
```js
mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true });
```
</section>
## Instructions
<section id='instructions'>
</section>
@ -19,11 +25,11 @@ challengeType: 2
```yml
tests:
- text: '"mongodb"依赖应该在package.json'
- text: '"mongodb" 应在 package.json 中作为依赖项定义'
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/file/package.json'').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, ''mongodb''); }, xhr => { throw new Error(xhr.responseText); })'
- text: '"mongoose"依赖应该在package.json'
- text: '"mongoose" 应在 package.json 中作为依赖项定义'
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/file/package.json'').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, ''mongoose''); }, xhr => { throw new Error(xhr.responseText); })'
- text: '"mongoose"应该连接数据库'
- text: '应使用 "mongoose" 连接数据库'
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/_api/is-mongoose-ok'').then(data => {assert.isTrue(data.isMongooseOk, ''mongoose is not connected'')}, xhr => { throw new Error(xhr.responseText); })'
```
@ -39,7 +45,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,17 +1,22 @@
---
id: 587d7fb8367417b2b2512c0e
title: 'Perform Classic Updates by Running Find, Edit, then Save'
localeTitle: '通过运行查找,编辑然后保存来执行经典更新'
challengeType: 2
forumTopicId: 301541
localeTitle: '通过执行查找、编辑、保存来执行更新'
---
## Description
<section id='description'> <code>0</code>在过去的好时光中,如果您想编辑文档并能够以某种方式使用它,例如在服务器响应中将其发回,则需要执行此操作。 Mongoose有一个专用的更新方法Model.update。它与低级mongo驱动程序绑定。它可以批量编辑符合特定条件的许多文档但它不会发回更新的文档只会发送“状态”消息。此外它使模型验证变得困难因为它只是直接调用mongo驱动程序。 <code>0</code>使用参数personId作为搜索关键字通过_id使用上述任何方法查找人物。将“hamburger”添加到她最喜欢的食物列表中您可以使用Array.push。然后 - 在find回调中 - save更新的Person。
[*]提示如果你的Schema中你将favoriteFoods声明为一个数组而没有指定类型即[String]这可能会很棘手。在那种情况下favorsFoods默认为Mixed type你必须使用document.markModified'edited-field')手动将其标记为已编辑。 http://mongoosejs.com/docs/schematypes.html - #Mixed
<section id='description'>
在传统的应用中,如果想要编辑 document 并在其它地方使用它比如放到服务器的返回数据中就必须执行查找、编辑和保存。Mongoose 有一个专用的更新方法 <code>Model.update()</code>,它与底层的 mongo 驱动绑定。通过这个方法,我们可以批量编辑符合特定条件的多个 document。但问题在于这个方法不会返回更新后的 document而是返回状态信息。此外由于它是直接调用 mongo 的底层驱动,因此它让处理 model 的验证变得更加棘手。
</section>
## Instructions
<section id='instructions'>
在这个挑战中,请使用参数 <code>personId</code> 作为字段,在数据库中通过 <code>_id</code> 找到相应的 person你可以使用之前挑战中的任何一种方法。然后将 "hamburger" 添加到它的 <code>favoriteFoods</code> 中(你可以使用 <code>Array.push()</code>)。之后,在 <code>.find()</code> 的回调里通过 <code>.save()</code> 方法更新数据库。
<strong>提示:</strong>如果你在 Schema 中将 <code>favoriteFoods</code> 声明为一个 Array数组并且没有指定数组的类型(如 <code>[String]</code>),那么此时,<code>favoriteFoods</code> 就会是默认的 Mixed 类型。如果想编辑它,就必须执行 <code>document.markModified('edited-field')</code>。请参阅 [Mongoose 文档](https://mongoosejs.com/docs/schematypes.html#Mixed)
</section>
@ -20,9 +25,8 @@ challengeType: 2
```yml
tests:
- text: 查找 - 编辑 - 更新项目应该成功
- text: 应成功地对一条数据进行查找、编辑和更新
testString: "getUserInput => $.post(getUserInput('url') + '/_api/find-edit-save', {name:'Poldo', age: 40, favoriteFoods:['spaghetti']}).then(data => { assert.equal(data.name, 'Poldo', 'item.name is not what is expected'); assert.equal(data.age, 40, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['spaghetti', 'hamburger'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 1, 'The item should be previously edited'); }, xhr => { throw new Error(xhr.responseText); })"
```
</section>
@ -36,7 +40,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,17 +1,20 @@
---
id: 587d7fb8367417b2b2512c0f
title: Perform New Updates on a Document Using model.findOneAndUpdate()
localeTitle: 使用model.findOneAndUpdate对文档执行新更新
challengeType: 2
forumTopicId: 301542
localeTitle: 使用 model.findOneAndUpdate() 对更新 document
---
## Description
<section id='description'> <code>0</code>最新版本的mongoose具有简化文档更新的方法。一些更高级的功能即前/后挂钩验证与此方法的行为不同因此Classic方法在许多情况下仍然有用。在按Id搜索时可以使用findByIdAndUpdate<code>0</code>按名称查找人员并将其年龄设置为20.使用函数参数personName作为搜索关键字。 <code>0</code>提示:我们希望您返回更新的文档。为此,您需要将选项文档{newtrue}作为findOneAndUpdate的第三个参数传递。默认情况下这些方法返回未修改的对象。
<section id='description'>
最近发布的 mongoose 版本简化了 document 的更新方式,但同时,一些高级功能(如 pre/post hook, 验证)的使用方式也变得和以前不同。因此,在很多情景下,上一个挑战中提到的老方法其实更常用。新方法的加入,可以让我们使用 <code>findByIdAndUpdate()</code> 来进行基于 Id 的搜索。
</section>
## Instructions
<section id='instructions'>
<code>personName</code> 作为 <code>Name</code> 的查找条件,并将查到的 person 年龄设为 20 岁。
<strong>提示:</strong>你需要返回更新后的 document。你可以把 <code>findOneAndUpdate()</code> 的第三个参数设置为 <code>{new: true}</code>。默认情况下,这个方法会返回修改前的数据。
</section>
## Tests
@ -19,7 +22,7 @@ challengeType: 2
```yml
tests:
- text: findOneAndUpdate项应该成功
- text: 应成功地使用 findOneAndUpdate 更新数据
testString: 'getUserInput => $.post(getUserInput(''url'') + ''/_api/find-one-update'', {name:''Dorian Gray'', age: 35, favoriteFoods:[''unknown'']}).then(data => { assert.equal(data.name, ''Dorian Gray'', ''item.name is not what expected''); assert.equal(data.age, 20, ''item.age is not what expected''); assert.deepEqual(data.favoriteFoods, [''unknown''], ''item.favoriteFoods is not what expected''); assert.equal(data.__v, 0, ''findOneAndUpdate does not increment version by design !!!''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -35,7 +38,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,12 +1,15 @@
---
id: 587d7fb7367417b2b2512c0b
title: Use model.find() to Search Your Database
localeTitle: 使用model.find搜索数据库
challengeType: 2
forumTopicId: 301543
localeTitle: 使用model.find搜索数据库
---
## Description
<section id='description'> <code>0</code>使用Model.find - &gt; [Person]查找具有给定名称的所有人<code>0</code>在最简单的用法中Model.find接受查询文档JSON对象作为第一个参数然后接受回调。它返回一个匹配数组。它支持极其广泛的搜索选项。在文档中查看它。使用函数参数personName作为搜索关键字。
<section id='description'>
使用 <code>Model.find() -> [Person]</code> 找出符合名字查询条件的所有人。
<code>Model.find()</code> 接收一个查询 document一个 JSON 对象)作为第一个参数,然后第二个参数是一个回调函数,它会返回由匹配到的数据组成的数组。这个方法支持很多搜索选项,详情请参阅文档。在这个挑战中,请使用 <code>personName</code> 作为搜索条件。
</section>
## Instructions
@ -19,7 +22,7 @@ challengeType: 2
```yml
tests:
- text: 找到与标准对应的所有项目都应该成功
- text: 应成功地找到所有符合条件的数据
testString: 'getUserInput => $.post(getUserInput(''url'') + ''/_api/find-all-by-name'', {name: ''r@nd0mN4m3'', age: 24, favoriteFoods: [''pizza'']}).then(data => { assert.isArray(data, ''the response should be an Array''); assert.equal(data[0].name, ''r@nd0mN4m3'', ''item.name is not what expected''); assert.equal(data[0].__v, 0, ''The item should be not previously edited''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -35,7 +38,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,17 +1,19 @@
---
id: 587d7fb7367417b2b2512c0d
title: Use model.findById() to Search Your Database By _id
localeTitle: 使用model.findById按_id搜索数据库
challengeType: 2
forumTopicId: 301544
localeTitle: 使用 model.findById() 方法,根据 _id 来搜索数据
---
## Description
<section id='description'> <code>0</code>保存文档时mongodb会自动添加字段_id并将其设置为唯一的字母数字键。按_id搜索是一种非常频繁的操作因此mongoose为它提供了一种专用方法。使用Model.findById - &gt; Person找到具有给定_id的仅!!人物。使用函数参数personId作为搜索关键字。
<section id='description'>
在保存 document 的时候MongoDB 会自动为它添加 <code>_id</code> 字段,并给该字段设置一个唯一的仅包含数字和字母的值。通过 <code>_id</code> 搜索是一个十分常见的操作为此Mongoose 提供了一个专门的方法。
</section>
## Instructions
<section id='instructions'>
执行<code>Model.findById() -> Person</code>,使用 <code>personId</code> 作为搜索参数,根据 <code>_id</code> 找到对应的唯一的person。
</section>
## Tests
@ -19,9 +21,8 @@ challengeType: 2
```yml
tests:
- text: 找到Id应该成功的项目
- text: 应成功地根据 Id 找到对应的数据
testString: "getUserInput => $.get(getUserInput('url') + '/_api/find-by-id').then(data => { assert.equal(data.name, 'test', 'item.name is not what expected'); assert.equal(data.age, 0, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['none'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })"
```
</section>
@ -35,7 +36,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>

View File

@ -1,18 +1,19 @@
---
id: 587d7fb7367417b2b2512c0c
title: Use model.findOne() to Return a Single Matching Document from Your Database
localeTitle: 使用model.findOne从数据库中返回单个匹配文档
challengeType: 2
forumTopicId: 301545
localeTitle: 使用 model.findOne() 从数据库中返回一个匹配的 document
---
## Description
<section id='description'>
Model.findOne()的行为类似于.find但它只返回一个文档不是数组即使有多个项目也是如此。在按声明为唯一的属性进行搜索时它尤其有用。使用Model.findOne - &gt; Person找到一个在她的收藏夹中有某种食物的人。使用函数参数food作为搜索键。
<code>Model.findOne()</code><code>Model.find()</code> 十分类似。但就算数据库中有很多条数据可以匹配查找条件,<code>Model.findOne()</code> 也只返回一个 document而不会返回一个数组。如果查找的条件是一个值唯一的属性<code>Model.findOne()</code> 会更加适用。
</section>
## Instructions
<section id='instructions'>
用一个特定的 food 作为 <code>Model.findOne() -> Person</code> 的查找参数,来寻找喜欢食物列表中包含给定食物的某一个人。
</section>
## Tests
@ -20,7 +21,7 @@ Model.findOne的行为类似于.find但它只返回一个文档
```yml
tests:
- text: 找一个项应该成功
- text: 应成功地找到一个数据
testString: 'getUserInput => $.post(getUserInput(''url'') + ''/_api/find-one-by-food'', {name: ''Gary'', age: 46, favoriteFoods: [''chicken salad'']}).then(data => { assert.equal(data.name, ''Gary'', ''item.name is not what expected''); assert.deepEqual(data.favoriteFoods, [''chicken salad''], ''item.favoriteFoods is not what expected''); assert.equal(data.__v, 0, ''The item should be not previously edited''); }, xhr => { throw new Error(xhr.responseText); })'
```
@ -36,7 +37,11 @@ tests:
<section id='solution'>
```js
// solution required
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
/section>
</section>