Linux command
mongosh 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Connect to local database
mongosh
Connect to specific database
mongosh [mongodb://host:27017/dbname]
Connect with authentication
mongosh -u [user] -p [password] [dbname]
Execute script file
mongosh [dbname] [script.js]
Evaluate expression
mongosh --eval "[db.collection.find()]"
Start shell without
mongosh --nodb
Connect with TLS
mongosh --tls "mongodb://[host]:27017/[dbname]"
Quiet mode
mongosh --quiet [script.js]
说明
mongosh is the modern MongoDB shell, providing a fully functional JavaScript and Node.js REPL environment for interacting with MongoDB deployments. It replaces the legacy mongo shell with improvements including syntax highlighting, intelligent autocomplete, inline help, and extensible snippets. It supports all CRUD operations, aggregation pipelines, database administration, and Atlas connectivity.
参数
- -u _USER_
- Username for authentication.
- -p _PASSWORD_
- Password for authentication.
- --eval _EXPR_
- Evaluate JavaScript expression.
- --nodb
- Start without database connection.
- --quiet
- Silence non-essential output.
- --host _HOST_
- Server hostname (default: localhost).
- --port _PORT_
- Server port (default: 27017).
- --authenticationDatabase _DB_
- Authentication database (default: admin).
- --tls
- Enable TLS/SSL connection.
- --shell
- Force interactive shell after running files/eval.
- --help
- Display help information.
FAQ
What is the mongosh command used for?
mongosh is the modern MongoDB shell, providing a fully functional JavaScript and Node.js REPL environment for interacting with MongoDB deployments. It replaces the legacy mongo shell with improvements including syntax highlighting, intelligent autocomplete, inline help, and extensible snippets. It supports all CRUD operations, aggregation pipelines, database administration, and Atlas connectivity.
How do I run a basic mongosh example?
Run `mongosh` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -u _USER_ do in mongosh?
Username for authentication.