mcpField
M
MongoDB Shell

MongoDB Shell

公式
github.comデータベース・データ402mongosh今日更新
リポジトリ公式サイト

MongoDB Shell について

MongoDB Shell(mongosh)は JavaScript に基づく MongoDB の公式対話型コマンドラインツールです。ローカルおよびリモートの MongoDB インスタンスへの接続、クエリ実行、データベース管理、管理操作に対応しています。

主な機能

  1. 01JavaScript で MongoDB クエリを作成し実行する
  2. 02MongoDB インスタンスに接続し、コレクションとドキュメントを閲覧する
  3. 03データベース管理とアグリゲーションパイプラインを実行する

活用シーン

  1. 01ターミナルから MongoDB データベースを対話的にクエリし管理する
  2. 02開発中にドキュメントデータとコレクションスキーマを素早く探索する
  3. 03MongoDB クエリとアグリゲーションパイプラインをテストする

インストール

OS またはパッケージマネージャーを選び、コマンドをターミナルで実行します。

$
brew install mongosh

インストール後にmongosh --versionを実行して確認します。

コマンド例

よく使うコマンドを使う順に並べています。

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

詳細

mongosh

Evergreen Waterfall CI

MongoDB Shell

このリポジトリは、すべての環境(REPL、ブラウザー、Compass など)における MongoDB Shell のさまざまなコンポーネントをまとめたモノレポです。

公式ドキュメントについては、MongoDB Docs ページをご覧ください。

MongoDB Shell は、4.0 以降の MongoDB サーバーで動作します。

MongoDB Shell の例

インストール

リリースの tarball はダウンロードページから入手できます。現在、MongoDB Shell は Windows(zip)、MacOS(zip)、Linux(tgz、deb、rpm)の3つのプラットフォームで提供しています。ダウンロード後、バイナリーを展開し、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

特定のバージョンに対してテストするには、サーバーバージョンを指定する semver 文字列を MONGOSH_SERVER_TEST_VERSION 環境変数に設定します。例: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 の configure フラグを、カンマ区切りのリストまたは JSON 配列で指定します。例:--shared-openssl,--shared-zlib
  • BOXEDNODE_MAKE_ARGS:Node.js の make 引数を、カンマ区切りのリストまたは JSON 配列で指定します(Windows では BOXEDNODE_CONFIGURE_ARGS と区別されません)。例:-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

コメント