Installing MongoDB and Basic Operations
Install mongodb-org from the official repo on Ubuntu/Debian, run CRUD in mongosh, and turn on authentication so your database isn't exposed on the open internet.
MongoDB is a document-oriented database: data lives as JSON-like documents, which suits flexible schemas and fast-moving projects. This guide focuses on Ubuntu/Debian and walks you through installing, connecting, and running basic operations on your own server or VPS — with the security step done properly.
Install mongodb-org from the official repo
The mongodb package shipped by most distributions is usually outdated. Install the mongodb-org meta-package from MongoDB's official APT repository instead. The commands below target MongoDB 8.0 on Ubuntu 22.04 (jammy); adjust the URLs to match your release:
# Import the official GPG key
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-8.0.gpg
# Add the repository (Debian users: swap "ubuntu jammy" for your debian codename)
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
sudo apt update
sudo apt install -y mongodb-org
Start mongod
mongod is the database daemon. Letting systemd manage it is the least-effort path:
sudo systemctl enable --now mongod # start now and on every boot
sudo systemctl status mongod # check it's running
By default it listens only on 127.0.0.1:27017 — local connections only. That's a safe default, so leave it alone for now.
Connect with mongosh
mongosh is the modern shell (the old mongo client is deprecated). Just run it to connect to the local instance:
mongosh
Databases and collections
- Database — a container for collections. Switch with use; it springs into existence on the first write.
- Collection — a group of documents, roughly a "table" in relational terms, but with no schema to define upfront.
- Document — a single JSON/BSON record, each carrying its own id primary key.
Basic CRUD
Inside mongosh, work against the users collection in a shop database:
use shop
// Create
db.users.insertOne({ name: "Alice", age: 30, tags: ["vip"] })
// Read
db.users.find({ age: { $gt: 18 } })
db.users.findOne({ name: "Alice" })
// Update ($set touches only the listed fields)
db.users.updateOne({ name: "Alice" }, { $set: { age: 31 } })
// Delete
db.users.deleteOne({ name: "Alice" })
Turn on authentication — don't leave it exposed
MongoDB ships with authentication off. If you open bindIp to the internet without setting a password, expect your database to be scanned, copied, and held for ransom within hours. Always create an admin user before enabling auth.
While auth is still off, create an administrator:
use admin
db.createUser({
user: "admin",
pwd: passwordPrompt(), // prompts interactively, keeping the password out of your shell history
roles: [ { role: "root", db: "admin" } ]
})
Then edit /etc/mongod.conf to require authentication:
security:
authorization: enabled
Restart and reconnect with credentials:
sudo systemctl restart mongod
mongosh -u admin -p --authenticationDatabase admin
A short security checklist: keep bindIp: 127.0.0.1; reach it remotely over an SSH tunnel or a private network; if you truly must expose it, put it behind a firewall allowlist plus TLS; and give each application its own least-privilege account instead of sharing root.
Summary
Install mongodb-org from the official repo for a current release, start mongod with systemctl, connect via mongosh, and organize data with the database/collection/document model. insertOne, find, updateOne, and deleteOne cover the basics. The step that matters most is authentication: create a root user, set authorization: enabled, and keep mongod bound to localhost. Do that and you'll have a MongoDB that works — and one that isn't sitting wide open.