Docs Home / VS Code Extension / Explore with Playgrounds / Use require()

Use require() to Load Local Files使用require()加载本地文件

You can use the require() function in your MongoDB Playgrounds to include code from local files. You can store your code in a single location and reuse that code in different playgrounds.您可以在MongoDB Playgrounds中使用require()函数来包含本地文件中的代码。您可以将代码存储在一个位置,并在不同的游乐场重用该代码。

About this Task关于此任务

This tutorial shows how to use require() to load local scripts. You can also use require() to load Node modules, like those downloaded from npm. 本教程展示了如何使用require()加载本地脚本。您还可以使用require()加载Node模块,就像从npm下载的模块一样。For more information, see Use require() to Include Node.js Modules.有关更多信息,请参阅使用require()包含Node.js模块

Steps步骤

1

Create a script file创建脚本文件

The following script file validates documents to ensure that the required fields are present. Save the script to your local filesystem as validate.js:以下脚本文件验证文档以确保所需字段存在。将脚本另存为validate.js到本地文件系统:

// validate.js

const required_fields = [ 'name', 'email' ]

const validate_data = (document) => {

let is_valid = true;

for (const field of required_fields) {
if (document[field] == null) {
is_valid = false;
}
};
return is_valid;
};

module.exports = validate_data;
2

Create a playground that uses the validation script创建一个使用验证脚本的游乐场

The following playground uses require() to call the validate_data function specified in validate.js. 以下游乐场使用require()调用validate.js中指定的validate_data函数。The validate_data function is called on two sample documents. If the document contains the required fields name and email, it is inserted into the people collection.在两个示例文档上调用validate_data函数。如果文档包含必填字段nameemail,则将其插入到people集合中。

Important

Update the first line of the playground with the path to the validate.js file:validate.js文件的路径更新游乐场的第一行:

// playground-1.mongodb.js

const validate = require('/path/to/validate.js');

use('mongodbVSCodePlaygroundDB');

const doc1 = { _id: 1, 'name': 'Taylor', 'email': 't123@gmail.com' };

const doc2 = { _id: 2, 'name': 'Taylor' };

const docs = [ doc1, doc2 ];

let inserted_count = 0;

for (const doc of docs) {
if (validate(doc)) {
db.getCollection('people').insertOne(doc);
inserted_count++;
}
};

console.log("Inserted " + inserted_count + " documents");
3

Run the playground跑操场

To run your Playground, press the Play Button at the top right of the Playground View. 要运行游乐场,请按游乐场视图右上角的播放按钮。VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. VS Code扩展拆分游乐场,并在“游乐场Results.json”窗格中输出游乐场的结果。If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。

Results结果

Only doc1 contains both required fields and is inserted into the collection. doc2 does not contain the required field email, and is not inserted.只有doc1包含这两个必填字段并插入到集合中。doc2不包含必填字段email,因此未插入。

To confirm that the correct document was inserted, query the people collection:要确认插入了正确的文档,请查询people集合:

use mongodbVSCodePlaygroundDB

db.people.find()

Output:输出:

[
{ _id: 1, name: 'Taylor', email: 't123@gmail.com' }
]

Learn More了解更多