fix(i18n): update Chinese translation of install and set up mongoose (#38901)

This commit is contained in:
ZhichengChen
2020-08-16 04:50:44 +08:00
committed by GitHub
parent f35e7de03e
commit d7cd3f7777
12 changed files with 222 additions and 91 deletions

View File

@ -1,17 +1,20 @@
---
id: 587d7fb9367417b2b2512c12
title: Chain Search Query Helpers to Narrow Search Results
localeTitle: 链搜索查询帮助缩小搜索结果
challengeType: 2
isHidden: false
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> 方法。 有很多的查询助手, 这里我们使用最 '著名' 的一种。
</section>
## Instructions
<section id='instructions'>
找到喜欢 "burrito" 的人,按照名字排序, 限制结果为两个 document, 并且隐藏他们的年龄。 使用链路 <code>.find()</code>, <code>.sort()</code>, <code>.limit()</code>, <code>.select()</code>, 然后 <code>.exec()</code>。将 <code>done(err, data)</code> 回调传给 <code>exec()</code> 方法。
</section>
## Tests
@ -19,7 +22,7 @@ challengeType: 2
```yml
tests:
- text: 链接查询助手应该成功
- text: Chaining query helpers should succeed
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,6 +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>

View File

@ -1,21 +1,42 @@
---
id: 587d7fb6367417b2b2512c07
title: Create a Model
localeTitle: 创建一个模型
challengeType: 2
isHidden: false
localeTitle: 创建一个模型
---
## Description
<section id='description'> <code>0</code>首先我们需要一个Schema。每个模式都映射到MongoDB集合。它定义了该集合中文档的形状。 <code>0</code>模式是模型的构建块。它们可以嵌套来创建复杂的模型,但在这种情况下,我们会保持简单。 <code>0</code>模型允许您创建对象的实例,称为文档。 <code>0</code>创建一个拥有此原型的人:
<section id='description'>
<b>C</b>RUD 之 - CREATE 创建
首先,我们需要一个 Schema每一个 Schema 对应一个 MongoDB collection并且在那个 collection 里面定义 documents 的模型。
Schemas 是 Models 的构建块。它们可以嵌套来创建复杂的模型,但是这里,我们只学习简单的用法。
Model 可以被实例化,实例化后的对象称为 documents。
注意: Glitch 是一个真实的服务,并且通过 handler 函数和 db 进行交互。 这些函数通过一些事件去触发(例如:有人从终端调用了你的 API我们在这些练习中遵循同样的方法。 比如,我们在完成 nserting、searching、updating 或者 deleting 这样的异步操作后接着回调<code>done()</code>函数。它遵循 Node 的惯例,需要在 success 时回调<code>done(null, data)</code>,在 error 时回调<code>done(err)</code>
Warning - 当与远程服务器交互时可能发生错误!
```js
/* Example */
var someFunc = function(done) {
//... do something (risky) ...
if(error) return done(error);
done(null, result);
};
```
<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>favoriteFoods : array of strings (*) </code>
你可以使用基础的 SchemaTypes 去添加更多的字段,比如使用 required 或者 unique 这样的简单验证去设置默认值。参考 <a href='http://mongoosejs.com/docs/guide.html'>Mongoose 文档</a>
[C]RUD Part I - CREATE
<code>/* 示例 */</code>
<code>var someFunc = function(done) {</code>
<code>//... do something (risky) ...</code>
<code> // 执行一些可能产生错误的代码</code>
<code> if(error) return done(error);</code>
<code> done(null, result);</code>
<code>};</code>
@ -23,7 +44,18 @@ challengeType: 2
## Instructions
<section id='instructions'>
创建一个拥有以下 Prototype 的 Person 对象:
<blockquote>
- Person Prototype -<br>
--------------------<br>
name : string [required]<br>
age : number<br>
favoriteFoods : array of strings (*)
</blockquote>
你可以使用基础的 SchemaTypes 去添加更多的字段,
比如使用 required 或者 unique 这样的简单验证去设置默认值。
参考 <a href='http://mongoosejs.com/docs/guide.html'>Mongoose 文档</a>
</section>
## Tests
@ -31,7 +63,7 @@ challengeType: 2
```yml
tests:
- text: 从mongoose模式创建实例应该会成功
- text: 成功创建一个 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,6 +79,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>

View File

@ -1,21 +1,28 @@
---
id: 587d7fb6367417b2b2512c09
title: Create and Save a Record of a Model
localeTitle: 创建并保存模型记录
challengeType: 2
isHidden: false
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 的 constructor构造器函数可以创建一个 document 对象,该对象包含<code>name</code><code>age</code><code>favoriteFoods</code>字段。这些字段的类型必须符合 Person Schema 里面定义的类型。然后调用<code>document.save()</code>。使用 Node 惯例传递 callback。通常情况下所有的 CRUD增查改删方法都会像下面一样作为最后一个参数去执行一个<code>callback()</code>
```js
/* Example */
// ...
person.save(function(err, data) {
// ...do your stuff here...
});
```
</section>
@ -24,9 +31,8 @@ challengeType: 2
```yml
tests:
- text: 创建和保存数据库项应该会成功
- text: 成功创建一条 db 并保存。
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); })'
```
</section>
@ -40,6 +46,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>

View File

@ -1,17 +1,19 @@
---
id: 587d7fb7367417b2b2512c0a
title: Create Many Records with model.create()
localeTitle: 使用model.create创建许多记录
challengeType: 2
isHidden: false
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>arrayOfPeople</code>作为<code>Model.create()</code>的参数创建很多个 people 实例。
</section>
## Tests
@ -19,7 +21,7 @@ challengeType: 2
```yml
tests:
- text: 一次创建多个数据库项应该会成功
- text: 应当可以一次创建多个 item项目
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,6 +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>

View File

@ -1,18 +1,20 @@
---
id: 587d7fb8367417b2b2512c11
title: Delete Many Documents with model.remove()
localeTitle: 使用model.remove删除许多文档
challengeType: 2
isHidden: false
localeTitle: 使用 model.remove() 删除许多文档
---
## 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>。 传入一个设置了 “name” 字段的 document 作为查询条件,当然还需要一个回调函数。
注意: <code>Model.remove()</code> 不会返回被删除的 document, 但是会返回一个包含操作结果和受影响的 item 数量的 JSON 对象。 不要忘记将它传递给 <code>done()</code> 回调,因为我们在测试中使用它。
</section>
## Tests
@ -20,7 +22,7 @@ Model.remove用于删除与给定条件匹配的所有文档。使用Model
```yml
tests:
- text: 一次删除多个项目应该会成功
- text: 一次性成功的删除多个 item(项目)。
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,6 +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>

View File

@ -1,12 +1,15 @@
---
id: 587d7fb8367417b2b2512c10
title: Delete One Document Using model.findByIdAndRemove
localeTitle: 使用model.findByIdAndRemove删除一个文档
challengeType: 2
isHidden: false
forumTopicId: 301539
localeTitle: 使用 model.findByIdAndRemove() 删除一个文档
---
## Description
<section id='description'> <code>0</code>通过她的_id删除一个人。您应该使用findByIdAndRemove或findOneAndRemove方法之一。它们与之前的更新方法类似。他们将删除的文件传递给cb。像往常一样使用函数参数personId作为搜索关键字。
<section id='description'>
你可以使用 <code>findByIdAndRemove()</code> 或者 <code>findOneAndRemove()</code> 方法, 通过 _id 删除一个人员。 它和上面的更新方法很像,都是先通过 personId 找到对应的 document再删除并返回被删除的 document。
</section>
## Instructions
@ -19,7 +22,7 @@ challengeType: 2
```yml
tests:
- text: 删除项目应该会成功
- text: 成功删除一个 item(项目)。
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,6 +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>

View File

@ -1,12 +1,19 @@
---
id: 587d7fb6367417b2b2512c06
title: Install and Set Up Mongoose
localeTitle: 安装和设置Mongoose
challengeType: 2
isHidden: false
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 依赖,将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后<code>require('mongoose')</code>,使用<code>mongoose.connect(<Your URI>)</code>命令来连接数据库。
```js
mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true });
```
</section>
## Instructions
@ -19,11 +26,11 @@ challengeType: 2
```yml
tests:
- text: '"mongodb"依赖应该在package.json'
- text: "在 package.json 文件中应该有 'mongodb' 依赖。"
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: "在 package.json 文件中应该有 'mongoose' 依赖。"
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,6 +46,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>

