Skip to content
Latchkey

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.js

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →