MongoDB & Mongoose Flashcards

1
Q

What is Mongoose?

A

Mongoose is an ODM ( Object Data Modeling ) library for MongoDB.

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

What Does Mongoose Provide?

A

It provides a higher level of abstraction from MongoDB.

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

What does an ODM allow us to do?

A

An ODM allows us to write plain JavaScript to interact with our database.

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

What are some Mongoose features?

A

Simple and rapid development, Schema creation, data modeling, and model creation.

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

How would you create a schema?

A

You create a schema by creating a variable and storing a new Mongoose schema instance.

const itemSchema = new mongoose.Schema()

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

What does the schema function accept?

A

The schema function accepts an object that describes how your document should be structured.

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

What are some properties our title can have in our schema?

A

Our title property can hold, type which defines what data type this field has to be.

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

How would you create a model?

A

First, define a variable for your model. The variable name should start with an uppercase letter.

Now, assign mongoose.model("ModelName", schema) to this variable. The string represents the model and collection name, while the schema defines the schema you’d like to use for your model.

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

How can we fetch documents from our database?

A

To fetch documents in our database, we can use the find method.

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

What types of helper functions does Mongoose provide for working with documents?

A

It provides several helper functions for CRUD operations.

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

What does a CRUD helper function return?

A

It returns a mongoose query object.

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

What does the find function accept?

A

It accepts an object that allows us to configure it to find any document we want.

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

How would you fetch every document in a collection?

A

To fetch every document, you want to use the find method and pass nothing inside.

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

How can find documents with the names of John and balance greater than or equal to 20?

A

Model.find( { name : 'John', balance : { $gte : 20 } } )

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

What helper function can you use to find an document by id?

A

You can use the findByIdmethod.

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

What are the three roles of route parameters in express?

A
  1. Route parameters capture dynamic data from the URL, typically inputted by the user or based on other dynamic factors.
  2. The captured data (e.g., userId) is used within the application to perform specific actions, such as fetching information from a database or rendering specific views based on the captured data.
  3. In some cases, based on the data captured through route parameters, the application can direct the user to other routes or perform additional logic to handle related operations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How would you define a parameter in express?

A

To define a parameter in express, they start with :.

/:parameter_name

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

Where are the parameter values stored?

A

They are stored in req.params.

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

With the findByIdfunction, what do we pass in?

A

We pass in the the document id.

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

What are queries?

A

Queries are operations that help us interact with our database.

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

What are Mongoose queries, and why can we await them?

A

Mongoose queries are objects used to interact with MongoDB databases. We can await them because they support asynchronous operations, allowing us to wait for database operations to complete without blocking the Node.js event loop.

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

How can we find a single document based on anything?

A

In Mongoose, you can find a single document based on any field or combination of fields using the findOne().

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

What method can we use to update a document by using id?

A

We can use the findByIdAndUpdate method.

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

What are other options for findByIdAndUpdate?

A

The other option is findOneAndUpdate.

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

What does findByIdAndUpdate take in as arguments?

A

It takes in a id you want to select from the collection.

The second argument is an object that contains the new data.

The third argument is an options object.

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

In findByIdAndUpdate options object, what can you pass in?

A

You can set runValidators as true. Which will validate the new data based on the schema.

You can set new to true. This will return the new document. By default, it returns the old document.

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

How can you delete a document, or maybe documents?

A

You can delete a document by using the following functions. findByIdAndDelete, or deleteMany.

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

What does deleteMany return?

A

It returns an object with a property of deleteCount that contains the number of documents deleted.

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

What does trim do in our schema?

A

It removed whitespace from the beginning and end of our string.

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

What does this mean type : [String]

A

It means, it should hold an array of strings.

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

What does required mean in our schema?

A

It means that certain field is required if the property required is set to true.

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

Are you able to set default values for your fields?

A

Yes

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

Should every field be required? If no, why?

A

No, because some values will be calculated throughout some period.

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

What does a query string start with?

URL String

A

It start with ?.

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

What does the query string hold?

A

It holds data about filtering, sorting, limit, page, and pagination.

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

How can we chain parameters in our query string?

A

We can chain parameters with &.

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

What does the following example mean? What object should we get from this query string?

?search=javascript

A

This means, we want to find a document with the search property set to javascript.

The query object would look like this

{ search : "javascript" }

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

How can we access the query string?

A

We can access the query string via our request object. In this object, we have the query object.

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

What is the first thing we need to do when trying to filter our data? Also, why?

A

The first thing we need to do is create a shallow copy of the req.query. We do this because we avoid altering the original array.

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

In our req.query copy, what do we do first?

What else should we remove after?

A

We must delete any other parameter that does not deal with filtering.

We should remove any properties that are undefined.

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

After our basic filtering logic is implemented, what do we do?

A

We pass in the object, into our find method so it creates a query. We then want to store it inside a variable.

41
Q

When a user wants to filter for <, >, or any other operations, what should we do to make this work?

A

We want to create a string, from the query object.

In our string, we want to replace any of the keywords that will enable them to do it by adding $ infront of it. Use REGEX!

42
Q

After replacing all of the keywords for advanced filtering, what should we do?

A

We need to turn it back into an object. Then, put this object into our find method.

43
Q

How can we sort in our API?

A

We should check if there is a sort object in our query.
If there is, split them to remove the ,. Then, join them spaced out.

44
Q

How can we add multiple sort values in the sort parameter.

A

You use ,

45
Q

What is sorting?
What happens where there more than 1?

A

Sorting is the way a collection is sorted based on the sort parameter.

it sorts from left to right.

46
Q

What does limiting fields mean?

A

It means we limit the amount of data we receive from a document.

47
Q

What do you need to do to start sorting?

A

You first must check if the sort object exist.

