在控制台中使用代码片段
警告
此功能是实验性的。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
的文档被错误输入为 string。分析表明,reserved
字段除了具有预期的布尔值外,还具有 string 元素。
┌─────────┬────────────┬───────────┬───────────┐ │ (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:[email protected] ├── mongosh:[email protected] └── [email protected]
若要查看注册表中可用的代码片段,请先 refresh
本地元数据缓存,然后 search
。
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
文件。