Linux command
mongo 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Connect to local database
mongo
Connect to remote database
mongo [mongodb://host:27017/dbname]
Connect with authentication
mongo -u [user] -p [password] [dbname]
Execute JavaScript file
mongo [dbname] [script.js]
Evaluate expression
mongo --eval "[db.collection.find()]"
Connect with SSL
mongo --ssl --host [host]
说明
mongo is the original interactive shell shipped with the MongoDB server up through release 4.4. It is a JavaScript REPL that connects to a mongod or mongos process via the MongoDB wire protocol and exposes the database as a tree of JavaScript objects: db, db.collection, and helper methods like find(), insertOne(), and aggregate(). Connections accept a MongoDB URI (mongodb:// or mongodb+srv://) or individual flags such as --host, --port, -u / -p / --authenticationDatabase for authentication, and --tls / --ssl for encrypted transports. A trailing JavaScript file plus --eval lets the shell be used in scripts, returning the result of the last expression on stdout. Beyond the shell, mongo is widely used for administrative tasks: creating users, configuring replica sets, sharding, and inspecting server status with db.serverStatus().
参数
- -u _USER_
- Username for authentication.
- -p _PASSWORD_
- Password for authentication.
- --eval _EXPR_
- Evaluate JavaScript expression.
- --ssl
- Use SSL connection.
- --host _HOST_
- Server hostname.
- --help
- Display help information.
FAQ
What is the mongo command used for?
mongo is the original interactive shell shipped with the MongoDB server up through release 4.4. It is a JavaScript REPL that connects to a mongod or mongos process via the MongoDB wire protocol and exposes the database as a tree of JavaScript objects: db, db.collection, and helper methods like find(), insertOne(), and aggregate(). Connections accept a MongoDB URI (mongodb:// or mongodb+srv://) or individual flags such as --host, --port, -u / -p / --authenticationDatabase for authentication, and --tls / --ssl for encrypted transports. A trailing JavaScript file plus --eval lets the shell be used in scripts, returning the result of the last expression on stdout. Beyond the shell, mongo is widely used for administrative tasks: creating users, configuring replica sets, sharding, and inspecting server status with db.serverStatus().
How do I run a basic mongo example?
Run `mongo` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -u _USER_ do in mongo?
Username for authentication.