If it exist, we must split the object and removing the , and joining them together but spaced.

48
Q

What should you do once you are done with the fields logic?

A

Pass in the sort string into the select function.

49
Q

What should you do once you are done with the sort logic?

A

You pass in the string in a sort function.

50
Q

Should you hide sensitive information with fields? If no, where?

A

No, you want to hide them inside your schema logic.

51
Q

What does pagination allow us to do?

A

It allows us to send a certain amount of documents in pages.

52
Q

How do you add a page?

A

First, you need to create a page variable that hold the page the user wants or a default.

const page = req.query.page * 1 || 1

53
Q

What is a limit?
How do you add a limit?

A

A limit, will decide how many documents are on a page.

const limit = req.query.limit * 1 || 20

54
Q

How would you calculate the number of documents you want to skip to get to a certain page?

A

const skip = ( page- 1 ) * 1

55
Q

With the skip and limit variables, how can you now make your pagination work?

A

You want to use the skip method on the query which will allow you to skip to the desired page (skip x amount of docs). You also then want to chain the limit method in which you pass in the limit variable.

56
Q

What do queries have access to in Mongoose?

A

Queries in Mongoose have access to methods because queries are instances of the Mongoose Query class.

57
Q

What is aliasing?

A

Aliasing is when you create a route for a popular endpoint that will have a predefined query.

58
Q

Why must you put filter, sort, pagination, fields, limit and pages into a class?

A

So we can than re use them if we have to.

59
Q

What is a pipeline is mongo?

A

Documents can be processed as groups or as an entire collection, transforming the data step by step before it is sent to the user.

60
Q

Why are aggregation pipelines useful?

A

Aggregation pipelines are useful because they enable us to calculate statistics based on specific groups of documents or the entire collection.

61
Q

How do we create a pipeline?

A

Use the aggregate method on the model we want to work with. This method accepts an array containing the stages our data will pass through to achieve the desired results.

62
Q

What is a stage, and how should the stage name start?

A

Stage is an object representing a different task, and stage names begin with a $.

63
Q

What does $match do?

A

The$match stage filters documents that match the specified criteria.

64
Q

What does the $group do?

A

The $group stage separates documents into groups according to a “group key”. The output is one document for each unique group key.

65
Q

What does $addFields do?

A

$addFields stage to create a new property in our document.

66
Q

What does $project do?

A

This stage can remove properties from the docuements.

67
Q

What does the $push operator do?

A

It can create an array of values in our documents.

68
Q

What does the $month operator do?

A

Returns a month number of a given date.

69
Q

What does the $unwind stage do?

A

Deconstructs an array field from the input documents to output a document for each element.

70
Q

What is the importance of virtual properties?

A

Allows us to create a property and value computed from existing values.

71
Q

Are virtual properties saved in MongoDB?

A

No

72
Q

How would you create a virtual property?

A

You create a virtual property by calling the virtual function on the schema you want to use it on.

73
Q

What are arguments you pass in the virtual function?

A

The property name. The second argument is a get function. This function will accept a callback function which computes the value.

74
Q

How can you see your virtual properties? Either in console or in your JSON.

A

In your schema, you want to set the virtuals to true on toJSON and toObject

75
Q

Since virtual properties are not real properties what can you not do with them?

A

You can not create based on them.

76
Q

What is document middleware?

A

Document middleware allows us to perform actions on a document before it is saved or after it is saved.

77
Q

How would you create a pre middleware?

A

You can create a pre middleware method by calling the pre function on the schema you want to use.

78
Q

In pre document middleware, what does the this keyword refer to?

A

It refers to the document.

79
Q

Do post hooks happen before or after they are sent to the client?

A

It happens before it is sent to the client.

80
Q

How would you create a post hook?

A

You use the post function.

81
Q

In a post document middleware, what does doc refer to?

A

It refers to the specific document instance that was affected.

82
Q

What is the next function in the mongoose middleware?

A

Its for signaling mongoose to move on to the next middleware in the stack.

83
Q

What is query middleware?

A

Functions that run either before or after a query executes.

84
Q

When calling a pre hook on a query middleware when does it happen?

A

It happens before we await the entire query.

85
Q

In a query pre hook, what does the this keyword refer to?

A

It refers to the query itself.

86
Q

What does pre hook query let us do?

A

It lets us chain on other queries.

87
Q

What are some use cases for pre hook query middleware?

A

Allows us to filter our certain things from the main DB.

88
Q

In post query middleware what does the docs refer to?

A

It refers to all the docs returned from the query.

89
Q

What is aggregation middleware?

A

It allows us to modify entire pipelines defined in a certain schema.

90
Q

What does the this keyword refer to in a pre aggregate hook?
What do we want to focus on?

A

It refers to the aggregation object. We want to focus on the pipeline method.

91
Q

What is in the pipeline method? What can we do?

A

The pipeline method returns a array, where we can add stages if we want.

92
Q

What is data validation?

A

Ensures that when users enter values, they are in the correct format for each field in our document schema, and that values have been provided for all the required fields.

93
Q

What is sanitization?

A

Involves ensuring that input data is clean, meaning no malicious code is being injected into our database or the application itself.

94
Q

What a built in validation brought by mongoose?

A

minlength, maxlength, min, max.

95
Q

What is enum used for?

A

It allows us to set a predefined set of values.

96
Q

In a custom validator, what should a function return?

A

It should return true or false.

97
Q

What does the function have access to?

A

It has access to the value.

98
Q

Does the function have access to the this keyword?
If yes, why?

A

Yes, incase we want to validate data against existing data.

99
Q

What properties does the custom validate function, error message go in?

A

They both go inside a property called validate. The function goes into a property called validator. The message goes into a property called message.