MongoDB Cheat sheet MongoDB Cheat sheet, Cheat Sheet of Database Programming

credit to owner credit to owner credit to owner credit to owner

Typology: Cheat Sheet

2021/2022

Uploaded on 09/13/2022

john-marvin-quilario
john-marvin-quilario 🇵🇭

2 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MongoDB Cheat Sheet
By Web Dev Simplified https://courses.webdevsimplified.com
Terminology
Database A container for collections. This is the same as a database in SQL and
usually each project will have its own database full of different collections.
Collection A grouping of documents inside of a database. This is the same as a table in
SQL and usually each type of data (users, posts, products) will have its own
collection.
Document A record inside of a collection. This is the same as a row in SQL and usually
there will be one document per object in the collection. A document is also
essentially just a JSON object.
Field
A key value pair within a document. This is the same as a column in SQL.
Each document will have some number of fields that contain information
such as name, address, hobbies, etc. An important difference between SQL
and MongoDB is that a field can contain values such as JSON objects, and
arrays instead of just strings, number, booleans, etc.
Basic Commands
mongosh Open a connection to your local MongoDB instance. All other commands
will be run within this mongosh connection.
show dbs Show all databases in the current MongoDB instance
use <dbname>
use myDatabase
Switch to the database provided by dbname
Switch to myDatabase
db
Show current database name
cls
Clear the terminal screen
show collec
ti
ons
Show all collections in the current database
db
.
d
r
o
pD
a
t
abase
()
Delete the current database
e
xit
E
x
it the mongosh session
pf3
pf4

Partial preview of the text

Download MongoDB Cheat sheet MongoDB Cheat sheet and more Cheat Sheet Database Programming in PDF only on Docsity!

MongoDB Cheat Sheet

By Web Dev Simplified https://courses.webdevsimplified.com

Terminology

Database

A container for collections. This is the same as a database in SQL and usually each project will have its own database full of different collections.

Collection

A grouping of documents inside of a database. This is the same as a table in SQL and usually each type of data (users, posts, products) will have its own collection.

Document

A record inside of a collection. This is the same as a row in SQL and usually there will be one document per object in the collection. A document is also essentially just a JSON object.

Field

A key value pair within a document. This is the same as a column in SQL. Each document will have some number of fields that contain information such as name, address, hobbies, etc. An important difference between SQL and MongoDB is that a field can contain values such as JSON objects, and arrays instead of just strings, number, booleans, etc.

Basic Commands

mongosh

Open a connection to your local MongoDB instance. All other commands will be run within this mongosh connection.

show dbs Show all databases in the current MongoDB instance

use

use myDatabase

Switch to the database provided by dbname Switch to myDatabase

db Show current database name

cls Clear the terminal screen

show collections Show all collections in the current database

db.dropDatabase() Delete the current database

exit Exit the mongosh session

Create

Each of these commands is run on a specific collection

db..

insertOne

db.users.insertOne({ name: “Kyle” })

Create a new document inside the specified collection Add a new document with the name of Kyle into the users collection

insertMany

db.users.insertMany([{ age: 26 }, { age: 20 }])

Create multi new documents inside a specific collection Add two new documents with the age of 26 and 20 into the users collection

Read

Each of these commands is run on a specific collection

db..

find

db.users.find()

Get all documents Get all users

find()

db.users.find({ name: “Kyle” }) db.users.find({ “address.street”: “123 Main St” })

Find all documents that match the filter object Get all users with the name Kyle Get all users whose adress field has a street field with the value 123 Main St

find(, )

db.users.find({ name: “Kyle” }, { name: 1, age: 1 }) db.users.find({}, { age: 0 })

Find all documents that match the filter object but only return the field specified in the select object Get all users with the name Kyle but only return their name, age, and _id Get all users and return all columns except for age

findOne

db.users.findOne({ name: “Kyle” })

The same as find, but only return the first document that matches the filter object Get the first user with the name Kyle

countDocuments

db.users.countDocuments({ name: “Kyle” })

Return the count of the documents that match the filter object passed to it Get the number of users with the name Kyle

Update

Each of these commands is run on a specific collection

db..

updateOne

db.users.updateOne({ age: 20 }, { $set: { age: 21 } })

Update the first document that matches the filter object with the data passed into the second parameter which is the update object Update the first user with an age of 20 to the age of 2 1

updateMany

db.users.updateMany({ age: 12 }, { $inc: { age: 3 } })

Update all documents that matches the filter object with the data passed into the second parameter which is the update object Update all users with an age of 12 by adding 3 to their age

replaceOne

db.users.replaceOne({ age: 12 }, { age: 13 })

Replace the first document that matches the filter object with the exact object passed as the second parameter. This will completely overwrite the entire object and not just update individual fields. Replace the first user with an age of 1 2 with an object that has the age of 13 as its only field

Complex Update Object

Any combination of the below can be use inside an update object to make complex updates

$set

db.users.updateOne({ age: 12 }, { $set: { name: “Hi” } })

Update only the fields passed to $set. This will not affect any fields not passed to $set. Update the name of the first user with the age of 12 to the value Hi

$inc

db.users.updateOne({ age: 12 }, { $inc: { age: 2 } })

Increment the value of the field by the amount given Add 2 to the age of the first user with the age of 12

$rename

db.users.updateMany({}, { $rename: { age: “years” } })

Rename a field Rename the field age to years for all users

$unset

db.users.updateOne({ age: 12 }, { $unset: { age: “” } })

Remove a field Remove the age field from the first user with an age of 12

$push

db.users.updateMany({}, { $push: { friends: “John” } })

Add a value to an array field Add John to the friends array for all users

$pull

db.users.updateMany({}, { $pull: { friends: “Mike” } })

Remove a value from an array field Remove Mike from the friends array for all users

Read Modifiers

Any combination of the below can be added to the end of any read operation

sort

db.users.find().sort({ name: 1, age: - 1 })

Sort the results of a find by the given fields Get all users sorted by name in alphabetical order and then if any names are the same sort by age in reverse order

limit

db.users.find().limit(2)

Only return a set number of documents Only return the first 2 users

skip

db.users.find().skip( 4 )

Skip a set number of documents from the beginning Skip the first 4 users when returning results. This is great for pagination when combined with limit.