46 Topics
Chapter•92 Minutes

Cheat Sheet: Common Methods and APIs

Practical mongosh reference for database, collection, CRUD, cursor, index, aggregation, and diagnostic methods.

db

Reference type:Cheat sheetFormat:Syntax + example + use case

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: practical example
db
💡 Explanation The database object selected in the current mongosh session. When to use it Inspect the active database and call its collection methods.
Key Takeaways
  • The database object selected in the current mongosh session.
  • Typical use: Inspect the active database and call its collection methods.

db.getName()

Reference type:Cheat sheetFormat:Syntax + example + use case

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(): practical example
db.getName()
💡 Explanation Returns the current database name. When to use it Check that a script targets the intended database.
Key Takeaways
  • Returns the current database name.
  • Typical use: Check that a script targets the intended database.

show dbs

Reference type:Cheat sheetFormat:Syntax + example + use case

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: practical example
show dbs
💡 Explanation mongosh helper listing databases visible to the current user. When to use it Discover available development databases; empty databases may not appear.
Key Takeaways
  • mongosh helper listing databases visible to the current user.
  • Typical use: Discover available development databases; empty databases may not appear.

show collections

Reference type:Cheat sheetFormat:Syntax + example + use case

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: practical example
show collections
💡 Explanation mongosh helper listing collections in the current database. When to use it Inspect collection names while debugging.
Key Takeaways
  • mongosh helper listing collections in the current database.
  • Typical use: Inspect collection names while debugging.

db.getCollectionNames()

Reference type:Cheat sheetFormat:Syntax + example + use case

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(): practical example
db.getCollectionNames()
💡 Explanation Returns collection names as an array. When to use it Inspect a database programmatically.
Key Takeaways
  • Returns collection names as an array.
  • Typical use: Inspect a database programmatically.

db.getCollection()

Reference type:Cheat sheetFormat:Syntax + example + use case

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(): practical example
db.getCollection("user-events").findOne()
💡 Explanation Returns a collection object by name, including names awkward to access with dot notation. When to use it Access dynamically named collections.
Key Takeaways
  • Returns a collection object by name, including names awkward to access with dot notation.
  • Typical use: Access dynamically named collections.

db.createCollection()

Reference type:Cheat sheetFormat:Syntax + example + use case

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(): practical example
db.createCollection("events", { capped: true, size: 1048576 })
💡 Explanation Explicitly creates a collection with options such as validation or capped behavior. When to use it Create a collection with specific settings.
Key Takeaways
  • Explicitly creates a collection with options such as validation or capped behavior.
  • Typical use: Create a collection with specific settings.

db.dropDatabase()

Reference type:Cheat sheetFormat:Syntax + example + use case

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(): practical example
db.dropDatabase()
💡 Explanation Drops the current database. When to use it Reset a disposable test database. Caution Destructive: verify database selection and privileges first.
Key Takeaways
  • Drops the current database.
  • Typical use: Reset a disposable test database.

insertOne()

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

insertOne(): practical example
db.products.insertOne({ sku: "KB-1", price: 1999 })
💡 Explanation Inserts one document and returns its inserted identifier. When to use it Create a record and retain insertedId.
Key Takeaways
  • Inserts one document and returns its inserted identifier.
  • Typical use: Create a record and retain insertedId.

insertMany()

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

insertMany(): practical example
db.products.insertMany([{sku:"KB-2"},{sku:"KB-3"}], { ordered: false })
💡 Explanation Inserts multiple documents. Ordered writes stop at the first error by default; unordered writes may continue. When to use it Seed or import batches. Caution Unordered operations can partially succeed; inspect write errors.
Key Takeaways
  • Inserts multiple documents. Ordered writes stop at the first error by default; unordered writes may continue.
  • Typical use: Seed or import batches.