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:本页讨论:
Preparing准备a snippet package.一个代码片段包。Publishing发布the snippet package to a registry.将代码段包添加到注册表。
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准备文件
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.如果你想创建一个私有仓库,你不必分叉社区仓库,但在完成以下步骤时,你应该手动重新创建一个类似的目录结构。
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.jsonCreate README.md.创建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.md。README.md描述了如何使用代码。当用户为代码段输入snippet help时,将显示此文件。
Create LICENSE.创建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许可证列表中选择一个许可证。
Create index.js.创建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此文件包含mongoshconsole.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该脚本可以调用其他文件和本地或远程npm模块。要require()a remote npm module use the construction:require()远程npm模块,请使用以下构造:const localRequire = require('module').createRequire(__filename);)For an example, see index.js in the例如,请参阅resumetokensnippet.resumethoken代码片段中的index.js。index.jsis 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 Filepackage.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:参数如下:
| "name" | |
| "snippetName" | install.install等命令的名称。 |
| "version" | |
| "description" | |
| "main" | 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" | |
| "publishConfig" | 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 MongoDB GitHub存储库中有几个package.json files in the MongoDB GitHub repository.package.json文件的示例。
Tip
MongoDB uses npm as a package registry.MongoDB使用npm作为包注册表。
npm relies on the npm依赖package.json file to manage packages. Refer to the npm package documentation for more information about package.json.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:要共享代码段,您必须将代码段包发布到注册表。该包将包含:
Your code代码README.mdLICENSEfile文件- package.json
When the files are complete, follow these steps to create and publish your snippet package.文件完成后,按照以下步骤创建并发布代码段包。
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, 此脚本的输出是一个brotli压缩的注册表索引文件index.bson.br.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是创建注册表索引的首选方法,但您也可以手动创建注册表索引。
Install the New Snippet Package安装新的代码段包
Follow these steps to install your new snippet package:按照以下步骤安装新的代码段包:
Refresh Metadata.刷新元数据。
Refresh the snippet metadata in your local 刷新本地mongosh.mongosh中的代码片段元数据。
snippet refreshContribute 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存储库,请执行以下操作:
Complete the Contributor Agreement.完成贡献者协议。
Read and complete the MongoDB Contributor Agreement.阅读并完成MongoDB贡献者协议。
Clone the Repository.克隆存储库。
Fork and clone the snippet project repository from GitHub.从GitHub分叉并克隆代码片段项目存储库。
Create Your Package Directory.创建包目录。
Add a new directory for your code under snippets/. Give it a descriptive name.在snippets/下为代码添加一个新目录。给它一个描述性的名字。
Create Your Package.创建包。
Create your snippet package. Be sure it contains the following files:创建代码片段包。确保它包含以下文件:
package.jsonindex.jsREADME.mdLICENSE
You do not have to create a registry index file. If your snippet package is accepted, MongoDB will update the registry index file.您不必创建注册表索引文件。如果代码段包被接受,MongoDB将更新注册表索引文件。
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.将其添加到代码段索引中。