閱讀本篇前,請先安裝 MongoDB。
[MongoDB] Windows 安裝 MongoDB、Windows Install MongoDB
MongoDB 6.0 之前 Shell 是直接在裡面的,6.0 之後需要單獨下載。
2. 解壓縮 mongosh-2.3.4-win32-x64.zip
3. 在 Search 輸入 environment。
4. 按 Environment Variables。
![[MongoDB] Windows 安裝 MongoDB S [MongoDB] Windows 安裝 MongoDB S](https://pimg.1px.tw/mitblog/1733580257-2103928060-g.png)
5. 在 System variables 的 Path 項目按 Edit。
![[MongoDB] Windows 安裝 MongoDB S [MongoDB] Windows 安裝 MongoDB S](https://pimg.1px.tw/mitblog/1733580592-2822288663-g.png)
6. 按 New 一個欄位,複製 mongosh-2.3.4-win32-x64 解壓縮的路徑貼上,按 OK。
![[MongoDB] Windows 安裝 MongoDB S [MongoDB] Windows 安裝 MongoDB S](https://pimg.1px.tw/mitblog/1733580820-1166924515-g.png)
6. 開啟 PowerShell 或 cmd,輸入 mongosh 命令,出現版本代表成功。
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows Loading personal and system profiles took 521ms. PS C:\Users\mitblog> mongosh Current Mongosh Log ID: 6754597289f5f77ec6893bf7 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.3.4 Using MongoDB: 8.0.3 Using Mongosh: 2.3.4 For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/ To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy). You can opt-out by running the disableTelemetry() command. ------ The server generated these startup warnings when booting 2024-12-06T23:29:09.898+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted ------ test>
7. 退出 mongo,可以輸入 quit 命令。
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows Loading personal and system profiles took 521ms. ...略 Using MongoDB: 8.0.3 Using Mongosh: 2.3.4 ...略 test> quit PS C:\Users\mitblog>
8. 可以輸入 mongosh --helop 查看更多命令的使用方式。
PS C:\Users\mitblog> mongosh --help
$ 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)
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 [full|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://docs.mongodb.com/mongodb-shell.
