Docs Home / mongosh / Snippets

Registries and Registry Configuration注册表和注册表配置

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存储库中为此项目打开一个问题。

This page discusses different registries and how to configure your system to use them.本页讨论不同的注册表以及如何配置系统以使用它们。

Types of Registry Configuration注册表配置类型

The snippets feature uses the npm package manager to install snippets from a pre-specified registry. You can configure your local mongosh to use one or more registries:片段功能使用npm包管理器从预先指定的注册表安装片段。您可以将本地mongosh配置为使用一个或多个注册表:

  • The community registry that is maintained by MongoDBMongoDB维护的社区注册表
  • A private registry that you maintain您维护的私有注册表
  • Multiple registries used together多个注册表一起使用

Using the MongoDB Registry使用MongoDB注册表

This is a public, community registry that is maintained by MongoDB.这是一个由MongoDB维护的公共社区注册表

The community registry is the default registry. It provides several useful snippets which can help you to get started. The snippets in the community registry are also good examples to use when you are ready to create your own snippets.社区注册表是默认注册表。它提供了几个有用的片段,可以帮助您入门。当您准备创建自己的代码段时,社区注册表中的代码段也是很好的示例

MongoDB users are encouraged to contribute to this public registry. To learn how to share your code with other MongoDB users, see Contribute a Snippet Package to the MongoDB Community.鼓励MongoDB用户为此公共注册表做出贡献。要了解如何与其他MongoDB用户共享代码,请参阅向MongoDB社区贡献代码段包

Using Private Snippet Registries使用私有代码段注册表

You can share code internally using a private registry.您可以使用私有注册表在内部共享代码。

If your snippets reveal proprietary or sensitive information, you can store them in a private, local registry instead of the public registry.如果你的代码片段泄露了专有或敏感信息,你可以将它们存储在私有的本地注册表中,而不是公共注册表中。

To create a private registry, see Define a New Registry.要创建私有注册表,请参阅定义新注册表

Using Multiple Registries使用多个注册表

A private registry can also be used in conjunction with the community registry and other private registries. Using multiple registries allows you to benefit from snippets maintained by MongoDB or third parties while also maintaining control over code you don't want to share externally.私有注册表也可以与社区注册表和其他私有注册表结合使用。使用多个注册表可以让你从MongoDB或第三方维护的代码片段中受益,同时还可以控制你不想在外部共享的代码。

To configure multiple registries, see Connecting to Registries.要配置多个注册表,请参阅连接到注册表

How to Configure a Registry如何配置注册表

To use a private registry or multiple registries:要使用私有注册表或多个注册表,请执行以下操作:

Define a New Registry定义新注册表

The npm public registry hosts the MongoDB snippets community registry. You can use npm to host your own public or private registry as well.npm公共注册表托管MongoDB代码片段社区注册表。你也可以使用npm来托管你自己的公共或私有注册表。

1

Create A GitHub Repository.创建GitHub存储库。

You will push snippet packages from your GitHub repository to your npm registry.您将把代码段包从GitHub存储库推送到npm注册表。

Follow the GitHub documentation to create a repository.按照GitHub文档创建存储库。

2

Create An npm Registry.创建一个npm注册表。

Follow the npm registry documentation to create a registry.按照npm注册表文档创建注册表。

3

Update snippetIndexSourceURLs.更新snippetIndexSourceURLs

To make the new registry available to your local mongosh installation, update the snippetIndexSourceURLs configuration settings.要使新注册表可用于本地mongosh安装,请更新snippetIndexSourceURLs配置设置。

config.set('snippetIndexSourceURLs',
'https://github.com/YOUR_COMPANY/PATH_TO_YOUR_REPOSITORY/index.bson.br;'
+ config.get('snippetIndexSourceURLs') )
4

Update snippetRegistryURL.更新snippetRegistryURL

If you created a registry that is hosted outside npm, update snippetRegistryURL to point to the new registry.如果你创建了一个托管在npm之外的注册表,请更新snippetRegistryURL以指向新的注册表。

Connecting to Registries连接到注册表

You can use a private registry in addition to, or instead of, the community MongoDB registry.除了社区MongoDB注册表之外,您还可以使用私有注册表,或者使用私有注册表代替社区MongoDB注册表。

snippetIndexSourceURLs ia a list of URLs. Each URL defines a path to an index file that contains metadata for the snippets in that registry.snippetIndexSourceURLs是一个URL列表。每个URL都定义了一个指向索引文件的路径,该文件包含该注册表中代码段的元数据。

Configure an additional registry by adding a URL to snippetIndexSourceURLs.通过添加一个URL来snippetIndexSourceURLs,以配置其他注册表。

config.set('snippetIndexSourceURLs',
'https://github.com/YOUR_COMPANY/PATH_TO_YOUR_REPOSITORY/index.bson.br;'
+ config.get('snippetIndexSourceURLs')
)

Restart mongosh for the update to take effect.重新启动mongosh以使更新生效。

Important

If two snippets with the same name appear in multiple registries, local system updates will be based on the entry in the first registry in the snippetIndexSourceURLs list.如果多个注册表中出现两个同名代码段,则本地系统更新将基于snippetIndexSourceURLs列表中第一个注册表中的条目。

Do not reuse snippet names to avoid potential conflicts.不要重复使用代码段名称以避免潜在冲突。