mcpField
M
MongoDB Shell

MongoDB Shell

官方
github.com数据库与数据402mongosh今天更新
代码仓库访问官网

关于 MongoDB Shell

MongoDB Shell(mongosh)是 MongoDB 的官方交互式命令行工具,基于 JavaScript。支持连接到本地和远程 MongoDB 实例,执行查询、管理数据库和运行管理命令。

主要功能

  1. 01使用 JavaScript 编写和执行 MongoDB 查询
  2. 02连接到 MongoDB 实例,浏览集合和文档
  3. 03执行数据库管理操作和聚合管道

适用场景

  1. 01在终端中交互式查询和管理 MongoDB 数据库
  2. 02开发时快速探索文档数据和集合架构
  3. 03测试 MongoDB 查询和聚合管道

安装

选择你的系统或包管理器,复制命令到终端运行。

$
brew install mongosh

安装完成后运行mongosh --version确认安装成功。

命令示例

常用命令按使用顺序排列。

01
mongosh
02
mongosh mongodb://localhost:27017/app

详细介绍

mongosh

Evergreen 瀑布式 CI

MongoDB Shell

此仓库是一个单体仓库,包含 MongoDB Shell 在所有环境(REPL、浏览器、Compass 等)中的各种组件。

如需查看官方文档,请访问 MongoDB 文档页面。

MongoDB Shell 适用于版本不低于 4.0 的 MongoDB 服务端。

MongoDB Shell 示例

安装

你可以从下载页面获取发行版压缩包。目前,我们为三个不同的平台提供 MongoDB Shell:Windows(zip)、MacOS(zip)和 Linux(tgz、deb 和 rpm)。 下载后,你需要解压二进制文件,并将其添加到 PATH 变量中。有关各受支持平台的详细说明,请访问安装文档。

或者:

  • 运行 npx mongosh,无需完整安装即可运行 mongosh。如果你已安装 npm,这是最简单的方式。
  • 运行 download_latest.sh 下载 mongosh 二进制文件。可以使用以下脚本:
curl -fsSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh

