04 - MongoDB Data Structures Flashcards

1
Q

Explain in human terms

MONGO_HOST = “172.31.90.91”

A

replace with your server’s private IP address

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

Explain in human terms

MONGO_DB = "my_db"

A

name of database in the MongoDB server that you want to use

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

db = client[MONGO_DB]

A
  • If the database with the specified name does not exist, it gets created.
  • Note that the database gets created after data is added to it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

movies = db[“movies”]

A
  • If the collection does not exist yet, this will create a collection named movies
  • in the database that is currently being used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain in human terms
result = movies.insert_one( { “title” : “Jaws” } )

A
  • inserts one document in the movies collection
  • will return an InsertOneResult object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

result.inserted_id

A
  • to get the ObjectID
  • the inserted_id property holds the id of the inserted document
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when you don’t assign an id to a document?

A

MongoDB will create one automatically

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

Will this work? Why/why not?

movies.insert_one( { "_id" : [ "Star Wars",
                             	"The Empire Strikes Back",
                             	"Return of the Jedi" ] } )
A

No
_id shouldn’t be an array

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

Will this work? Why/why not?

movies.insert_one( { “Star Wars” } )

A

no
wrong format

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

Will this work? Why/why not?

movies.insert_one( { "_id" : "Star Wars" } )

A

Yes
* not an array
* follows format

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

When will this not work?

result = movies.insert_many( [ { "_id" : "Batman", "year" : 1989 },
                    	{ "_id" : "Home Alone", "year" : 1990 },
                    	{ "_id" : "Ghostbusters", "year" : 1984 },
                    	{ "_id" : "Ghostbusters", "year" : 1984 },
                    	{ "_id" : "Lord of the Rings", "year" : 2001} ] )
result.inserted_ids
A

the second ghostbusters

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

What happens when an insert fails?
Ordered vs Unordered

A

Ordered
* will stop processing inserts upon encountering an error.

Unordered
* will still attempt all of the others.
* inserts may be executed in a different order than you specified.

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

How to specify an insert is unordered?

A

ordered = False

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

Define

Field

A

key-value pair, a MongoDB document can have several fields

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

Differentiate

Field name and key

A

same

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

List

ways to delete

4

A
movies.delete_one()
movies.delete_many()
movies.drop() or db.drop_collection("movies")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What will be the result?

movies.find({"_id" : ObjectId("5b963e71175f8feddc46f1c3")}, {"title":0})
A

movie with specified id, no title

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

What will be the result?

movies.find({}, {"title":1})

A

gets all movies and shows title

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

What will the results of these be?

movies.find()
movies.find({})
A

both the same, gets all

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

When you use find(), MongoDB returns a pointer to the result set called?

A

cursor

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

Is cursor.hasNext() used in both mongosh and PyMongo?

A

mongosh only

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

How to sort

A

testcol.find().sort("a",1)
1 for ascending, -1 for descending

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

How to sort multiple keys?

A

testcol.find().sort([("a",1),("b",-1)])

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

Limit result & skip some results

A

testcol.find().sort("a",1).limit(5).skip(5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
# Define $lt
Exists and is less than
26
# Define $lte
Exists and is less than or equal to
27
# Define $gt
Exists and is greater than
28
# Define $gte
Exists and is greater than or equal to
29
# Define $eq
Equal to
30
# Define $ne
Does not exist or does but is not equal to
31
# Define $in
Exists and is in a set
32
$nin
Does not exist or is not in a set
33
Exists and is less than
# Define $lt
34
Exists and is less than or equal to
# Define $lte
35
Exists and is greater than
# Define $gt
36
Exists and is greater than or equal to
# Define $gte
37
Equal to
# Define $eq
38
Does not exist or does but is not equal to
# Define $ne
39
Exists and is in a set
# Define $in
40
Does not exist or is not in a set
$nin
41
# Define $or
Match either of two or more values
42
# Define $not
Used with other operators
43
# Define $nor
Match neither of two or more values
44
# Define $and
Match both of two or more values
45
# List Logical Query Operators | 4
$or $not $nor $and
46
Match either of two or more values
# Define $or
47
Used with other operators
# Define $not
48
Match neither of two or more values
# Define $nor
49
Match both of two or more values
# Define $and
50
# List Comparison Query Operators | 8
$gt $gte $lt $lte $eq $ne $in $nin
51
# Simplify `movies.find({"title" : {"$not" : {"$in" : ["Batman", "Godzilla"]}}})` | Find all movies that are not batman or Godzilla
`movies.find({"title":{"$nin":["Batman","Godzilla"]}})`
52
# Simplify `movies.find({"$nor": [ { "category" : {"$in": [ "action" ]} }, { "category" : {"$in": [ "adventure" ]} }]} )` | Finds neither action nor adventure movies
`movies.find({"$nor":[{"category":{"$in":["action","adventure"]}}]})`
53
Find all sci-fi movies
`movies.find({"category": "sci-fi"})`
54
Find either sci-fi or comedy movies
`movies.find({"category":{"$in":["sci-fi","comedy"]}})`
55
Find all movies that made profit
`movies.find({"$expr": {"$gt": [ "$revenue" , "$budget" ]}})` * compares revenue > budget
56
# List Array Query Operators | 3
* **$all**: Array field must contain all values listed. * **$size**: Array must have a particular size. e.g. $size : 2 means 2 elements in the array * **$elemMatch**: All conditions must be matched by at least one element in the array
57
# Differentiate `db.movies.find( { "category" : { "$all" : [ "sci-fi", "action" ] } } )` And ``` db.movies.find( { "category" : { $in : [ "sci-fi", "action" ] } } ) ```
$all: has both sci-fi and action $in: basta in the set
58
$all
Array field must contain all values listed
58
$size
Array must have a particular size. e.g. $size : 2 means 2 elements in the array
59
$elemMatch
All conditions must be matched by at least one element in the array
60
Array field must contain all values listed
$all
61
Array must have a particular size. e.g. $size : 2 means 2 elements in the array
$size
62
All conditions must be matched by at least one element in the array
$elemMatch
63
# Code All movies with 3 categories
`movies.find( { "category" : { "$size" : 3 } } )`
64
# Code All sci-fi and action movies
`movies.find( { "category" : { "$all" : [ "sci-fi", "action" ] } } )`
65
Find movies that were filmed in Florence, Italy using $elemMatch
``` movies.find( { "filming_locations" : { "$elemMatch" : { "city" : "Florence", "country" : "Italy" } } } ) ```
66
# LIst Update Functions | 3
``` replace_one() update_one() update_many() ```
67
Differentiate ``` replace_one() update_one()```
replace: whole document update: mutates
68
$inc
Increment a field’s value by the specified amount
69
$mul
Multiply a field’s value by the specified amount
70
$rename
Rename a field
71
$min
Updates the field value to a specified value if the specified value is less than the current value of the field
72
$max
Updates the field value to a specified value if the specified value is greater than the current value of the field
73
$currentDate
Set the value of a field to the current date or timestamp.
74
Increment a field’s value by the specified amount
$inc
75
Multiply a field’s value by the specified amount
$mul
76
Rename a field
$rename
77
Updates the field value to a specified value if the specified value is less than the current value of the field
$min
78
Updates the field value to a specified value if the specified value is greater than the current value of the field
$max
79
Set the value of a field to the current date or timestamp.
$currentDate