Basic MongoDB Operations

Queries and Indexes

Display query stats

db.<collection>.find({<query conditions>}).explain('executionStats')

Basic Document Operations

Find all documents in a collection

db.<collection>.find({})

Sorting documents

db.<collection>.find({}).sort()

Find one document in a collection

db.<collection>.findOne({})

Count documents in a collection

db.<collection>.count()

Insert a document

db.<collection>.insert({fieldA: 'a', fieldB: 'b'})

Updating a document

db.<collection>.update({_id: '<document id>'}, {'$set': {'fieldA': 'value'}})

Updating multiple documents

db.<collection>.update({<field condition>: '<condition>'}, {'$set': {'fieldA': 'value'}}, {multi: true})

Delete a document

db.<collection>.remove({_id: '<document id>'})

Collection Operations

Remove a collection

db.<collection>.drop()

List all collections

show collections

Database Operations

List all databases

show databases

Switch to a database

use <database>