diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.chinese.md index 2133bfdc83..307cf41549 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.chinese.md @@ -1,17 +1,19 @@ --- id: 587d7fb9367417b2b2512c12 title: Chain Search Query Helpers to Narrow Search Results -localeTitle: 链搜索查询帮助缩小搜索结果 challengeType: 2 +forumTopicId: 301533 +localeTitle: 通过链式调用搜索查询辅助函数来缩小搜索结果 --- ## Description -
0如果未将回调作为Model.find()(或其他搜索方法)的最后一个参数传递,则不执行查询。您可以将查询存储在变量中以供以后使用。这种对象使您可以使用链接语法构建查询。当您最终链接方法.exec()时,将执行实际的数据库搜索。将回调传递给最后一个方法。有很多查询助手,这里我们将使用最“着名”的助手。 0找到的人喜欢“burrito”。按名称对它们进行排序,将结果限制为两个文档,并隐藏它们的年龄。链.find(),. sort(),. limit(),。select(),然后是.exec()。将done(错误,数据)回调传递给exec()。 +
+如果不给 Model.find()(或者别的搜索方法)的最后一个参数传入回调函数, 查询将不会执行。你可以将查询条件存储在变量中供以后使用,我们也可以通过链式调用这类变量来构建新的查询字段。实际的数据库操作会在最终调用 .exec() 方法时执行。需要注意的是,你必须给 exec() 传一个回调方法。Mongoose 为我们提供了许多查询辅助函数, 这里我们使用最著名的一种。
## Instructions
- +找出喜欢 burrito 的人并按 name 进行排序。同时,我们需要隐藏他们的 age 属性。结果应限制在两个 document 内。请链式调用 .find().sort().limit().select()。最后调用 .exec(),并传入给它传入 done(err, data) 回调函数。
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.chinese.md index 98bc41c8c1..1511e90aeb 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.chinese.md @@ -1,29 +1,45 @@ --- id: 587d7fb6367417b2b2512c07 title: Create a Model -localeTitle: 创建一个模型 challengeType: 2 +forumTopicId: 301535 +localeTitle: 创建一个模型 --- ## Description -
0首先,我们需要一个Schema。每个模式都映射到MongoDB集合。它定义了该集合中文档的形状。 0模式是模型的构建块。它们可以嵌套来创建复杂的模型,但在这种情况下,我们会保持简单。 0模型允许您创建对象的实例,称为文档。 0创建一个拥有此原型的人: -- Person Prototype - --------------------- -name : string [required] -age : number -favoriteFoods : array of strings (*) 0使用mongoose基本模式类型。如果需要,还可以添加0个字段,使用简单的验证器,如required或unique, 0并设置默认值。请参阅mongoose文档 。 -[C] RUD第一部分 - 创建0注意:Glitch是一个真实的服务器,在真实服务器中,与db的交互发生在处理函数中。当某些事件发生时执行这些功能(例如某人在您的API上命中端点)。我们将在这些练习中遵循相同的方法。 done()函数是一个回调,告诉我们在完成插入,搜索,更新或删除等异步操作后我们可以继续。它遵循Node约定,应该在成功时调用done(null,data),或者在出错时调用(err)。 0警告 - 与远程服务交互时,可能会发生错误! -/* Example */ -var someFunc = function(done) { -//... do something (risky) ... -if(error) return done(error); -done(null, result); -}; +
+CRUD 第一小节—CREATE + +首先,我们需要一个 Schema(架构、模式或纲要,详情请参阅维基百科),每一个 Schema 都对应一个 MongoDB 的 collection,并且在相应的 collection 里定义 documents 的“样子”。 +Schema 用于组成 Model(模型),我们甚至可以通过嵌套 Schema 来创建复杂的模型。但是这里我们只学习 Schema 的基础用法。 +我们可以根据模型创建实例,模型实例化后的对象称为 documents。 + +Repl.it 是一个真实的服务器。习惯上,我们会编写函数来处理外界与服务器中数据库的交互,这些函数会在特定事件发生(比如有人调用了我们的服务器 API)时执行。接下来的挑战题目即是以此为基础。done() 是一个回调函数,它的作用是在一个异步操作(比如对数据库进行插入、搜索、更新或删除)执行完成时,通知我们可以继续执行后续的其它代码。这与 Node.js 中的处理方式十分类似。在 Node.js 中,我们会在(异步操作)成功时调用 done(null, data),在失败时调用 done(err)。 + +注意:与远程服务器进行交互时,我们需要考虑到发生错误的可能 + +```js +/* 示例 */ + +var someFunc = function(done) { + //... do something (risky) ... + if(error) return done(error); + done(null, result); +}; +``` +
+ ## Instructions
- +以此为原型创建一个 Person: +
+- Person 的原型 -
+--------------------
+name : string [required]
+age : number
+favoriteFoods : array of strings (*)
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model.chinese.md index 3e42748cad..d98c67dc47 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-and-save-a-record-of-a-model.chinese.md @@ -1,21 +1,28 @@ --- id: 587d7fb6367417b2b2512c09 title: Create and Save a Record of a Model -localeTitle: 创建并保存模型记录 challengeType: 2 +forumTopicId: 301536 +localeTitle: 创建并保存模型记录 --- ## Description -
0使用之前构建的Person构造函数创建文档实例。将具有字段名称,年龄和favoriteFoods的对象传递给构造函数。它们的类型必须符合Person Schema中的类型。然后在返回的文档实例上调用方法document.save()。使用Node约定传递回调。这是一种常见模式,以下所有CRUD方法都将这样的回调函数作为最后一个参数。 -/* Example */ -// ... -person.save(function(err, data) { -// ...do your stuff here... -}); +
+在这个挑战中,你需要给之前创建的模型添加一条数据。
## Instructions
+用我们在上一个挑战中写好的 Person 构造函数创建 document 实例:将包含 nameagefavoriteFoods 的对象传给构造函数。它们的数据类型必须符合我们在 Person 原型中定义的类型。然后在返回的 document 实例上调用方法 document.save()。同时,我们需要像之前提到过的那样,为它传一个回调函数。在 Mongoose 中,所有的 CRUD 方法接收的最后一个参数都是回调函数。 + +```js +/* 示例 */ + +// ... +person.save(function(err, data) { + // ...do your stuff here... +}); +```
@@ -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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.chinese.md index f88f2deb3b..9b335707aa 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.chinese.md @@ -1,17 +1,20 @@ --- id: 587d7fb7367417b2b2512c0a title: Create Many Records with model.create() -localeTitle: 使用model.create()创建许多记录 +forumTopicId: 301537 challengeType: 2 +localeTitle: 使用model.create()创建许多记录 --- ## Description -
0有时您需要创建模型的许多实例,例如,在使用初始数据为数据库播种时。 Model.create()接受一个对象数组,如[{name:'John',...},{...},...]作为第一个参数,并将它们全部保存在db中。使用函数参数arrayOfPeople使用Model.create()创建许多人。 +
+在一些情况下,比如进行数据库初始化,你会需要创建很多 model 实例来用作初始数据。Model.create() 接受一组像 [{name:'John', ...}, {...}, ...] 的数组作为第一个参数,并将其保存到数据库。
## Instructions -
- +
+修改 createManyPeople 方法,使用 arrayOfPeople 作为 Model.create() 的参数来创建多个 people 实例。 +注意:你可以使用在上一个挑战中创建的 model 来完成当前挑战。
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-many-documents-with-model.remove.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-many-documents-with-model.remove.chinese.md index 537bf309c1..028d829efb 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-many-documents-with-model.remove.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-many-documents-with-model.remove.chinese.md @@ -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 -
-Model.remove()用于删除与给定条件匹配的所有文档。使用Model.remove()删除名称为“Mary”的所有人员。将其传递给查询文档,其中设置了“name”字段,当然还有回调。 0注意:Model.remove()不返回已删除的文档,而是返回包含操作结果和受影响项目数的JSON对象。不要忘记将它传递给done()回调,因为我们在测试中使用它。 +
+Model.remove() 可以用于删除符合给定匹配条件的所有 document。
## Instructions -
- +
+如果想要删除所有叫 "Mary" 的人,我们可以使用 Model.remove(),并给它传入一个包含 name 字段的对象作为查询条件。当然,我们还需要给它传入一个回调函数。 +注意:Model.remove() 不会返回被删除的 document,而是会返回一个包含操作结果以及受影响的数据数量的 JSON 对象。不要忘记将它传入 done() 回调,因为我们需要在挑战的测试中调用它。
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-one-document-using-model.findbyidandremove.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-one-document-using-model.findbyidandremove.chinese.md index c6337bb633..6e7b9e54e1 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-one-document-using-model.findbyidandremove.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/delete-one-document-using-model.findbyidandremove.chinese.md @@ -1,16 +1,18 @@ --- id: 587d7fb8367417b2b2512c10 title: Delete One Document Using model.findByIdAndRemove -localeTitle: 使用model.findByIdAndRemove删除一个文档 challengeType: 2 +forumTopicId: 301539 +localeTitle: 使用 model.findByIdAndRemove 删除一个 document --- ## Description -
0通过她的_id删除一个人。您应该使用findByIdAndRemove()或findOneAndRemove()方法之一。它们与之前的更新方法类似。他们将删除的文件传递给cb。像往常一样,使用函数参数personId作为搜索关键字。 +
+如果想在我们的数据库中通过 _id 删除一个人的数据,我们可以使用 findByIdAndRemove()findOneAndRemove() 方法,它的使用方式和上面的更新方法很像。在这个挑战中,请使用 personId 找到对应的数据并把它删除。
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.chinese.md index e71dbe23a5..4e18c14292 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.chinese.md @@ -1,16 +1,22 @@ --- id: 587d7fb6367417b2b2512c06 title: Install and Set Up Mongoose -localeTitle: 安装和设置Mongoose challengeType: 2 +forumTopicId: 301540 +localeTitle: 安装和设置 Mongoose --- ## Description -
0将mongodb和mongoose添加到项目的package.json中。然后需要猫鼬。将mLab数据库URI作为MONGO_URI存储在私有.env文件中。使用mongoose.connect连接到数据库( ) +
+在 package.json 文件中添加 mongodb 和 mongoose 作为项目依赖,然后引入 Mongoose。之后,将 MongoDB Atlas 的 URI 作为 MONGO_URI 字段存储在私有的 .env 文件中。然后使用单引号或双引号包裹 URI,最后通过以下的代码片段来连接数据库: + +```js +mongoose.connect(, { useNewUrlParser: true, useUnifiedTopology: true }); +``` +
-## Instructions -
+
@@ -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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-classic-updates-by-running-find-edit-then-save.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-classic-updates-by-running-find-edit-then-save.chinese.md index 594bc8fd54..2a727185d7 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-classic-updates-by-running-find-edit-then-save.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-classic-updates-by-running-find-edit-then-save.chinese.md @@ -1,17 +1,22 @@ --- id: 587d7fb8367417b2b2512c0e title: 'Perform Classic Updates by Running Find, Edit, then Save' -localeTitle: '通过运行查找,编辑然后保存来执行经典更新' challengeType: 2 +forumTopicId: 301541 +localeTitle: '通过执行查找、编辑、保存来执行更新' --- ## Description -
0在过去的好时光中,如果您想编辑文档并能够以某种方式使用它,例如在服务器响应中将其发回,则需要执行此操作。 Mongoose有一个专用的更新方法:Model.update()。它与低级mongo驱动程序绑定。它可以批量编辑符合特定条件的许多文档,但它不会发回更新的文档,只会发送“状态”消息。此外,它使模型验证变得困难,因为它只是直接调用mongo驱动程序。 0使用参数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) +
+ +在传统的应用中,如果想要编辑 document 并在其它地方使用它(比如放到服务器的返回数据中),就必须执行查找、编辑和保存。Mongoose 有一个专用的更新方法 Model.update(),它与底层的 mongo 驱动绑定。通过这个方法,我们可以批量编辑符合特定条件的多个 document。但问题在于,这个方法不会返回更新后的 document,而是返回状态信息。此外,由于它是直接调用 mongo 的底层驱动,因此它让处理 model 的验证变得更加棘手。
## Instructions -
+
+在这个挑战中,请使用参数 personId 作为字段,在数据库中通过 _id 找到相应的 person(你可以使用之前挑战中的任何一种方法)。然后,将 "hamburger" 添加到它的 favoriteFoods 中(你可以使用 Array.push())。之后,在 .find() 的回调里通过 .save() 方法更新数据库。 + +提示:如果你在 Schema 中将 favoriteFoods 声明为一个 Array(数组)并且没有指定数组的类型(如 [String]),那么此时,favoriteFoods 就会是默认的 Mixed 类型。如果想编辑它,就必须执行 document.markModified('edited-field')。请参阅 [Mongoose 文档](https://mongoosejs.com/docs/schematypes.html#Mixed)
@@ -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); })" - ```
@@ -36,7 +40,11 @@ tests:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-new-updates-on-a-document-using-model.findoneandupdate.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-new-updates-on-a-document-using-model.findoneandupdate.chinese.md index eb39a6976a..1255d5e459 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-new-updates-on-a-document-using-model.findoneandupdate.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/perform-new-updates-on-a-document-using-model.findoneandupdate.chinese.md @@ -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 -
0最新版本的mongoose具有简化文档更新的方法。一些更高级的功能(即前/后挂钩,验证)与此方法的行为不同,因此Classic方法在许多情况下仍然有用。在按Id搜索时可以使用findByIdAndUpdate()。 0按名称查找人员并将其年龄设置为20.使用函数参数personName作为搜索关键字。 0提示:我们希望您返回更新的文档。为此,您需要将选项文档{new:true}作为findOneAndUpdate()的第三个参数传递。默认情况下,这些方法返回未修改的对象。 +
+最近发布的 mongoose 版本简化了 document 的更新方式,但同时,一些高级功能(如 pre/post hook, 验证)的使用方式也变得和以前不同。因此,在很多情景下,上一个挑战中提到的老方法其实更常用。新方法的加入,可以让我们使用 findByIdAndUpdate() 来进行基于 Id 的搜索。
## Instructions -
- +
+用 personName 作为 Name 的查找条件,并将查到的 person 年龄设为 20 岁。 +提示:你需要返回更新后的 document。你可以把 findOneAndUpdate() 的第三个参数设置为 {new: true}。默认情况下,这个方法会返回修改前的数据。
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.chinese.md index 3cf71f5cd2..472b23db4a 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.chinese.md @@ -1,16 +1,19 @@ --- id: 587d7fb7367417b2b2512c0b title: Use model.find() to Search Your Database -localeTitle: 使用model.find()搜索数据库 challengeType: 2 +forumTopicId: 301543 +localeTitle: 使用model.find()搜索数据库 --- ## Description -
0使用Model.find() - > [Person]查找具有给定名称的所有人0在最简单的用法中,Model.find()接受查询文档(JSON对象)作为第一个参数,然后接受回调。它返回一个匹配数组。它支持极其广泛的搜索选项。在文档中查看它。使用函数参数personName作为搜索关键字。 +
+使用 Model.find() -> [Person] 找出符合名字查询条件的所有人。 +Model.find() 接收一个查询 document(一个 JSON 对象)作为第一个参数,然后第二个参数是一个回调函数,它会返回由匹配到的数据组成的数组。这个方法支持很多搜索选项,详情请参阅文档。在这个挑战中,请使用 personName 作为搜索条件。
## 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:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id.chinese.md index 21d62decb7..5feb1004b1 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id.chinese.md @@ -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 -
0保存文档时,mongodb会自动添加字段_id,并将其设置为唯一的字母数字键。按_id搜索是一种非常频繁的操作,因此mongoose为它提供了一种专用方法。使用Model.findById() - > Person找到具有给定_id的(仅!!)人物。使用函数参数personId作为搜索关键字。 +
+在保存 document 的时候,MongoDB 会自动为它添加 _id 字段,并给该字段设置一个唯一的仅包含数字和字母的值。通过 _id 搜索是一个十分常见的操作,为此,Mongoose 提供了一个专门的方法。
## Instructions -
- +
+执行Model.findById() -> Person,使用 personId 作为搜索参数,根据 _id 找到对应的(唯一的)person。
## 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); })" - ```
@@ -35,7 +36,11 @@ tests:
```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> +
diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.chinese.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.chinese.md index c033e872b8..34f0e10737 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.chinese.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.chinese.md @@ -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 -
-Model.findOne()的行为类似于.find(),但它只返回一个文档(不是数组),即使有多个项目也是如此。在按声明为唯一的属性进行搜索时,它尤其有用。使用Model.findOne() - > Person,找到一个在她的收藏夹中有某种食物的人。使用函数参数food作为搜索键。 +
+Model.findOne()Model.find() 十分类似。但就算数据库中有很多条数据可以匹配查找条件,Model.findOne() 也只返回一个 document,而不会返回一个数组。如果查找的条件是一个值唯一的属性,Model.findOne() 会更加适用。
## Instructions -
- +
+用一个特定的 food 作为 Model.findOne() -> Person 的查找参数,来寻找喜欢食物列表中包含给定食物的某一个人。
## 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:
```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> +