Docs HomeMongoDB Shell

Create and Share Snippets创建和共享代码段

Warning

Experimental feature实验特性

This feature is experimental. MongoDB does not provide support for Snippets. 此功能是实验性的。MongoDB不支持Snippets。This feature may be changed or removed at any time without prior notice.此功能可能随时更改或删除,恕不另行通知。

Bugs are not expected, however should you encounter one, please open an issue in the GitHub repository for this project.预计不会出现错误,但如果您遇到错误,请在GitHub存储库中打开此项目的问题。

You can write scripts to manipulate data or carry out administrative tasks in mongosh. 您可以编写脚本来操作mongosh中的数据或执行管理任务。Packaging a script as a snippet provides a way to easily share scripts within your organization or across the MongoDB user community.将脚本打包为片段提供了一种在组织内或MongoDB用户社区中轻松共享脚本的方法。

This page discusses:本页讨论:

For examples of scripts and the metadata files in snippet packages, see the snippets in the community snippet registry on GitHub.有关代码片段包中的脚本和元数据文件的示例,请参阅GitHub上社区代码片段注册表中的代码片段。

Tip

If you plan to submit your snippet to the community registry, be sure to review the information in Contribute a Snippet Package to the MongoDB Community.如果您计划将代码段提交到社区注册表,请务必查看向MongoDB社区贡献代码段包中的信息。

Create a Snippet Package创建代码段包

The steps in this section focus on packaging a script. For more details on writing scripts see Write Scripts.本节中的步骤侧重于打包脚本。有关编写脚本的更多详细信息,请参阅编写脚本

Prepare the Files准备文件

1

Fork the Community Repository.分叉社区存储库。

If you plan to contribute to the community repository, fork the snippets project repository.如果您计划为社区存储库做出贡献,请派生snippets项目存储库

You do not have to fork the community repository if you want to create a private repo, but you should manually recreate a similar directory structure as you work through the following steps.如果要创建私有repo,则不必派生社区存储库,但在执行以下步骤时,应手动重新创建类似的目录结构。

2

Create a Package Directory.创建包目录。

Create a directory for your snippet package under the snippets directory in the forked repository. 在分叉存储库中的snippets目录下为snippet包创建一个目录。This directory will contain the code for your script and several metadata files.该目录将包含脚本的代码和几个元数据文件。

This example shows directories for two snippet packages, decrypt-cards and update-auth. 此示例显示了两个snippet包(decrypt-cardsupdate-auth)的目录。The contents of the community snippets directories are omitted for clarity.为了清楚起见,省略了社区snippets目录的内容。

mongo-snippets
|
├── scripts
│   ├── make-index.js
│   └── show-index.js
└── snippets
├── analyze-schema
├── decrypt-cards
│   ├── LICENSE-Community.txt
│   ├── README.md
│   ├── error-matchers.js
│   ├── index.js
│   └── package.json
├── mock-collection
├── mongocompat
├── resumetoken
├── spawn-mongod
└── update-auth
├── LICENSE
├── README.md
├── index.js
└── package.json
3

Create README.md.创建README.md

Create a README.md. 创建README.mdThe README.md describes how to use your code. README.md描述了如何使用代码。This file is displayed when a user enters snippet help for your snippet.当用户输入代码段的snippet help时,会显示此文件。

4

Create LICENSE.创建LICENSE

Create a LICENSE file. 创建一个LICENSE文件。You will need to enter a license identifier string later, so try to chose a license from the SPDX license list.稍后需要输入许可证标识符字符串,因此请尝试从SPDX许可证列表中选择一个许可证。

5

Create index.js.创建index.js

