Docs HomeMongoDB Shell

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

Warning

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

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. 在使用snippet包之前,必须先安装它。Once a snippet package has been installed, it is loaded each time mongosh starts.一旦安装了snippet包,每次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. "_id": 3的文档被错误地作为字符串输入。The analysis shows that the reserved field has string elements in addition to the expected booleans.分析表明,除了预期的布尔值之外,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. 使用snippet uninstall载命令删除代码段。If you are unsure of the name, the snippet ls command lists all installed snippets.如果不确定名称,snippet ls命令会列出所有已安装的snippet。

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命令返回本地安装的snippet列表以及一些版本和源信息。

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.snippet search列出了可用的代码段及其版本,并给出了简短的描述。

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://github.com/YOUR_COMPANY/YOUR_REPO_PATH/index.bson.br
--> Homepage: https://davemungo.github.io/mongosh-snippets/

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代码段的自述文件。

# 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文件,该文件可以提供有用的帮助。