CLI 用法

  $ mongosh [options] [db address] [file names (ending in .js or .mongodb)]

  Options:

    -h, --help                                 Show this usage information
    -f, --file [arg]                           Load the specified mongosh script
        --host [arg]                           Server to connect to
        --port [arg]                           Port to connect to
        --build-info                           Show build information
        --version                              Show version information
        --quiet                                Silence output from the shell during the connection process
        --shell                                Run the shell after executing files
        --nodb                                 Don't connect to mongod on startup - no 'db address' [arg] expected
        --norc                                 Will not run the '.mongoshrc.js' file on start up
        --eval [arg]                           Evaluate javascript
        --json[=canonical|relaxed]             Print result of --eval as Extended JSON, including errors
        --retryWrites[=true|false]             Automatically retry write operations upon transient network errors (Default: true)
        --deep-inspect[=true|false]           Force full depth inspection of server results (default: true if in interactive mode)

  Authentication Options:

    -u, --username [arg]                       Username for authentication
    -p, --password [arg]                       Password for authentication
        --authenticationDatabase [arg]         User source (defaults to dbname)
        --authenticationMechanism [arg]        Authentication mechanism
        --awsIamSessionToken [arg]             AWS IAM Temporary Session Token ID
        --gssapiServiceName [arg]              Service name to use when authenticating using GSSAPI/Kerberos
        --sspiHostnameCanonicalization [arg]   Specify the SSPI hostname canonicalization (none or forward, available on Windows)
        --sspiRealmOverride [arg]              Specify the SSPI server realm (available on Windows)

  TLS Options:

        --tls                                  Use TLS for all connections
        --tlsCertificateKeyFile [arg]          PEM certificate/key file for TLS
        --tlsCertificateKeyFilePassword [arg]  Password for key in PEM file for TLS
        --tlsCAFile [arg]                      Certificate Authority file for TLS
        --tlsAllowInvalidHostnames             Allow connections to servers with non-matching hostnames
        --tlsAllowInvalidCertificates          Allow connections to servers with invalid certificates
        --tlsCertificateSelector [arg]         TLS Certificate in system store (Windows and macOS only)
        --tlsCRLFile [arg]                     Specifies the .pem file that contains the Certificate Revocation List
        --tlsDisabledProtocols [arg]           Comma separated list of TLS protocols to disable [TLS1_0,TLS1_1,TLS1_2]
        --tlsFIPSMode                          Enable the system TLS library's FIPS mode

  API version options:

        --apiVersion [arg]                     Specifies the API version to connect with
        --apiStrict                            Use strict API version mode
        --apiDeprecationErrors                 Fail deprecated commands for the specified API version

  FLE Options:

        --awsAccessKeyId [arg]                 AWS Access Key for FLE Amazon KMS
        --awsSecretAccessKey [arg]             AWS Secret Key for FLE Amazon KMS
        --awsSessionToken [arg]                Optional AWS Session Token ID
        --keyVaultNamespace [arg]              database.collection to store encrypted FLE parameters
        --kmsURL [arg]                         Test parameter to override the URL of the KMS endpoint

  OIDC auth options:

        --oidcFlows[=auth-code,device-auth]    Supported OIDC auth flows
        --oidcRedirectUri[=url]                Local auth code flow redirect URL [http://localhost:27097/redirect]
        --oidcTrustedEndpoint                  Treat the cluster/database mongosh as a trusted endpoint
        --oidcIdTokenAsAccessToken             Use ID tokens in place of access tokens for auth
        --oidcDumpTokens[=mode]                Debug OIDC by printing tokens to mongosh's output [redacted|include-secrets]
        --oidcNoNonce                          Don't send a nonce argument in the OIDC auth request

  DB Address Examples:

        foo                                    Foo database on local machine
        192.168.0.5/foo                        Foo database on 192.168.0.5 machine
        192.168.0.5:9999/foo                   Foo database on 192.168.0.5 machine on port 9999
        mongodb://192.168.0.5:9999/foo         Connection string URI can also be used

  File Names:

        A list of files to run. Files must end in .js and will exit after unless --shell is specified.

  Examples:

        Start mongosh using 'ships' database on specified connection string:
        $ mongosh mongodb://192.168.0.5:9999/ships

  For more information on usage: https://mongodb.com/docs/mongodb-shell.

本地开发

要求
  • Node.js v24.x
安装
npm run bootstrap
npm run compile-cli
运行测试

运行所有测试(可能需要一些时间):

npm test

运行特定软件包中的测试:

npm -w @mongosh/cli-repl run test

若要针对特定版本进行测试,可以将 MONGOSH_SERVER_TEST_VERSION 环境变量设置为指定服务端版本的 semver 字符串,例如 MONGOSH_SERVER_TEST_VERSION='>= 7.0.0-rc0' npm -w @mongosh/cli-repl run test。

启动 CLI

通过 npm:

npm run start <... connection string, CLI args, etc.>

这会先编译 CLI 软件包的所有 TypeScript 依赖项。

如果确定其他所有内容都已编译,也可以在 cli-repl 软件包中运行 start:

npm run -w @mongosh/cli-repl start <... connection string, CLI args, etc.>
编译

编译所有 TypeScript:

npm run compile

仅编译 CLI 及其依赖项:

npm run compile-cli

编译独立可执行文件(可能需要一些时间):

npm run compile-exec

编译时相关的环境变量如下:

  • NODE_JS_VERSION:指定用于编译的 Node.js 版本,例如 16.15.0 或 16.x
  • BOXEDNODE_CONFIGURE_ARGS:Node.js 配置标志,采用逗号分隔列表 或 JSON 数组,例如 --shared-openssl,--shared-zlib
  • BOXEDNODE_MAKE_ARGS:Node.js make 参数(在 Windows 上,与 BOXEDNODE_CONFIGURE_ARGS 没有区别) 采用逗号分隔列表或 JSON 数组,例如 -j12

编译特定软件包,例如 Debian 的 .deb:

npm run compile-exec
npm run evergreen-release package -- --build-variant=deb-x64

编译和打包输出写入 dist/。

发布

请参阅 build 软件包文档。

贡献

如需报告问题,请在我们的 JIRA 项目中创建工单。

如需参与贡献,请参阅 CONTRIBUTING.md。

你希望在 MongoDB Shell 中看到哪些其他功能?欢迎在我们的反馈论坛提交建议。

许可证

Apache-2.0

评论

支持 Markdown