Create an index.js file.创建一个index.js文件。

  • This file contains the entry point to your code that is exposed in the mongosh console.该文件包含在mongosh控制台中公开的代码的入口点。
  • The script is written in JavaScript and defines your new functions.该脚本是用JavaScript编写的,并定义了您的新函数。
  • The script can be in a single file or multiple files.脚本可以在单个文件中,也可以在多个文件中。
  • The script can call other files and local or remote npm modules. 该脚本可以调用其他文件以及本地或远程npm模块。To require() a remote npm module use the construction:require()远程npm模块,请使用以下构造:

    const localRequire = require('module').createRequire(__filename);)

    For an example, see index.js in the resumetoken snippet.有关示例,请参阅resumetoken片段中的index.js

  • index.js is referenced in package.json.package.json中引用了index.js
  • The MongoDB repository has example code.MongoDB存储库有示例代码
Tip

If you have an existing script, either rename it index.js or create an index.js file to load it. 如果您有一个现有的脚本,请将其重命名为index.js或创建一个index.js文件来加载它。For an example of an index.js file that loads other scripts, see this one in the community repository.有关加载其他脚本的index.js文件的示例,请参阅社区存储库中的这一个

Prepare the package.json File准备package.json文件

package.json contains metadata that the package registry uses to manage snippets.包含包注册表用于管理代码段的元数据。

A minimal package.json file looks like this:一个最小的package.json文件如下所示:

{
"name": "@mongosh/snippet-resumetoken",
"snippetName": "resumetoken",
"version": "1.0.2",
"description": "Resume token decoder script",
"main": "index.js",
"license": "Apache-2.0",
"publishConfig": {
"access": "public"
}
}

The parameters are:参数为:

FieldDescription
"name"The npm package that contains the snippet.包含代码段的npm包。
"snippetName"The snippet name. 代码段名称。This is the name used with commands like install.这是与install等命令一起使用的名称。
"version"The package version. 程序包版本。This should be incremented when you update your snippet.更新代码段时,此值应递增。
"description"A brief note about what your snippet does. 关于您的代码片段的作用的简短说明。Caution, if the description is more than 50 or 60 characters long it may cause display problems with some snippet commands.注意,如果描述长度超过50或60个字符,则可能会导致某些代码段命令的显示问题。
"main"This is the starting point for your code, index.js. Note that functions in other files can be scoped so that they are also available in the the mongosh shell.这是您的代码index.js的起点。请注意,其他文件中的函数可以设置作用域,以便它们也可以在mongosh shell中使用。
"license"The license for users of your code. 代码用户的许可证。If you want to contribute to the shared registry, the license should be from the SPDX license list. 如果您想为共享注册表做出贡献,则许可证应该来自SPDX许可证列表See also the MongoDB Contributor Agreement.另请参阅MongoDB贡献者协议
"publishConfig"This value is used to control access to your snippet package. 此值用于控制对代码段包的访问。public is typical, but npm provides other options as well.public是典型的,但npm也提供了其他选项

Use this code to create a skeleton package.json file. 使用此代码创建一个骨架package.json文件。Edit the file and replace each UPDATE to insert the values for your snippet package.编辑文件并替换每个UPDATE以插入代码段包的值。

{
"name": "@UPDATE/UPDATE",
"snippetName": "UPDATE",
"version": "UPDATE",
"description": "UPDATE",
"main": "UPDATE",
"license": "UPDATE",
"publishConfig": {
"access": "UPDATE"
}
}

There are several examples of package.json files in the MongoDB GitHub repository.MongoDB GitHub存储库中有几个package.json文件的例子。

Tip

MongoDB uses npm as a package registry.MongoDB使用npm作为包注册表。

npm relies on the package.json file to manage packages. npm依赖于package.json文件来管理包。Refer to the npm package documentation for more information about package.json.有关package.json的更多信息,请参阅npm包文档

Publish a Snippet发布代码段

To share your snippet, you must publish you snippet package to a registry. 若要共享代码片段,必须将代码片段包发布到注册表。The package will contain:该包将包含:

When the files are complete, follow these steps to create and publish your snippet package.文件完成后,请按照以下步骤创建并发布代码段包。

1

Create a registry index file.创建注册表索引文件。

