Brent Keller

MongoDB Cheatsheet

This is a simple reference so I can remember commands and syntax as I infrequently use MongoDB.

Mongo commands

show dbs
use <db>
show tables
db.<collection>.find()

# Drop a MongoDB database
use <db name>
db.dropDatabase()

Manipulating collections

db.<collection>.update({'title':'MongoDB Overview'},
   {$set:{'title':'New MongoDB Tutorial'}},{multi:true})

db.<collection>.deleteMany({}) # deletes all docs in collection
db.<collection>.deleteOne()

db.<collection>.update(
   { }, # all records in the collection
   { $set: { userId: null } }, # set the userId field
   { multi: true } # allows update of more than one document
)

Query syntax

Query Selectors
Query Embedded Documents

# Filter by date: greater than
{ "date": { $gte: new ISODate("2020-05-02") } }

# Embedded document using dot notation query
{ "person.age": { 30 } }

# Querying an ObjectId requires using the function
{  "user.userId": ObjectId('6243c5656cc0146e52e75383') }
© 2023 Brent Keller