View File

@ -1,18 +1,24 @@
---
id: 587d7fb8367417b2b2512c0e
title: 'Perform Classic Updates by Running Find, Edit, then Save'
localeTitle: '通过运行查找,编辑然后保存来执行经典更新'
challengeType: 2
isHidden: false
localeTitle: 通过运行 find、edit、save 来执行经典更新
---
## 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 的驱动程序。
</section>
## Instructions
<section id='instructions'>
使用参数 personId 作为搜索关键字,然后通过 _id 找到一个 person使用上述任何一种方法. 将 “hamburger” 添加到她的 favoriteFoods 列中去 (可以使用 <code>Array.push()</code>)。然后在 <code>.find()</code> 的回调里通过 <code>.save()</code> 方法更新。
[*] 提示: 如果你在 Schema 中将 favoriteFoods 声明为一个 Array(数组), 而没有指定数组的类型(如:[String]),结果会让人很意外。 在这种情况下favoriteFoods 默认为 Mixed 类型。如果想要编辑它,就必须执行 <code>document.markModified'edited-field'</code>。(http://mongoosejs.com/docs/schematypes.html - #Mixed)
</section>
## Tests
@ -20,9 +26,8 @@ challengeType: 2
```yml
tests:
- text: 查找 - 编辑 - 更新项目应该成功
- text: 对一个 item项目的 Find-edit-update 操作成功
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,6 +41,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>

View File

@ -1,17 +1,21 @@
---
id: 587d7fb8367417b2b2512c0f
title: Perform New Updates on a Document Using model.findOneAndUpdate()
localeTitle: 使用model.findOneAndUpdate对文档执行新更新
challengeType: 2
isHidden: false
forumTopicId: 301542
localeTitle: 使用 model.findOneAndUpdate() 对文档执行新的更新
---
## 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 的最新版本简化了 documents 的更新。 但是一些高级的用法 (比如 pre/post 钩子, 验证) 更复杂, 所以老方法更常用。当通过 Id 进行搜索时还可以使用 <code>findByIdAndUpdate()</code>
</section>
## Instructions
<section id='instructions'>
使用 personName 作为搜索的关键词查找 person并将查到的 person 的年龄设为 20 岁。
提示: 我们想要你返更新后的 document你可以把 <code>findOneAndUpdate()</code> 的第三个参数设置为 <code>{new: true}</code>。 默认情况下,这些方法返回未被修改的数据。
</section>
## Tests
@ -19,7 +23,7 @@ challengeType: 2
```yml
tests:
- text: findOneAndUpdate项应该成功
- text: 对一个 item项目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,6 +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>

View File

@ -1,12 +1,16 @@
---
id: 587d7fb7367417b2b2512c0b
title: Use model.find() to Search Your Database
localeTitle: 使用model.find搜索数据库
challengeType: 2
isHidden: false
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 对象)作为第一参数,然后是回调。它将返回匹配到的项目组成的数组。这个支持极其广泛的搜索选项。使用人名作为搜索的关键词,来校验它。
</section>
## Instructions
@ -19,7 +23,7 @@ challengeType: 2
```yml
tests:
- text: 找到与标准对应的所有项目都应该成功
- text: 成功找到所有符合条件的 item项目
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,6 +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>

View File

@ -1,17 +1,19 @@
---
id: 587d7fb7367417b2b2512c0d
title: Use model.findById() to Search Your Database By _id
localeTitle: 使用model.findById按_id搜索数据库
challengeType: 2
isHidden: false
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 自动添加 _id 字段,并给该字段设置 unique唯一属性。通过 _id 搜索是一个非常频繁的操作,所以 Mongose 为它提供了一个专门的方法。
</section>
## Instructions
<section id='instructions'>
使用人物 Id 作为参数,执行<code>Model.findById() -> Person</code>,找到这个 _id 对应的唯一的一个人。
</section>
## Tests
@ -19,9 +21,8 @@ challengeType: 2
```yml
tests:
- text: 找到Id应该成功的项目
- text: 通过 Id 成功找到对应的 item项目
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,6 +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>

