MongoDB Flashcards

1
Q

What is MongoDB

A

No SQL or non-relational database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Collection

A

A group of documents in MongoDB. Do not enforce a rigid schema

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Document

A

a record stored in JSON-like format, and each document in the collection has a different structure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Visualize a collection

A

[
{
“name”: “Bob”,
“age”: 30
},
{
“name”: “Charlie”,
“age”: 35,
“hobbies”: [“cycling”, “hiking”]
}
]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Visualize a document

A

{“name”: “Alice”, “age”:25}
documents are stored ({..})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Query using .find()

A

db.collection.find()
General querying; returns matching documents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

$gt

A

db.collection.find({“field_name”: {“$gt”: data}})
Matches values greater than a specified value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Update a single document in a collection

A

$set
db.collection.updateOne({“filter_by_fieldname”:”filter_value”},{“$set”:{“age”:modified_data}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Delete a single document

A

.deleteOne
db.collection.deleteOne({“filter_name”:”condition”})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Delete multiple documents

A

.deleteMany
db.users.deleteMany({“age”:{“$lt”:30}}) // Deletes users younger than 30

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Delete a collection

A

.drop()
db.collection.drop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Import mongoDB into python

A

from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient(“mongodb://localhost:27017/”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Connect to or access a database

A

Insert a single document

db = client.name_of_database
#create or access the collection
my_collection = db.name_of_collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Insert a document

A

name_of_collect.insert_one({“name”: “Lorraine”, “age”: “Forever Young”})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

.find() vs. .find_one()

A

.find() - Retrieves all documents that match a query.
.find_one() - Retrieves only the first matching document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Get the field names present in a collection

A

myCollection_fields = mycollection.keys()
print(myCollection_fields)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Save a list of the fields of the collection

A

myCollection_fields = list(mycollection.keys())
print(myCollection_fields)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you filter?

A

db.collection.find(
{“field_name”: {“$operator”: “value”}}, # Filter condition
{“field1”: 1, “field2”: 1, “_id”: 0} # Projection (optional)
).sort(“field_to_sort”, 1).limit(10) #create a filter variable with constraints
filter_doc = {
‘gender’:’male’,
‘surname’: ‘%A’
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Value in a range

A

$in: <list></list>

db.myCollection.count_documents({
‘Countries’: {
‘$in’: [‘France’,’USA’]
}
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Create a filter criteria to count laureates who died (“diedCountry”) in the USA (“USA”). Save the document count as count.

A

Create a filter for laureates who died in the USA
criteria = {“diedCountry”: “USA”}
# Save the count of these laureates
count = db.laureates.count_documents(criteria)
print(count)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

$ne

A

criteria = { “diedCountry”: “USA”,
“bornCountry”: { “$ne”: “USA”},
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How many laureates were born in “USA”, “Canada”, or “Mexico”? Save a filter as criteria and your count as count.

A

Save a filter for laureates born in the USA, Canada, or Mexico
criteria = { “bornCountry”:
{ “$in”: [“Canada”, “Mexico”, “USA”]}
}
# Count them and save the count
count = db.laureates.count_documents(criteria)
print(count)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

$exists

A

Check for the existence of an field regardless of content
db.collection_name.count_documents(“prizes.0”: {“$exists”:True}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

fields.#

A

“field_name.0”
References a field and the documents and the position of the element with dot notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

T or F: This is how you would query documents if you wanted to check for documents without the field “born”

criteria = {“born”: {“$exists”: True}}

A

False. That checks for fields with.

This checks for fields without.
criteria = {“born”: {“$exists”: False}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Using the .distinct to show a list of countries that are in the diedCountry field that do not also appear in the bornCountry

A

countries = set(db.laureates.distinct(“diedCountry”)) - set(db.laureates.distinct(“bornCountry”))
print(countries)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

.len()

A

The len() function in Python calculates the number of elements in the list returned by .distinct().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

.set()

A

Converts data returned into a python set. A set is an unordered collection of unique elements in Python, which is ideal for performing set operations like union, intersection, and difference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

When do you need to use .set()

A

Eliminate Duplicates from a collection.
Perform Set Operations such as union, intersection, difference, or symmetric difference.
Ensure the uniqueness of elements in a collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Visualize how to find which countries have USA-born laureates had affiliations for their prizes?

A

db.laureates.distinct(“prizes.affiliations.country”, {“bornCountry”: “USA”})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

.distinct()

A

Retrieves unique values for a field.
db.prizes.distinct(“category”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

.findOne()

A

Retrieves a single matching document.
db.laureates.findOne({“bornCountry”: “USA”})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

.count_documents()

A

Counts the number of documents matching a query.
db.laureates.count_documents({“bornCountry”: “USA”})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

.aggregate()

A

Performs advanced queries, transformations, or grouping.
db.prizes.aggregate([{“$group”: {“_id”: “$category”, “count”: 1}}])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are the ways to query in MongoDB

A

.find, .distinct, .findOne, .count_documents, .aggregate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

$elemMatch

A

is a query operator in MongoDB used to match specific elements in an array field. It allows you to query for documents where at least one element in an array satisfies multiple conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Visualize how to use $elemMatch to find the number of laureates who won an unshared ({“share”: “1”}) prize in physics after World War II ({“year”: {“$gte”: “1945”}}) ?

A

db.laureates.count_documents({“prizes”:
{“$elemMatch”:{
“share”: “1”,
“category”: “physics”,
“year”: {“$gte”: “1945”}
}}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

$eq

A

{ age: { $eq: 25 } }
Matches values equal to a specified value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

$gte

A

{ age: { $gte: 25 } }
Matches values greater than or equal to a specified value.

40
Q

$lt and $lte

A

Matches values less than or equal to a specified value.

41
Q

$nin

A

{ age: { $nin: [20, 25, 30] } }
Matches none of the values in a specified array.

42
Q

What are the logical operators?

A

$and, $or, $not, $nor

43
Q

$nor

A

Matches documents that fail all conditions.

44
Q

Use an if statement to show the collection

A

collection = db.laureates
for document in collection.find():
print(document)

45
Q

Visualize how to: discover how many laureates in total have a first name beginning with “G” and a surname beginning with “S”?

A

db.laureates.count_documents({“firstname”: Regex(“^G”), “surname”: Regex(“^S”) })

46
Q

regex()

A

operator is used to perform pattern matching in string fields using regular expressions.
{ “field”: { “$regex”: pattern, “$options”: options } }

47
Q

^ (Caret)

A

Matches the start of a string.
{ “name”: { “$regex”: “^J” } }

48
Q

What matches the end of a string?

A

$ (Dollar Sign)
{ “name”: { “$regex”: “son$” } }

49
Q

What Acts as an escape character to include special characters in a regex pattern?

A

\ (Backslash)
{ “email”: { “$regex”: “\.” } }

50
Q

$options

A

The $options modifier allows you to control how the regex matches strings. It supports the following flags:
{ “name”: { “$regex”: “john”, “$options”: “i” } }

Common $options:
i: Case-insensitive matching.
m: Multiline mode.
x: Ignore whitespace and comments in the pattern.
s: Allows . to match newline characters.
u: Enables Unicode case foldin

51
Q

.*:

A

Matches any characters (zero or more)

52
Q

What Match Strings with Special Characters

A


{ “field”: { “$regex”: “\$” } }

53
Q

Visualize using a regular expression object to filter for laureates with “Germany” in their “bornCountry” value.

A

criteria = {“bornCountry”: Regex(“Germany”)}
print(set(db.laureates.distinct(“bornCountry”, criteria)))

54
Q

How do you import Regex for MongoDB?

A

from bson.regex import Regex

55
Q

.insert_many()

A

db.myCollection.insertMany([
{ name: “Charlie”, age: 35 },
{ name: “Diana”, age: 28 }
])

56
Q

project={}

A

To specify which fields you want to include or exclude in the query results.
To include only certain fields in the result, set their value to 1 in the projection. To exclude fields, set their value to 0. Note:
You cannot mix 1 (include) and 0 (exclude) in the same projection, except for _id.
docs = db.collection.find(
filter={“age”: {“$gt”: 30}}, # Filter: Find documents where age > 30
projection={“name”: 1, “age”: 1, “_id”: 0} # Include only “name” and “age”; exclude “_id”
)

57
Q

Visualize using a dictionary to get only the first name and last name from a dictionary.

A

Use projection to select only firstname and surname
docs = db.laureates.find(
filter= {“firstname” : {“$regex” : “^G”},
“surname” : {“$regex” : “^S”} },
projection={“name”: 1, “surname”: 1} )

Print the first document
print(docs[0])

58
Q

Iterate over the documents, and for each document, concatenate the first name and the surname fields together with a space in between to obtain full names.

A

Use projection to select only firstname and surname
docs = db.laureates.find(
filter= {“firstname” : {“$regex” : “^G”},
“surname” : {“$regex” : “^S”} },
projection= [“firstname”, “surname”] )

Iterate over docs and concatenate first name and surname
full_names = [doc[“firstname”] + “ “ + doc[“surname”] for doc in docs]

Print the full names
print(full_names)

59
Q

itemgetter

A

itemgetter(key) is used to retrieve a specific field (or key) from dictionaries within a list.

60
Q

visualize using itemgetter

A

from operator import itemgetter

61
Q

Create an index

A

collection.create_index([(“field_name”, 1)])
You would use 1 for ascending, -1 for descending.

62
Q

Purpose: Speed up queries by category and sort by year.
Purpose: Identify all unique prize categories.
Purpose: Get the most recent prize for a category awarded to a Purpose: Create a human-readable report.
Encapsulation: Wrap the logic in a function with parameters for collection and field names for reuse.

A

def laureate_report(collection, category_field=”category”, year_field=”year”, share_field=”laureates.share”, share_value=”1”):
# Create a compound index on category and year field index_model = [(category_field, 1), (year_field, -1)] collection.create_index(index_model)
report = “”
# For each distinct category (sorted alphabetically)
for category in sorted(collection.distinct(category_field)):
doc = collection.find_one(
{category_field: category, share_field: share_value},
sort=[(year_field, -1)] )
# Append the formatted category and year to the report
report += “{category}: {year}\n”.format(**doc)
return report

63
Q

An empty {}

A

An empty {} means “match everything”, so it retrieves all documents.

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

64
Q

Visualize retrieving a given page of prize data on laureates who have the word “particle” (use $regex) in their prize motivations (“prizes.motivation”). Sort laureates first by ascending “prizes.year” and next by ascending “surname”.

A

particle_laureates = list(
db.laureates.find(
{“prizes.motivation”: {“$regex”: “particle”}},
[“firstname”, “surname”, “prizes”])
.sort([(“prizes.year”, 1), (“surname”, 1)])
.skip(page_size * (page_number - 1))
.limit(page_size))

65
Q

Visualize how to get the below output with projection:

{“name”: “Bob”, “age”: 30}
{“name”: “Charlie”, “age”: 35}

A

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

66
Q

Visualize how to exclude age from the below output with projection:

{“_id”: ObjectId(“…”), “name”: “Bob”}
{“_id”: ObjectId(“…”), “name”: “Charlie”, “hobbies”: [“cycling”, “hiking”]}

A

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

67
Q

Visualize how to retrieve only users where age is greater than 30

A

db.users.find({“age”: {“$gt”: 30}})

68
Q

Visualize how to Retrieve a document where name is “Bob”

A

db.users.find_one({“name”: “Bob”})

69
Q

Visualize how to retrieve 2 users per page, skipping the first 2

A

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

70
Q

$match

A

Filters Documents (Like WHERE in SQL)
Filters documents before further processing in the pipeline.
Works just like find(), but inside an aggregation pipeline.
db.users.aggregate([
{ “$match”: { “age”: { “$gt”: 30 } } }
])
SQL EQuivalent: SELECT * FROM users WHERE age > 30;

71
Q

$project

A

Reshapes Output (Like SELECT in SQL)
Controls which fields to include/exclude in the final result.
Can also rename fields and create computed fields.
Can Performs field comparisons and calculations.
SQL Equiv:
SELECT name,
CASE WHEN age >= 30 THEN ‘Senior’ ELSE ‘Junior’ END AS age_category
FROM users;

72
Q

$group

A

Groups Documents (Like GROUP BY in SQL)
Groups multiple documents based on a shared field.
Used for aggregations like SUM(), COUNT(), AVG().

73
Q

$unwind

A

Expands Arrays (Like JOIN UNNEST in SQL)
Flattens arrays so that each array element becomes a separate document.
Helpful for analyzing nested array data.
If needed, use $unwind: { preserveNullAndEmptyArrays: true } to keep documents that don’t have arrays.

74
Q

What is the SQL equivalent to below code?

SELECT hobbies, COUNT(*) AS count
FROM users
WHERE age > 30
GROUP BY hobbies
ORDER BY count DESC;

A

db.users.aggregate([
{ “$match”: { “age”: { “$gt”: 30 } } }, # Step 1: Filter users over 30
{ “$unwind”: “$hobbies” }, # Step 2: Expand hobbies array
{ “$group”: { “_id”: “$hobbies”, “count”: { “$sum”: 1 } } }, # Step 3: Count hobbies
{ “$sort”: { “count”: -1 } }, # Step 4: Sort by most common
{ “$project”: { “hobby”: “$_id”, “count”: 1, “_id”: 0 } } # Step 5: Format output
])

75
Q

from collections import OrderedDict

A

It’s a special type of dictionary (dict) that remembers the order in which items were inserted.
In Python 3.6+, regular dictionaries also maintain insertion order, but OrderedDict is still useful for older versions or special cases.

76
Q

Visualize a pipeline creation

A

create_pipeline [
{“$match”: {filter_field: {“$in”: list(filter_values)}}}, # Filters documents
{“$project”: {field: 1 for field in project_fields}}, # Selects specific fields
{“$sort”: OrderedDict([(sort_field, sort_order)])} # Sorts results
]

77
Q

Aggregations (use of the $)

A

a process of transforming and analyzing data in MongoDB. Instead of just retrieving documents (find()), aggregation processes multiple documents to compute statistics, filter, group, or reshape data.
🔹 Example: Instead of listing all sales, we might want to compute the total revenue.

78
Q

Explain Field references → “fieldName” vs. “$fieldName”

A

✅ Use $ Before a Field Name to Reference It
When you want to refer to a field inside an aggregation stage, you must use $.

79
Q

T or F: These aggregation operators $match and $group start with $

A

False. All MongoDB aggregation operators (like $match, $group, $sum, etc.) start with $.

80
Q

When do you not use $?

A

For string literals
For field names outside of aggregations

81
Q

$lookup

A

Joins Collections (Like JOIN in SQL)
Performs a left outer join between two collections.
Adds matching documents from another collection.

82
Q

T or F: db[“prizes”].distinct(“category”) is the same as db.prizes.distinct(“category”)

83
Q

When to Use Brackets Instead of Dots?

A

If your collection name has special characters or spaces:
db[“prizes-data”].find()
If your collection name matches a Python keyword:
db[“class”].find()

84
Q

$expr

A

$expr allows you to use aggregation expressions inside a find() query.
It enables comparisons between fields within the same document, which $match alone cannot do.
It allows you to use operators like $gt, $eq, $and, etc., on document fields dynamically.

85
Q

True or false. $expr and $match perform field comparisons and computations

A

False. Only $expr allows field comparisons and computations. $match does standard filtering and simple conditions like field = value or field > value.

86
Q

.stats()

A

db.collection.stats()
Use this to see size, storage, and index details.

87
Q

db.collection.explain()

A

db.collection.find({“Rank”: {“$gte”: 4}}).explain(“executionStats”)
Use this to see how MongoDB executes a query.

88
Q

.aggregate()

A

MongoDB’s .aggregate() function is used for advanced data processing. It allows you to filter, group, sort, transform, and analyze data efficiently.

Think of .aggregate() as a pipeline where documents pass through stages that modify or filter them.

89
Q

.creatIndex()

A

db.collection.createIndex({ “Rank”: 1 })

90
Q

.getIndexes()

A

shows how to get indexes that exist

db.collections.getIndexes()

91
Q

Visualize using $project to ✅ Find employees whose bonus is greater than 10% of their salary

A

db.employees.aggregate([
{“$project”: { “name”: 1, “salary”: 1, “bonus”: 1,
“bonus_gt_10_percent”: {
“$gt”: [“$bonus”, { “$multiply”: [“$salary”, 0.1] }]}}}})

92
Q

$indexOfBytes

A

Finds the position of a substring inside a string.
Returns -1 if the substring is not found or True/False
Works case-sensitive (unlike $indexOfCP which is Unicode-aware).

93
Q

$addFields

A

Add a new field inside a sub-document (address.full)
db.users.aggregate([
“$addFields”: {“address.full”: { “$concat”: [“$address.city”, “, “, “$address.country”] }}])
Input:
{ “name”: “Alice”, “address”: { “city”: “New York”, “country”: “USA” } }
Output:
{ “name”: “Alice”, “address”: { “city”: “New York”, “country”: “USA”, “full”: “New York, USA” } }

94
Q

Create a new database

A

use my_database