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
Q

Define

$lt

A

Exists and is less than

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

Define

$lte

A

Exists and is less than or equal to

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

Define

$gt

A

Exists and is greater than

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

Define

$gte

A

Exists and is greater than or equal to

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

Define

$eq

A

Equal to

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

Define

$ne

A

Does not exist or does but is not equal to

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

Define

$in

A

Exists and is in a set

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

$nin

A

Does not exist or is not in a set

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

Exists and is less than

A

Define

$lt

34
Q

Exists and is less than or equal to

A

Define

$lte

35
Q

Exists and is greater than

A

Define

$gt

36
Q

Exists and is greater than or equal to

A

Define

$gte

37
Q

Equal to

A

Define

$eq

38
Q

Does not exist or does but is not equal to

A

Define

$ne

39
Q

Exists and is in a set

A

Define

$in

40
Q

Does not exist or is not in a set

A

$nin

41
Q

Define

$or

A

Match either of two or more values

42
Q

Define

$not

A

Used with other operators

43
Q

Define

$nor

A

Match neither of two or more values

44
Q

Define

$and

A

Match both of two or more values

45
Q

List

Logical Query Operators

4

A

$or
$not
$nor
$and

46
Q

Match either of two or more values

A

Define

$or

47
Q

Used with other operators

A

Define

$not

48
Q

Match neither of two or more values

A

Define

$nor

49
Q

Match both of two or more values

A

Define

$and

50
Q

List

Comparison Query Operators

8

A

$gt
$gte
$lt
$lte
$eq
$ne
$in
$nin

51
Q

Simplify

movies.find({"title" : {"$not" : {"$in" : ["Batman", "Godzilla"]}}})

Find all movies that are not batman or Godzilla

A

movies.find({"title":{"$nin":["Batman","Godzilla"]}})

52
Q

Simplify

movies.find({"$nor": [ { "category" : {"$in": [ "action" ]} }, { "category" : {"$in": [ "adventure" ]} }]} )

Finds neither action nor adventure movies

A

movies.find({"$nor":[{"category":{"$in":["action","adventure"]}}]})

53
Q

Find all sci-fi movies

A

movies.find({"category": "sci-fi"})

54
Q

Find either sci-fi or comedy movies

A

movies.find({"category":{"$in":["sci-fi","comedy"]}})

55
Q

Find all movies that made profit

A

movies.find({"$expr": {"$gt": [ "$revenue" , "$budget" ]}})

  • compares revenue > budget
56
Q

List

Array Query Operators

3

A
  • $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
Q

Differentiate

db.movies.find( { "category" : { "$all" : [ "sci-fi", "action" ] } } )
And
~~~
db.movies.find( { “category” : { $in : [ “sci-fi”, “action” ] } } )
~~~

A

$all: has both sci-fi and action
$in: basta in the set

58
Q

$all

A

Array field must contain all values listed

58
Q

$size

A

Array must have a particular size. e.g. $size : 2 means 2 elements in the array

59
Q

$elemMatch

A

All conditions must be matched by at least one element in the array

60
Q

Array field must contain all values listed

A

$all

61
Q

Array must have a particular size. e.g. $size : 2 means 2 elements in the array

A

$size

62
Q

All conditions must be matched by at least one element in the array

A

$elemMatch

63
Q

Code

All movies with 3 categories

A

movies.find( { "category" : { "$size" : 3 } } )

64
Q

Code

All sci-fi and action movies

A

movies.find( { "category" : { "$all" : [ "sci-fi", "action" ] } } )

65
Q

Find movies that were filmed in Florence, Italy
using $elemMatch

A
movies.find( {
	"filming_locations" : {
  	"$elemMatch" : {
    	"city" : "Florence",
    	"country" : "Italy"
  	} } } )
66
Q

LIst

Update Functions

3

A
replace_one()
update_one()
update_many()
67
Q

Differentiate
~~~
replace_one()
update_one()```

A

replace: whole document
update: mutates

68
Q

$inc

A

Increment a field’s value by the specified amount

69
Q

$mul

A

Multiply a field’s value by the specified amount

70
Q

$rename

A

Rename a field

71
Q

$min

A

Updates the field value to a specified value if the specified value is less than the current value of the field

72
Q

$max

A

Updates the field value to a specified value if the specified value is greater than the current value of the field

73
Q

$currentDate

A

Set the value of a field to the current date or timestamp.

74
Q

Increment a field’s value by the specified amount

A

$inc

75
Q

Multiply a field’s value by the specified amount

A

$mul

76
Q

Rename a field

A

$rename

77
Q

Updates the field value to a specified value if the specified value is less than the current value of the field

A

$min

78
Q

Updates the field value to a specified value if the specified value is greater than the current value of the field

A

$max

79
Q

Set the value of a field to the current date or timestamp.

A

$currentDate