The registry index file is not the same as the index.js file that contains your snippet code. 注册表索引文件与包含代码段的index.js文件不同。The registry index file, index.bson.br, contains metadata for the snippet packages in your registry.注册表索引文件index.bson.br包含注册表中代码段包的元数据。

The registry index file must be compressed before it is uploaded for use. 在上载注册表索引文件以供使用之前,必须对其进行压缩。The make-index.js utility in the scripts directory walks through your snippet source directories gathering the information that is needed to create the registry index file. script目录中的make-index.js实用程序遍历代码段源目录,集合创建注册表索引文件所需的信息。After it creates the registry index file, make-index.js script also compresses it.创建注册表索引文件后,make-index.js脚本也会对其进行压缩。

Run make-index.js from the mongo-snippets directory create the index.mongo-snippets目录运行make-index.js创建索引。

node ./scripts/make-index.js

The output of this script is a brotli-compressed registry index file, index.bson.br.这个脚本的输出是一个brotli-compressed注册表索引文件index.bson.br

You can use show-index.js to view the compressed registry index file.您可以使用show-index.js来查看压缩的注册表索引文件。

Using make-index.js is the preferred way to create a registry index, but you can also create a registry index manually.使用make-index.js是创建注册表索引的首选方式,但您也可以手动创建注册表索引

2

Commit Your Snippet提交您的代码段

Commit your snippet and the registry index file to your GitHub repository.将您的代码段和注册表索引文件提交到GitHub存储库。

3

Publish Changes发布更改

Publish your changes to your npm registry.将更改发布到npm注册表。

npm publish --access public

Install the New Snippet Package安装新的代码段包

Follow these steps to install your new snippet package:按照以下步骤安装新的snippet包:

1

Refresh Metadata.刷新元数据。

Refresh the snippet metadata in your local mongosh.刷新本地mongosh中的代码段元数据。

snippet refresh
2

Install the Snippet.安装代码段。

Install the snippet.安装代码段。

snippet install YOUR_NEW_SNIPPET

Contribute a Snippet Package to the MongoDB Community为MongoDB社区贡献一个Snippet包

If you have written a code snippet that might be useful for other MongoDB users, you are invited to contribute it to the community repository hosted on GitHub.如果您编写了一个可能对其他MongoDB用户有用的代码片段,我们将邀请您将其贡献给GitHub上托管的社区存储库

To submit a snippet to the shared MongoDB repository:要将代码段提交到共享的MongoDB存储库,请执行以下操作:

1

Complete the Contributor Agreement.完成出资人协议。

Read and complete the MongoDB Contributor Agreement.阅读并完成MongoDB贡献者协议

2

Clone the Repository.克隆存储库。

Fork and clone the snippet project repository from GitHub.从GitHub派生并克隆snippet项目存储库

3

Create Your Package Directory.创建您的程序包目录。

Add a new directory for your code under snippets/. snippets/下为代码添加一个新目录。Give it a descriptive name.给它一个描述性的名字。

4

Create Your Package.创建您的程序包。

Create your snippet package. 创建代码段包。Be sure it contains the following files:请确保它包含以下文件:

  • package.json
  • index.js
  • README.md
  • LICENSE

You do not have to create a registry index file. 您不必创建注册表索引文件。If your snippet package is accepted, MongoDB will update the registry index file.如果您的snippet包被接受,MongoDB将更新注册表索引文件。

5

Commit your changes.提交您的更改。

Commit your changes to your GitHub repository.将更改提交到GitHub存储库。

6

Submit Your Snippet Code.提交您的代码段。

Open a pull request against the snippet project repository.打开针对代码段项目存储库的拉请求。

MongoDB will review your pull request. If it is accepted, we will:MongoDB将审查您的拉取请求。如果被接受,我们将:

  • Merge your code into our GitHub repository.将您的代码合并到GitHub存储库中。
  • Publish it to the npm registry.将其发布到npm注册表。
  • Add it to the snippet index.将其添加到代码段索引中。