Skip to content

Instantly share code, notes, and snippets.

@rocktimsaikia
Last active January 18, 2021 08:46
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rocktimsaikia/34263b681a890e195bec69d4fbc1f364 to your computer and use it in GitHub Desktop.
🔥 A tiny mongodb command cheatsheet for my personal use. Because I tend to forget them quite often

Common mongo shell commands/methods

show dbs : Show all the databases.
use <database> : switch/move into a database.
show collections : Show all the collections in that database.
db.<collection>.find() : Show all documents in the collection.
db.<collection>.help() : Show help on collection methods.
db.<collection>.drop() : Drops or removes completely the collection.
db.<collection>.deleteMany() : Delete documents from the collection.
db.dropDatabase(): Deletes a database.

Comparison Query Operators

$eq : Matches values that are equal to a specified value.
$ne : Matches all values that are not equal to a specified value.
$gt : Matches values that are greater than a specified value.
$gte : Matches values that are greater than or equal to a specified value.
$lt : Matches values that are less than a specified value.
$lte : Matches values that are less than or equal to a specified value.
$in : Matches any of the values specified in an array.
$nin : Matches none of the values specified in an array.

Logical Query Operators

$and : Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$or : Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
$not : Inverts the effect of a query expression and returns documents that do not match the query expression.
$nor : Joins query clauses with a logical NOR returns all documents that fail to match both clauses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment