Lecture 12- MongoDB Flashcards

1
Q

What is a non-relational database?

A
  • Polymorphic data structure
  • Flexible schemas
  • Easy to scale large workloads
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the document model

A
  • JSON notation
  • Field-value pairs are separated by colon
  • Fields must be enclosed within quotation marks (string values are often quoted as well)
  • Two field-values are separated by commas
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a document?

A

A way to organize and store data as a set of field-value pairs in MongoDB

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

What is a collection?

A

An organized store of documents in MongoDB, usually with common fields between documents

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

What is the different between these two documents?

A

Two documents in the same collection but with different fields

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

Whats the difference between tabular (relational) data model vs document data model?

A
  • Tabular = related data is split across multiple records and tables
  • Document = related data is contained in a single, rich document
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe MongoDB query language

A
  • simple syntax
  • designed to query documents
  • only queries a single collection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is this query doing?

A

Find documents (in people collection) with the age field value $lt (less than) 30

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

What is this query doing?

A

Insert some real data on people

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

What is this query doing?

A

Find people whose age is below 30

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

How do you use MQL find?

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

What is insertOne(), insertMany(), writeConcern and ordered?

A
  • insertOne() = insert one document into a collection
  • insertMany() = insert an array of documents into a collection
  • writeConcern = sets the level of acknowledgement requested from MongoDB for write operations
  • ordered = for insertMany() there is an additional option for controlling whether the documents are inserted in ordered or unordered fashion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give an example using insertOne(), insertMany(), writeConcern and ordered

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

What is updateOne() and updateMany()?

A
  • updateOne() = update one document into a collection
  • updateMany() = update an array of documents into a collection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an example using updateOne() and updateMany()

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

What is deleteOne(), deleteMany() and writeConcern?

A
  • deleteOne() = deletes one document from a collection
  • deleteMany() = deletes many documents from a collection
  • writeConcern = sets the level of acknowledgement requested from MongoDB for write operations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Give an example using deleteOne(), deleteMany() and writeConcern

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

What are some MQL query operator comparisons and what they do?

A
  • $lt = exists and less than
  • $lte = exists and less than or equal to
  • $gt = exists and greater than
  • $gte = exists and greater than or equal to
  • $ne = does not exist or does not equal to
  • $eq = exists and equal to
  • $in = exists and in a set
  • $nin = does not exists or not in a set
  • $or = match either of two or more values
  • $not = used with other operators to negate
  • $nor = match neither of two or more values
  • $and = match both of two or more values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Give an example using $and

A
20
Q

Give an example using $not

A
21
Q

Can MongoDB operators be combined?

A

Yes, allows for more complex conditions to be included in the query

22
Q

AND and OR are what type of operators?

A

Boolean operators

23
Q

Give an example of a query using an implicit AND and OR

A
24
Q

Give an example of a query using explicit AND and OR

A
25
Q

Give an example of a query using the NOT operator

A
26
Q

What is the format of aggregation?

A
27
Q

Describe MongoDB aggregation framework

A
  • Complex operations broken into stages
  • Operators called within stages
  • Functionality within the DB
28
Q

What is the MQL find() equivalent to $match, $projection, $sort, $limit, $skip and $count?

A
29
Q

Complete this task using find() and aggregate(): run a query to find the first ten documents for farm ‘1’

A
30
Q

An aggregation pipeline is an ____ and is used to hold the stages to execute and the parameters for that stage

A

Array

31
Q

Each stage is a ____

A

Document (JSON like)

32
Q

What does $match, $project and $limit do?

A
  • $match = filters documents based on conditions
  • $project = selects specific fields to display
  • $limit =restricts the number of results returned
33
Q

Describe what is going on in this query

A
  • { $match: { “farm”: 1 } }: document format, filters the documents to include only those where farm is equal to 1
  • { $project: { “name”: 1, “milk”: 1, “-id”: 0 } } document format, reshapes each document by specifying which fields to include or exclude, “name”: 1 and “milk”: 1 mean to include the name and milk fields, * “-id”: 0 means to execute the -id field
  • { $limit: 10 }: limits the result set to a maximum of 10 documents
34
Q

What does $group do?

A

This stage takes the incoming stream of documents, and segments it (each group is represented by a single document)

35
Q

Complete this task: find the total marks for each student across all subjects

A
36
Q

Complete this task: find the maximum marks scored in each subject

A
37
Q

What is this query doing?

A
  • Find the top two subjects based on average marks
  • Finding the average marks per students
  • Sorting the output based on average marks in descending order
  • Limiting the output to two documents
38
Q

What does $addFields do?

A

This stage takes the incoming stream of documents and adds a new field to the document as it is processed

39
Q

What is this query doing?

A
  • A new field called age is being added to each document
  • Age is determined by the JavaScript function defined in the $function operator
  • Math.random() = generates a random number between 0 (inclusive) and 1 (exclusive)
  • Math.random() * 5 = scales the random number to a range between 0 and 5
  • (Math.random() * 5) +1 = adding 1 shifts the range to 1 to 6 (exclusive of 6)
  • Math.floor(…) = this rounds down the random number to the nearest whole number, giving an integer value between 1 and 5
  • The function generates a random integer between 1 and 5, which will be the value of age for each document
40
Q

What are the three main parameters for $function?

A
  1. Body = code that defined the function, generates a random number an assigns it to the age field
  2. Args = array of arguments that can be passed to the function, empty
  3. Lang = the language to use for the function, JavaScript
41
Q

What does $project do?

A

$project stage transforms the documents by specifying which fields to include or modify

42
Q

What is going on in this query?

A
  • “expense”: 1 (includes the field expense in the output)
  • “airline”: “$details.airline” (includes a field named airline, with its value coming from the details.airline field in the original document
  • “flight_class”: “$details.seat_class” = includes a field called flight_class, using the value from details.seat_class
  • “flight_type”:”$details.flight_type” = includes a field called flight_type, with its value from details.flight_type
  • overspend: { $subtract: [ “$spent”, “$budget” ] } = creates a new field called overspend by calculating the difference between spent and budget
  • $subtract operator that performs subtraction between two values
43
Q

What does $unwind do?

A
  • Deconstruct an array field outputing a document for each element
  • It deconstructs an array field to output a document for each element in that array
  • This means that each output document represents one of the array field values and each value will have its own document
  • It essentially splits a document that contains an array into multiple documents, each with a single value from the array
  • Specify a field path to indicate the array to be deconstructed or specify a document operator
  • Unwind nested arrays
  • Control ouput documents if there are empty or null arrays
  • Handling empty or missing arrays
44
Q

Give an example of a query using $unwind

A
45
Q

What does the $lookup stage do?

A
  • Performs a left outer join between two collections
  • Used to combine data from multiple collections based on a specific relationship
  • References documents from one collection within another collection
46
Q

What is going on in this query?

A
  • From = the name of the other collection you want to join with
  • LocalField = the field from the current collection that you want to match with localField
  • ForeignField = the field from the collection that you want to match with localField
  • As = the name of the new array field where the matching documents form the collection will be sorted
47
Q

What is this query doing?

A

Find the orders made by each customer