


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
credit to owner credit to owner credit to owner credit to owner
Typology: Cheat Sheet
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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.
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.
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.
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.
Open a connection to your local MongoDB instance. All other commands will be run within this mongosh connection.
use myDatabase
Switch to the database provided by dbname Switch to myDatabase
Create
db.
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
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
db.
db.users.find()
Get all documents Get all users
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
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
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
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
db.
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
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
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
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
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
db.users.updateMany({}, { $rename: { age: “years” } })
Rename a field Rename the field age to years for all users
db.users.updateOne({ age: 12 }, { $unset: { age: “” } })
Remove a field Remove the age field from the first user with an age of 12
db.users.updateMany({}, { $push: { friends: “John” } })
Add a value to an array field Add John to the friends array for all users
db.users.updateMany({}, { $pull: { friends: “Mike” } })
Remove a value from an array field Remove Mike from the friends array for all users
Read Modifiers
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
db.users.find().limit(2)
Only return a set number of documents Only return the first 2 users
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.