Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on

MongoDB Cheatsheet

Basic Commands:

Command Description
mongod Starts the MongoDB daemon
mongo Starts the MongoDB shell
use <db> Switches to a database
show dbs Lists all databases
show collections Lists all collections in the current database
db.collection.find(query) Queries a collection
db.collection.insert(doc) Inserts a document into a collection
db.collection.update(query, update) Updates a document in a collection
db.collection.remove(query) Removes documents from a collection

Querying Documents

Operation Syntax Description
Equality { field: value } Matches documents where the specified field is equal to the specified value
Less Than { field: { $lt: value } } Matches documents where the specified field is less than the specified value
Greater Than { field: { $gt: value } } Matches documents where the specified field is greater than the specified value
In { field: { $in: [value1, value2, ...] } } Matches documents where the specified field matches any of the specified values
Not In { field: { $nin: [value1, value2, ...] } } Matches documents where the specified field does not match any of the specified values

Inserting Documents

Operation Syntax Description
Insert One db.collection.insertOne(document) Inserts a single document into a collection
Insert Many db.collection.insertMany([document1, document2, ...]) Inserts multiple documents into a collection

Updating Documents

Operation Syntax Description
Update One db.collection.updateOne(query, update) Updates a single document in a collection
Update Many db.collection.updateMany(query, update) Updates multiple documents in a collection
Replace One db.collection.replaceOne(query, document) Replaces a single document in a collection

Deleting Documents

Operation Syntax Description
Delete One db.collection.deleteOne(query) Deletes a single document from a collection
Delete Many db.collection.deleteMany(query) Deletes multiple documents from a collection

Query Operators

Query Operator Description
$eq Matches documents where the value is equal to the specified value.
$ne Matches all documents where the value is not equal to the specified value.
$gt Matches all documents where the value is greater than the specified value.
$gte Matches all documents where the value is greater than or equal to the specified value.
$lt Matches all documents where the value is less than the specified value.
$lte Matches all documents where the value is less than or equal to the specified value.
$in Matches any of the values specified in an array.
$nin Matches none of the values specified in an array.
$and Joins query clauses with a logical AND.
$or Joins query clauses with a logical OR.
$not Inverts the effect of a query operator.
$type Matches documents where the value is of a specified BSON type.
$exists Matches documents where the field exists.
$mod Performs a modulo operation on the value of a field and selects documents with a specified result.
$regex Selects documents where values match a specified regular expression.
$options Modifies the $regex operator to include options.
$text Performs text search.
$where Matches documents that satisfy a JavaScript expression.

Top comments (0)