Docs Home / mongosh / Snippets

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

Warning

Experimental feature实验特性

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

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. Packaging a script as a snippet provides a way to easily share scripts within your organization or across the MongoDB user community.您可以编写脚本来操纵数据或在mongosh中执行管理任务。将脚本打包为代码段提供了一种在组织内或整个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.如果您计划为社区存储库做出贡献,请分叉代码片段项目存储库

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.如果你想创建一个私有仓库,你不必分叉社区仓库,但在完成以下步骤时,你应该手动重新创建一个类似的目录结构。

2

Create a Package Directory.创建包目录。

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

This example shows directories for two snippet packages, decrypt-cards and update-auth. The contents of the community snippets directories are omitted for clarity.

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. The README.md describes how to use your code. This file is displayed when a user enters snippet help for your snippet.创建一个README.mdREADME.md描述了如何使用代码。当用户为代码段输入snippet help时,将显示此文件。

4

Create LICENSE.创建LICENSE

Create a LICENSE file. You will need to enter a license identifier string later, so try to chose a license from the SPDX license list.创建LICENSE文件。稍后您需要输入许可证标识符字符串,因此请尝试从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. To require() a remote npm module use the construction:该脚本可以调用其他文件和本地或远程npm模块。要require()远程npm模块,请使用以下构造:

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

    For an example, see index.js in the resumetoken snippet.例如,请参阅resumethoken代码片段中的index.js

  • index.js is referenced in package.json.package.json中引用。
  • 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:参数如下:

Field字段Description描述
"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. Edit the file and replace each UPDATE to insert the values for your snippet package.使用此代码创建骨架package.json文件。编辑文件并替换每个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. Refer to the npm package documentation for more information about package.json.npm依赖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. The registry index file, index.bson.br, contains metadata for the snippet packages in your registry.注册表索引文件与包含代码片段的index.js文件不同。注册表索引文件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. After it creates the registry index file, make-index.js script also compresses it.scripts目录中的make-index.js实用程序遍历代码段源目录,集合创建注册表索引文件所需的信息。创建注册表索引文件后,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压缩的注册表索引文件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:按照以下步骤安装新的代码段包:

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分叉并克隆代码片段项目存储库

3

Create Your Package Directory.创建包目录。

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

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.您不必创建注册表索引文件。如果代码段包被接受,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将审查pull请求。如果被接受,我们将:

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