View File

@ -1,18 +1,20 @@
---
id: 587d7fb7367417b2b2512c0c
title: Use model.findOne() to Return a Single Matching Document from Your Database
localeTitle: 使用model.findOne从数据库中返回单个匹配文档
challengeType: 2
isHidden: false
forumTopicId: 301545
localeTitle: 使用 model.findOne() 从数据库中返回一个匹配的文档
---
## Description
<section id='description'>
Model.findOne()的行为类似于.find但它只返回一个文档不是数组即使有多个项目也是如此。在按声明为唯一的属性进行搜索时它尤其有用。使用Model.findOne - &gt; Person找到一个在她的收藏夹中有某种食物的人。使用函数参数food作为搜索键。
<code>Model.findOne()</code>表现像<code>Model.find()</code>,但是它仅仅返回一个 document而不是一个数组即使数据库里有很多条 item项目。当你按声明成<code>unique</code>的属性进行搜索时,<code>Model.findOne()</code>尤其有用。
</section>
## Instructions
<section id='instructions'>
把 food 作为<code>Model.findOne() -> Person</code>的参数,来找到一个在她的爱好中有某一食物的人。
</section>
## Tests
@ -20,7 +22,7 @@ Model.findOne的行为类似于.find但它只返回一个文档
```yml
tests:
- text: 找一个项应该成功
- text: 成功找到一个 item项目
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,6 +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>