From ab222e31e7a158944b78b929f6470168476ce6b4 Mon Sep 17 00:00:00 2001 From: Steven Ding <1139274654@qq.com> Date: Wed, 13 Jan 2021 00:31:18 +0800 Subject: [PATCH] an audit for some changes (#40680) --- ...-query-helpers-to-narrow-search-results.md | 22 +++++++++++-- .../mongodb-and-mongoose/create-a-model.md | 31 +++++++++++++------ .../create-many-records-with-model.create.md | 9 ++++++ .../use-model.find-to-search-your-database.md | 19 ++++++++++-- ...le-matching-document-from-your-database.md | 15 +++++++-- ...le-matching-document-from-your-database.md | 2 +- 6 files changed, 80 insertions(+), 18 deletions(-) diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.md index 2e185b9452..cf3d3f5892 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.md @@ -7,11 +7,20 @@ forumTopicId: 301533 # --description-- -如果不给 `Model.find()`(或者别的搜索方法)的最后一个参数传入回调函数, 查询将不会执行。你可以将查询条件存储在变量中供以后使用,我们也可以通过链式调用这类变量来构建新的查询字段。实际的数据库操作会在最终调用 `.exec()` 方法时执行。需要注意的是,你必须给 `exec()` 传一个回调方法。Mongoose 为我们提供了许多查询辅助函数, 这里我们使用最著名的一种。 +如果不给 `Model.find()`(或者别的搜索方法)的最后一个参数传入回调函数, 查询将不会执行。你可以将查询条件存储在变量中供以后使用,我们也可以通过链式调用这类变量来构建新的查询字段。实际的数据库操作会在最终调用 `.exec()` 方法时执行。需要注意的是,你必须给 `exec()` 传一个回调方法。并且你必须把回调函数传给最后一个方法。Mongoose 为我们提供了许多查询辅助函数, 这里我们使用最著名的一种。 # --instructions-- -找出喜欢 `burrito` 的人并按 `name` 进行排序。同时,我们需要隐藏他们的 `age` 属性。结果应限制在两个 document 内。请链式调用 `.find()`、`.sort()`、`.limit()` 和 `.select()`。最后调用 `.exec()`,并传入给它传入 `done(err, data)` 回调函数。 +修改 `queryChain` 函数来用变量 `foodToSearch` 来查询喜欢指定食物的人。找出喜欢 `burrito` 的人并按 `name` 进行排序。同时,我们需要隐藏他们的 `age` 属性。结果应限制在两个 document 内。请链式调用 `.find()`、`.sort()`、`.limit()` 和 `.select()`,在最后调用 `.exec()` 并给它传入 `done(err, data)` 回调函数。 + +## 拓展阅读 + +如果你想进一步了解,你可以参看: + +- 索引( Indexes, 对查询效率非常重要 ), +- Pre/Post hooks, +- Validation, +- Schema 虚拟、模型、静态和实例方法 # --hints-- @@ -64,5 +73,14 @@ forumTopicId: 301533 ); ``` +# --seed-- + # --solutions-- +```js +/** + 后端挑战不需要 solutions, + 因为他们需要使用一个完整的项目进行测试。 + 请查询我们的贡献指南以了解更多信息。 +*/ +``` diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.md index b8d395c99d..bdf1b9daa1 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.md @@ -7,11 +7,11 @@ forumTopicId: 301535 # --description-- -**C**RUD 第一小节—CREATE +**C**RUD 第一小节——CREATE 首先,我们需要一个 Schema(架构、模式或纲要,详情请参阅[维基百科](https://en.wikipedia.org/wiki/Database_schema)),每一个 Schema 都对应一个 MongoDB 的 [collection](https://docs.mongodb.com/manual/reference/glossary/#term-collection),并且在相应的 collection 里定义 [documents](https://docs.mongodb.com/manual/reference/glossary/#term-document) 的“样子”。 Schema 用于组成 Model(模型),我们甚至可以通过嵌套 Schema 来创建复杂的模型。但是这里我们只学习 Schema 的基础用法。 我们可以根据模型创建实例,模型实例化后的对象称为 documents。 -Repl.it 是一个真实的服务器。习惯上,我们会编写函数来处理外界与服务器中数据库的交互,这些函数会在特定事件发生(比如有人调用了我们的服务器 API)时执行。接下来的挑战题目即是以此为基础。`done()` 是一个回调函数,它的作用是在一个异步操作(比如对数据库进行插入、搜索、更新或删除)执行完成时,通知我们可以继续执行后续的其它代码。这与 Node.js 中的处理方式十分类似。在 Node.js 中,我们会在(异步操作)成功时调用 `done(null, data)`,在失败时调用 `done(err)`。 +Repl.it 是一个真实的服务器。习惯上,我们会编写函数来处理外界与服务器中数据库的交互,这些函数会在特定事件发生(比如有人调用了我们的服务器 API)时执行。接下来的挑战题目即是以此为基础。`done()` 是一个回调函数,它的作用是在一个异步操作(比如对数据库进行插入、查询、更新或删除)执行完成时,通知我们可以继续执行后续的其它代码。这与 Node.js 中的处理方式十分类似,在 Node.js 中,我们会在(异步操作)成功时调用 `done(null, data)`,在失败时调用 `done(err)`。 注意:与远程服务器进行交互时,我们需要考虑到发生错误的可能 @@ -27,15 +27,19 @@ var someFunc = function(done) { # --instructions-- -以此为原型创建一个 Person: +按下面的原型信息创建一个名为 `personSchema` 的 schema: -
-- Person 的原型 -
---------------------
-name : string [required]
-age : number
+```markup +- Person 的原型 - +-------------------- +name : string [required] +age : number favoriteFoods : array of strings (*) -
+``` + +采用 Mongoose 基础 schema 类型。你如果还想添加更多的键,就请使用 required 或 unique 等简单的验证器(validators),并设置默认值。详情请参考 [Mongoose 文档](http://mongoosejs.com/docs/guide.html)。 + +请从 `personSchema` 创建一个名为 `Person` 的 model。 # --hints-- @@ -72,5 +76,14 @@ favoriteFoods : array of strings (*) ); ``` +# --seed-- + # --solutions-- +```js +/** + 后端挑战不需要 solutions, + 因为他们需要使用一个完整的项目进行测试。 + 请查询我们的贡献指南以了解更多信息。 +*/ +``` diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.md index 2257a335c4..2e5e70c850 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create.md @@ -56,5 +56,14 @@ forumTopicId: 301537 ); ``` +# --seed-- + # --solutions-- +```js +/** + 后端挑战不需要 solutions, + 因为他们需要使用一个完整的项目进行测试。 + 请查询我们的贡献指南以了解更多信息。 +*/ +``` diff --git a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.md index da42551920..3358548191 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.find-to-search-your-database.md @@ -1,15 +1,19 @@ --- id: 587d7fb7367417b2b2512c0b -title: 使用model.find()搜索数据库 +title: 使用 model.find() 查询数据库 challengeType: 2 forumTopicId: 301543 --- # --description-- -使用 `Model.find() -> [Person]` 找出符合名字查询条件的所有人。 +我们尝试一种最简单的用法,`Model.find()` 接收一个查询 document(一个 JSON 对象)作为第一个参数,一个回调函数作为第二个参数,它会返回由匹配到的数据组成的数组。这个方法支持很多搜索选项,详情请参阅文档。 -`Model.find()` 接收一个查询 document(一个 JSON 对象)作为第一个参数,然后第二个参数是一个回调函数,它会返回由匹配到的数据组成的数组。这个方法支持很多搜索选项,详情请参阅文档。在这个挑战中,请使用 `personName` 作为搜索条件。 +# --instructions-- + +请使用 `Model.find() -> [Person]` 修改 `findPeopleByName` 函数,以查询全部拥有指定姓名的人。 + +请使用函数参数中的 `personName` 作为搜索条件。 # --hints-- @@ -37,5 +41,14 @@ forumTopicId: 301543 ); ``` +# --seed-- + # --solutions-- +```js +/** + 后端挑战不需要 solutions, + 因为他们需要使用一个完整的项目进行测试。 + 请查询我们的贡献指南以了解更多信息。 +*/ +``` 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.md b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md index b4f8307c67..687a91a781 100644 --- a/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md +++ b/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md @@ -1,17 +1,17 @@ --- id: 587d7fb7367417b2b2512c0c -title: 使用 model.findOne() 从数据库中返回一个匹配的 document +title: 使用 model.findOne() 从数据库中返回一个单一匹配的 Document challengeType: 2 forumTopicId: 301545 --- # --description-- -`Model.findOne()` 与 `Model.find()` 十分类似。但就算数据库中有很多条数据可以匹配查找条件,`Model.findOne()` 也只返回一个 document,而不会返回一个数组。如果查找的条件是一个值唯一的属性,`Model.findOne()` 会更加适用。 +`Model.findOne()` 与 `Model.find()` 十分类似。但就算数据库中有很多条数据可以匹配查询条件,`Model.findOne()` 也只返回一个 document,而不会返回一个数组。如果查询条件是声明为唯一值的属性,`Model.findOne()` 会更加适用。 # --instructions-- -用一个特定的 food 作为 `Model.findOne() -> Person` 的查找参数,来寻找喜欢食物列表中包含给定食物的某一个人。 +修改 `findOneByFood` 函数,用 `Model.findOne() -> Person` 来查询在收藏夹中有某种食物的一个人。将函数参数中的 `food` 作为检索条件。 # --hints-- @@ -39,5 +39,14 @@ forumTopicId: 301545 ); ``` +# --seed-- + # --solutions-- +```js +/** + 后端挑战不需要 solutions, + 因为他们需要使用一个完整的项目进行测试。 + 请查询我们的贡献指南以了解更多信息。 +*/ +``` diff --git a/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md b/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md index a3b914f7b9..8a72b10794 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md +++ b/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database.md @@ -7,7 +7,7 @@ forumTopicId: 301545 # --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. +`Model.findOne()` behaves like `Model.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. # --instructions--