MongoDB Basics Flashcards
(50 cards)
How to list all databases?
show dbs
Write a query to create a new database named games
use games
Write a query to create a collection named dogsBreeds
db.createCollection("dogBreeds")
db
doesn’t need to be replaced by the actual name of the database
How to list all collections in a database?
show collections
What is a collection?
It is a group of documents. It is equivalent to the table in relational databases.
For example, students from a school would be grouped into the students
collection, with one document holding the information of a student.
Write a query that lists all documents in the students
collection
db.students.find()
Do documents in a collection need to share the same structure?
No, there schema is not enforced within instances of a collection, in fact, each document can have its own set of fields.
Remember that a flexible schema is one of the characteristics from MongoDB’s document store.
What does the acronym CRUD stands for?
Create, Read, Update and Delete (operations).
These are basic operations to manipulate data from a database.
Write a query to count the number of documents in a collection?
db.collection_name.countDocuments()
Write a query to insert the following data in the students
collection.
name: Ana Bell
age: 34
email: anabell@uni.com
Write a query to insert the following data in the books
collection.
title: A Brief History of Time: From the Big Bang to Black Holes
author: Stephen Hawking
title: Sapiens: A Brief History of Humankind
author: Yuval Harari
title: The 4-Hour Workweek
author: Timothy Ferriss
Write a query to find the document within the books
collection that has the following title
The 4-Hour Workweek?
db.books.find({"title": "The 4-Hour Workweek"})
What happens when findOne()
is used and there are multiple documents that match the criteria provided (see example below)?
db.students.findOne({"liveInCampus": True})
It will return only the first match.
Write a query to find the document within the students
collection for which the id
is equal to 202503456 (string).
db.students.find({"id": "202503456"})
What happens when deleteMany()
is used and there are multiple documents that match the criteria provided (see example below)?
All documents that match the criteria will be deleted.
What are the two commands for deleting one and many documents from a collection?
db.collection_name.deleteOne({"id": "123"})
db.collection_name.deleteMany({"id": "123"})
Write a query that updates all documents in the books
collection, that have genre
equal to Drama to include a field called onSale
and set it to True
.
db.books.UpdateMany({"genre": "Drama"}, {$set: {"onSale": True}});
Explain why using indexing in database is benefitial and give an real-world example of
Indexes allows us to read data without having to loop over the entire set of documents in a collection, similar to how to an index works in a book.
Suppose we are searching for a document with id
equals to 1534534. Without an index, MongoDB must scan all documents in the collection and check if id == 1534534
Alternatively, if we create an index for the id
and sort it, then the search operations becomes much faster as we just need go straight for the index.
Ultimately, having the index improved the efficiency of the read operation.
For example, when we want look for a word in a physical dictionary, there is no need to go through the whole book just to find one word.
The dictionary is indexed already so one just need to look for the first letter fo the word, then the second one and so on until we find the word we are looking for.
When should I create an index for a MongoDB database?
Indexes should be created for the most frequent queries.
One can create a compound index with many fields, but adding to many fields to the index can hurt the efficiency of the read operation.
Specifically, a single compound index can include up to 32 fields, and a collection can include up to 64 indexes.
What is a compound index?
A compound index includes more than one field in its composition. It allows us to create an index for a frequent query that requires more than one field to return the information needed.
Complete the sentence below
An index in MongoDB is a special data structure that stores —– and —–
An index in MongoDB is a special data structure that stores Field being indexed and the Location of the corresponding document on disk.
In which format does MongoDB stores an index?
It stores as a (balanced) tree, containing the fields indexed and the path to the correspoding documents on disk.
What are the downsides of creating too many indexes?
- Too many indexes can slow down write operations (insert, update, delete) because indexes must be updated as well.
- Indexes consume disk space.
Write two queries:
1. Create an index for the bigdata
collection using the account_no
field in ascending order.
2. Show list of existing indexes