Database cheatsheet
MongoDB 8 Cheatsheet
mongosh shell commands for documents, queries, aggregation, indexes, backups and administration.
Connecting and navigating
- Check the shell version — Print the installed mongosh version.
- Connect to a local server — Open the shell against a database on the default port.
- Connect to Atlas — Connect with a connection string and a user, prompting for the password.
- List databases — Show the databases on the server.
- Switch database — Switch to a database. It is created when you first store data.
- List collections — Show the collections in the current database.
- Check the server version — Print the version of the server you are connected to.
Create, read, update, delete
- Insert a document — Add one document to a collection.
- Insert several documents — Add an array of documents in one call.
- Find all documents — Return documents from a collection. The shell shows the first 20.
- Find with a filter and projection — Find users older than 30 and return only their names.
- Find one document — Return the first document that matches.
- Sort and paginate — Return the 10 newest documents, skipping the first 20.
- Count matching documents — Count the documents that match a filter.
- Update a document — Set fields on the first matching document.
- Update many documents — Increase a counter on every matching document.
- Update or insert — Update a document, or insert it when none matches.
- Delete one document — Delete the first matching document.
- Delete many documents — Delete every document that matches the filter.
- List distinct values — Return the unique values of a field.
Query and update operators
- Match any of several values — Find documents whose field equals one of the listed values.
- Combine conditions with OR — Find documents that match either condition.
- Search with a regular expression — Find names that start with Ad.
- Check that a field exists — Find documents that have an email field.
- Match inside an array — Find orders with a line item that satisfies several conditions together.
- Add to an array — Append a value to an array field.
- Remove from an array — Remove matching values from an array field.
- Remove a field — Delete a field from a document.
Aggregation
- Group and total — Sum order amounts per status.
- Filter, sort and limit — Run $match early so the pipeline processes fewer documents.
- Join another collection — Add matching users to each order, like a left outer join.
- Flatten an array — Output one document per array element.
- Reshape documents — Keep some fields and compute new ones.
Indexes and performance
- Create an index — Create an ascending index on one field.
- Create a unique index — Reject documents with a duplicate value.
- Create a compound index — Index two fields together, in the order you filter and sort by them.
- Expire documents automatically — Delete documents an hour after the time in createdAt.
- List indexes — Show the indexes on a collection.
- See how a query runs — Show the plan, index use and documents examined.
- See running operations — List operations that are in progress.
Backup and administration
- Back up a database — Write a binary dump of one database to a folder.
- Restore a dump — Restore a dump into a server.
- Export a collection to JSON — Write a collection to a JSON file.
- Import JSON — Load a JSON file into a collection.
- Check database size — Show the data size, storage size and index size.
- Create a user — Create a user with read and write access to one database. Use a real password.
- Drop a collection — Delete a collection and its indexes.
- Drop the current database — Delete the current database.
Related cheatsheets
- DuckDB 2 cheatsheet — Query CSV, Parquet and DataFrames with SQL from Python or the duckdb command line, without running a server.
- Elasticsearch 9 cheatsheet — curl requests for indexes, documents, searches, bulk loads and cluster health. Security is on by default, so the examples authenticate.
- MySQL 8.4 cheatsheet — mysql client, backup and maintenance commands for MySQL 8.4.
- PostgreSQL 18 cheatsheet — psql, backup and maintenance commands for PostgreSQL 18.
- Redis 8 cheatsheet — redis-cli and core commands for keys, data structures and server administration.
- SQLite 3 cheatsheet — sqlite3 shell dot-commands, pragmas, import and export, and the SQL you use most.