Skip to content

Instantly share code, notes, and snippets.

@estorgio
Last active August 22, 2025 15:55
Show Gist options
  • Save estorgio/0277e82720beea2acd32851d5226d24a to your computer and use it in GitHub Desktop.
Save estorgio/0277e82720beea2acd32851d5226d24a to your computer and use it in GitHub Desktop.
MongoDB Shell Basics
// List all databases
show dbs
// Switch to a database
use dbname
// List all collections in the current database
show collections
// Drop current database
db.dropDatabase()
// Create collection
db.createCollection("students")
// Create collection (with advanced options)
db.createCollection("teachers", {
capped: true, // set capped mode to true
size: 1000000, // limit data size to 1000000 bytes
max:100 // limit number of documents to 100
}, {
autoIndexId: false // disable indexes for _id (not recommended)
})
// Drop collection
db.students.drop()
// Get all documents in a collection
db.students.find()
// Insert a single document in a collection
db.students.insertOne({name: "Spongebob", age: 30, gpa: 3.2})
// Insert multiple documents at once in a collection
db.students.insertMany([
{name: "Patrick", age: 38, gpa: 1.5},
{name: "Sandy", age: 27, gpa: 4.0},
{name: "Gary", age: 18, gpa: 2.5}
])
// Insert document with variety of data types
db.students.insertOne({
... name: "Larry", // string
... age: 32, // integer
... gpa: 2.8, // double
... fullTime: false, // boolean
... registeredDate: new Date("2025-08-22T11:25:32"), // date
... graduationDate: null, // null
... courses: ["Biology", "Chemistry", "Calculus"], // array
... address: { // nested document
... street: "123 Fake St.",
... city: "Bikini Bottom",
... zip: 12345
... }
... })
// Sort documents by a particular field ("name" in this case) in ascending order (the value "1")
db.students.find().sort({name: 1})
// Sort documents by a particular field ("name" in this case) in descending order (the value "-1")
db.students.find().sort({name: -1}
// Limit the numbeer of documents returned in a query (in this case just 3 document)
db.students.find().limit(3)
// Combine sort and limit
db.students.find().sort({gpa: -1}).limit(1)
// Filter documents if field is equal to a value (name == "Spongebob")
db.students.find({name: "Spongebob"})
// Filter with multiple fields
db.students.find({gpa: 4.0, fullTime: true})
// Select only a few fields (in this case, a "name") to return with "projection"
db.students.find({}, {name: true})
// Forcefully omit id field from a query
db.students.find({}, {_id:false, name: true})
// Update a field in a document
db.students.updateOne({ name: "Spongebob" }, { $set: {fullTime: true} })
// Update a field in a document (using ID)
db.students.updateOne({_id: ObjectId('68a7dd81785f6134dc89b03d')}, { $set: {fullTime: false} })
// Remove/unset a field in a document (using ID)
db.students.updateOne({_id: ObjectId('68a7dd81785f6134dc89b03d')}, {$unset: {fullTime:""}})
// Update multiple documents at once
db.students.updateMany({}, {$set: {fullTime: false} })
// Update only those documents if a particular field doesn't exists
db.students.updateMany({fullTime:{$exists: false}}, {$set: {fullTime: true}})
// Delete a document that matched a filter (in this case name == "Larry")
db.students.deleteOne({name: "Larry"})
// Delete multiple documents
db.students.deleteMany({fullTime: false})
// Delete multiple documents that have a missing field (in this case "registerDate")
db.students.deleteMany({registerDate: {$exists:false}})
// Comparison operators
// Example: db.students.find({age: {$gt: 25}) // age > 25
{ field: value } // field == value
{ field: { $eq: value } } // field == value
{ field: { $ne: value } } // field !== value
{ field: { $lt: value } } // field < value
{ field: { $lte: value } } // field <= value
{ field: { $gt: value } } // field > value
{ field: { $gte: value } } // field >= value
{ field: { $gte: 3, $lte: 4 } } // field >= 3 && field <= 4
{ field: { $in: ["a", "b", "c"] } } // field in ["a", "b", "c"]
{ field: { $nin: ["a", "b", "c"] } } // field not in ["a", "b", "c"]
// Logical operators
{ $and: [{a:4}, {b:5}, {c:6}] } // a==4 && b==5 && c==6
{ $or: [{a:4}, {b:5}, {c:6}] } // a==4 || b==5 || c==6
{ $nor: [{a:4}, {b:5}, {c:6}] } // !(a==4 || b==5 || c==6)
{ field: { $not: {$gte: 4} } } // !(field > 4)
// Indexes
db.student.getIndexes() // show all indexes
db.student.createIndex({name: 1}) // add index to 'name' with ASC order
db.student.dropIndex("name_1") // drop an index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment