Docs Home / mongosh / Snippets

Use Snippets in the Console在控制台中使用代码段

Warning

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 is an overview of working with snippets in the mongosh console.此页面概述了如何在mongosh控制台中使用代码片段。

Install Snippet Packages安装代码段包

You must install a snippet package before you use it. Once a snippet package has been installed, it is loaded each time mongosh starts.在使用之前,您必须安装一个代码段包。一旦安装了代码段包,每次启动mongosh时都会加载它。

If you know the name of the snippet you want to install, enter:如果知道要安装的代码段的名称,请输入:

snippet install <name>

Otherwise, search the repository to get a list of available snippets.否则,请搜索存储库以获取可用代码段的列表。

snippet search

Once the snippet is installed, enter y to load it.安装代码段后,输入y加载它。

 Running install...
Installed new snippets analyze-schema. Do you want to load them now? [Y/n]: y
Finished installing snippets: analyze-schema

Note

If this is the first time you have used snippets you may see a warning like the following:如果这是您第一次使用代码段,您可能会看到如下警告:

This operation requires downloading a recent release of npm. Do
you want to proceed? [Y/n]:

You must install npm to use snippets.你必须安装npm才能使用代码片段。

Run a Snippet运行片段

Before running a new snippet, run snippet help <SNIPPET NAME> to learn more about the snippet's functionality.在运行新代码段之前,请运行代码段帮助snippet help <SNIPPET NAME>以了解有关代码段功能的更多信息。

For example, snippet help analyze-schema indicates that you can use the the analyze-schema by passing it a collection name.例如,snippet help analyze-schema指示您可以通过向analyze-schema传递集合名称来使用它。

testDB> snippet help analyze-schema
# analyze-schema

Analyze the schema of a collection or a cursor.

```js
> schema(db.coll);
┌─────────┬───────┬───────────┬────────────┐
│ (index) │ 0 │ 1 │ 2 │
├─────────┼───────┼───────────┼────────────┤
│ 0 │ '_id' │ '100.0 %' │ 'ObjectID' │
│ 1 │ 'a ' │ '50.0 %' │ 'Number' │
│ 2 │ 'a ' │ '50.0 %' │ 'String' │
└─────────┴───────┴───────────┴────────────┘

Once you know how to call a snippet, you can use it as in the following example.一旦你知道如何调用一个代码段,你就可以像下面的例子一样使用它。

Consider the reservations collection:考虑reservations(预订)集合:

db.reservations.insertMany( [
{"_id": 1001, "roomNum": 1, "reserved": true },
{"_id": 1002, "roomNum": 2, "reserved": true },
{"_id": 1003, "roomNum": 3, "reserved": "false" },
{"_id": 1004, "roomNum": 4, "reserved": true },
] )

To analyze the collection, install the analyze-schema snippet if it is not already present, pass in the collection name to run it.要分析集合,请安装analyze-schema代码段(如果尚未存在),传入集合名称以运行它。

snippet install analyze-schema
schema(db.reservations)

The document with "_id": 3 was misentered as a string. The analysis shows that the reserved field has string elements in addition to the expected booleans."_id": 3的文档被错误地输入为字符串。分析表明,reserved(保留)字段除了预期的布尔值外,还有字符串元素。

┌─────────┬────────────┬───────────┬───────────┐
│ (index) │ 0 │ 1 │ 2 │
├─────────┼────────────┼───────────┼───────────┤
│ 0 │ '_id ' │ '100.0 %' │ 'Number' │
│ 1 │ 'reserved' │ '75.0 %' │ 'Boolean' │
│ 2 │ 'reserved' │ '25.0 %' │ 'String' │
│ 3 │ 'roomNum ' │ '100.0 %' │ 'Number' │
└─────────┴────────────┴───────────┴───────────┘

Uninstall Snippets卸载代码段

Use the snippet uninstall command to remove a snippet. If you are unsure of the name, the snippet ls command lists all installed snippets.使用snippet uninstall命令删除代码段。如果您不确定名称,则snippet ls命令会列出所有已安装的代码段。

This code uninstalls the analyze-schema snippet.此代码卸载analyze-schema代码段。

snippet uninstall analyze-schema

Find Available Snippet Packages查找可用的代码段包

The snippet ls command returns a list of locally installed snippets along with some version and source information.snippet ls命令返回本地安装的代码段列表以及一些版本和源信息。

snippets@ /root/.mongodb/mongosh/snippets
├── mongosh:PRIVATE..DecryptCards@1.0.5
├── mongosh:analyze-schema@1.0.5
└── npm@7.23.0

To see the snippets that are available in the registry, first refresh the local metadata cache and then search.要查看注册表中可用的代码段,请先refresh(刷新)本地元数据缓存,然后进行search(搜索)。

snippet refresh
snippet search

snippet search lists available snippets, their version, and gives a brief description.列出可用的代码片段及其版本,并给出简要描述。

This instance has a second, private registry configured. Since the private registry was listed first, those snippets precede the MongoDB snippets in the list of available snippets.此实例配置了第二个私有注册表。由于私有注册表首先列出,因此这些代码段在可用代码段列表中位于MongoDB代码段之前。

┌─────────┬─────────────────────────────────┬─────────┬────────────────────────────────────────────────────────────────┐
│ (index) │ name │ version │ description │
├─────────┼─────────────────────────────────┼─────────┼────────────────────────────────────────────────────────────────┤
0'PRIVATE..DecryptCards''1.0.5''Decrypt credit card numbers'
1'PRIVATE..updateAuthentication''1.0.2''Update user pwds and authentication'
2'resumetoken''1.0.2''Resume token decoder script'
3'mongocompat''1.0.7''mongo compatibility script for mongosh'
4'spawn-mongod''1.0.1''Spin up a local mongod process'
5'mock-collection''1.0.2''mockCollection([{ a: 1 }, { a: 2 }]).find({ a: { $gt: 2 } })'
6'analyze-schema''1.0.5''schema(db.coll)'
└─────────┴─────────────────────────────────┴─────────┴────────────────────────────────────────────────────────────────┘

Get Repository Information获取存储库信息

Display the homepage and URL for each snippet repository:显示每个代码段存储库的主页和URL:

snippet info

The output lists each repository.输出列出了每个存储库。

Snippet repository URL:      https://compass.mongodb.com/mongosh/snippets-index.bson.br
--> Homepage: https://github.com/mongodb-labs/mongosh-snippets

Get Help for a Snippet获取片段帮助

Each snippet is unique and has its own interface. The best way to find information about how a particular snippet works is to view its README file by running snippet help:每个代码段都是唯一的,并且有自己的接口。查找特定代码段如何工作的信息的最佳方法是通过运行snippet help查看其README文件:

snippet help mongocompat

This command displays the README file for the mongocompat snippet in the mongosh console.此命令在mongosh控制台中显示mongocompat代码段的README文件。

# mongocompat

Provide `mongo` legacy shell compatibility APIs.

```js
> Array.sum([1, 2, 3])
6
> tojsononeline({a:1,b:2,c:3})
{ "a" : 1, "b" : 2, "c" : 3 }
```

When you create your own your own snippet packages, be sure to include a README.md file that provides useful help.当您创建自己的代码段包时,请确保包含一个提供有用帮助的README.md文件。