文档菜单
文档首页
/
MongoDB Shell
/

在控制台中使用代码片段

本页面

  • 安装代码片段包
  • 运行代码片段
  • 卸载代码片段
  • 查找可用的代码片段包
  • 获取存储库信息
  • 获取代码片段的帮助

警告

此功能为实验性功能。MongoDB 不提供代码片段的支持。此功能可能会在任何时候更改或删除,恕不另行通知。

预计不会出现错误,但如果您遇到任何问题,请在GitHub 存储库为该项目报告问题。

此页面是关于在mongosh控制台中处理代码片段的概述。

在使用之前,您必须安装一个片段包。一旦安装了片段包,每次启动 mongosh 时都会加载它。

如果您知道要安装的片段名称,请输入

snippet install <name>

否则,搜索存储库以获取可用片段的列表。

snippet search

安装片段后,输入 y 来加载它。

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

注意

如果您第一次使用片段,可能会看到如下警告

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

您必须安装 npm 才能使用片段。

在运行新片段之前,运行 snippet help <SNIPPET 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' │
└─────────┴───────┴───────────┴────────────┘

一旦您知道如何调用片段,您就可以像以下示例中使用它。

考虑 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 },
] )

要分析集合,如果尚未安装,请安装 analyze-schema 片段,然后传递集合名称来运行它。

snippet install analyze-schema
schema(db.reservations)

包含 "_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' │
└─────────┴────────────┴───────────┴───────────┘

使用 snippet uninstall 命令来删除片段。如果您不确定名称,可以使用 snippet ls 命令列出所有已安装的片段。

此代码将卸载 analyze-schema 片段。

snippet uninstall analyze-schema

snippet ls 命令返回本地安装的代码片段列表,以及一些版本和来源信息。

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

要查看注册表中可用的代码片段,首先刷新本地元数据缓存,然后搜索

snippet refresh
snippet search

snippet search 列出可用的代码片段、它们的版本,并给出简要描述。

此实例配置了第二个私有注册表。由于私有注册表列在前面,这些代码片段在可用代码片段列表中排在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)'
└─────────┴─────────────────────────────────┴─────────┴────────────────────────────────────────────────────────────────┘

显示每个代码片段仓库的主页和URL

snippet info

输出列表中列出每个仓库。

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

每个代码片段都是唯一的,并且拥有自己的接口。要了解特定代码片段的工作方式,最好的方法是运行 snippet help 来查看其 README 文件。

snippet help mongocompat

此命令将显示 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 }
```

当您 创建自己的代码片段包 时,请确保包含一个提供有用帮助的 README.md 文件。

返回

代码片段