mongosh Command Reference: Flags, Usage & CI Examples
mongosh runs JavaScript and commands against a MongoDB server.
mongosh is the modern MongoDB shell. It connects via a connection string and runs JavaScript interactively or with --eval. In CI it creates users, seeds data, and asserts state.
Common flags and usage
- "mongodb://..." : connection string with host, db, and options
- --host / --port: alternative to the URI host and port
- --eval "<js>": run JavaScript and exit
- -u / -p: username and password
- --authenticationDatabase: database holding the credentials
- --quiet: suppress the startup banner
Example
shell
until mongosh "${MONGODB_URI}" --quiet --eval 'db.adminCommand({ping:1})'; do
echo "waiting for mongo..."; sleep 1
done
mongosh "${MONGODB_URI}" seed.jsIn CI
Loop on db.adminCommand({ping:1}) to wait for the server before seeding. Users created in the admin database need ?authSource=admin in the URI; a missing authSource is the most common authentication failure.
Key takeaways
- mongosh runs JavaScript and admin commands against MongoDB.
- Wait for the server with an adminCommand ping loop.
- Admin-created users require ?authSource=admin in the URI.
Related guides
mongodump Command Reference: Flags, Usage & CI ExamplesReference for mongodump: --uri, --db, --collection, --gzip, --archive, and a CI example that exports a MongoD…
redis-cli Command Reference: Flags, Usage & CI ExamplesReference for redis-cli: -h, -p, -a, -n, ping, the URI form, and a CI healthcheck loop that waits for a Redis…
psql Command Reference: Flags, Usage & CI ExamplesReference for the psql PostgreSQL client: connection flags, -c and -f, PGPASSWORD, ON_ERROR_STOP, and a CI ex…