Cheat Sheet: Common Methods and APIs
Practical mongosh reference for database, collection, CRUD, cursor, index, aggregation, and diagnostic methods.
db
Reference group: Database and collection discovery
What it does
The database object selected in the current mongosh session.
Syntax
db
Use case
Inspect the active database and call its collection methods.
db- The database object selected in the current mongosh session.
- Typical use: Inspect the active database and call its collection methods.
db.getName()
Reference group: Database and collection discovery
What it does
Returns the current database name.
Syntax
db.getName()
Use case
Check that a script targets the intended database.
db.getName()- Returns the current database name.
- Typical use: Check that a script targets the intended database.
show dbs
Reference group: Database and collection discovery
What it does
mongosh helper listing databases visible to the current user.
Syntax
show dbs
Use case
Discover available development databases; empty databases may not appear.
show dbs- mongosh helper listing databases visible to the current user.
- Typical use: Discover available development databases; empty databases may not appear.
show collections
Reference group: Database and collection discovery
What it does
mongosh helper listing collections in the current database.
Syntax
show collections
Use case
Inspect collection names while debugging.
show collections- mongosh helper listing collections in the current database.
- Typical use: Inspect collection names while debugging.
db.getCollectionNames()
Reference group: Database and collection discovery
What it does
Returns collection names as an array.
Syntax
db.getCollectionNames()
Use case
Inspect a database programmatically.
db.getCollectionNames()- Returns collection names as an array.
- Typical use: Inspect a database programmatically.
db.getCollection()
Reference group: Database and collection discovery
What it does
Returns a collection object by name, including names awkward to access with dot notation.
Syntax
db.getCollection(name)
Use case
Access dynamically named collections.
db.getCollection("user-events").findOne()- Returns a collection object by name, including names awkward to access with dot notation.
- Typical use: Access dynamically named collections.
db.createCollection()
Reference group: Database and collection discovery
What it does
Explicitly creates a collection with options such as validation or capped behavior.
Syntax
db.createCollection(name, options)
Use case
Create a collection with specific settings.
db.createCollection("events", { capped: true, size: 1048576 })- Explicitly creates a collection with options such as validation or capped behavior.
- Typical use: Create a collection with specific settings.
db.dropDatabase()
Reference group: Database and collection discovery
What it does
Drops the current database.
Syntax
db.dropDatabase()
Use case
Reset a disposable test database.
Important notes
Destructive: verify database selection and privileges first.
db.dropDatabase()- Drops the current database.
- Typical use: Reset a disposable test database.
insertOne()
Reference group: Collection CRUD methods
What it does
Inserts one document and returns its inserted identifier.
Syntax
db.collection.insertOne(document)
Use case
Create a record and retain insertedId.
db.products.insertOne({ sku: "KB-1", price: 1999 })insertedId.- Inserts one document and returns its inserted identifier.
- Typical use: Create a record and retain
insertedId.
insertMany()
Reference group: Collection CRUD methods
What it does
Inserts multiple documents. Ordered writes stop at the first error by default; unordered writes may continue.
Syntax
db.collection.insertMany(documents, { ordered })
Use case
Seed or import batches.
Important notes
Unordered operations can partially succeed; inspect write errors.
db.products.insertMany([{sku:"KB-2"},{sku:"KB-3"}], { ordered: false })- Inserts multiple documents. Ordered writes stop at the first error by default; unordered writes may continue.
- Typical use: Seed or import batches.