Install MongoDB and the mongosh Shell

Harry · 14 Sep 2026 · 4 views
Advertisement
Advertisement

Getting MongoDB

You have three easy options:

  • MongoDB Community Server installed locally.
  • Docker: docker run -d -p 27017:27017 --name mongo mongo:7 – running in seconds.
  • MongoDB Atlas, a free managed cluster in the cloud.

MongoDB listens on port 27017 by default.

Connect with mongosh

mongosh is the modern MongoDB shell – a full JavaScript environment connected to your database:

mongosh "mongodb://localhost:27017"

Databases and collections

Switch to (or lazily create) a database with use. Neither the database nor a collection truly exists until you insert data into it:

use shop

db.products.insertOne({ name: "Keyboard", price: 1999 })
show dbs            // now 'shop' appears
show collections    // 'products' appears

Here db refers to the current database and products is the collection – it was created automatically by the first insert.

Helpful shell commands

db.products.countDocuments()   // how many docs
db.products.find()             // list documents
db.products.drop()             // delete the collection
db.dropDatabase()              // delete the current database

Key points

  • Run MongoDB locally, via Docker, or on Atlas; the default port is 27017.
  • mongosh is a JavaScript shell connected to your database.
  • use dbname selects a database; it and its collections appear on first insert.
  • db.collection.xxx() is the pattern for all data operations.
Share this post:

Comments (0)

Please login or register to comment.