w6- advanced mongodb Flashcards

1
Q

How to have strongly-typed documents?

A

MongooseJS

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

MongooseJS

A

strongly-typed schemas, data validation, data pre-processing, and many other features.

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

one-to-one rel approaches?

A

author_id as property of book.

let Author = {
_id: 1234,
firstName: ‘Tim’,
lastName: ‘John’,
age: 35
};

let book1 = {
_id: 789,
title: ‘FIT2095 Book’,
author_id: 1234,
isbn: 9876
}

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

Mongoose

A

object data modelling (ODM) library that provides a modelling environment for your collections.

enforces structure as needed while still keeping the flexibility and scalability of MongoDB.

JavaScript framework commonly used in a Node.js application with a MongoDB database

built-in typecasting, validation, query building, business logic hooks and more out of the box.

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

How does Mongoose interact with MongoDB?

A

It uses the MongoDB driver to interact with MongoDB storage.
.

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

Mongoose SchemaTypes?

A

currently contains eight SchemaTypes that a property is saved as when it is persisted to MongoDB.

String

Number

Date

Buffer

Boolean

Mixed

ObjectId

Array

Decimal128

Map

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

SchemaTypes handle the definition of?

A

defaults

validation

getters

setters

field selection defaults for queries

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

Schema?

A

Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

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

Field is mandatory?

A

required:true
make a field required, set the field an object with two properties: the type and required, which needs a boolean value.

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

age: {
type: Number,
validate: {
validator: function (ageValue) {
return ageValue >= 10 && ageValue <= 110;
},
message: ‘Age should be a number between 10 and 110’
}
}

A

Validator if it is between 10 and 110

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

age: { type: Number, min: 5, max: 20 }

A

Anbother way to validator Mongoose

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

created: {
type: Date,
default: Date.now
}

A

field has default date

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

Boook schema referencing author

A

author: {
type: mongoose.Schema.Types.ObjectId,
ref: ‘Author’
},

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

Mongoose models?

A

Models are higher-order constructors that take a schema and create an instance of a document equivalent to records in a relational database

A Mongoose model is a wrapper on the Mongoose schema.

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

Difference b/w mongoose schema and mongoose model?

A

Mongoose schema defines the structure of the document, default values, validators, etc.,

whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

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

module.exports = mongoose.model(‘Author’, authorSchema);

A

to export a model, we need to invoke the model constructor and pass it a string represents the name of the collection and a reference to the schema.

author collection

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

let url=’mongodb://localhost:27017/libDB’;

A

Mongoose URL string which has syntax: mongodb://ServerAddress: Port//DbName

18
Q

format

A

adding new book
saving book

19
Q

Note: the creating and saving of book1 and book2 have been implemented inside the callback of author1.save function. Why?? N

A

Node.js is asynchronous and both book1 and book2 require the author’s ID (lines 31 and 43). Therefore, we have to create them after the save operation of the author is done.

20
Q

.insertMany() function

A

This function takes as input an array of documents (objects) and inserts them if they are valid.

21
Q

Mongoose models have several static functions that can be used for CRUD operations?

A

Model.deleteMany()

Model.deleteOne()

Model.find()

Model.findById()

Model.findByIdAndDelete()

Model.findByIdAndRemove()

Model.findByIdAndUpdate()

Model.findOne()

Model.findOneAndDelete()

Model.findOneAndRemove()

Model.findOneAndUpdate()

Model.replaceOne()

Model.updateMany()

Model.updateOne()

22
Q

Using ‘where’, we can create complex expressions. Example: Find all documents with firstName starting with the letter ‘T’ and the age >= 25.

A

Author.where({ ‘name.firstName’: /^T/ }).where(‘age’).gte(25).exec(function (err, docs) {
console.log(docs);

});

23
Q

What does exec mean?

A

Note exec indicates the end of the chain and invokes the callback function.

24
Q

and sort the results in ascending order by the age

A

Author.where({ ‘name.firstName’: /^T/ }).where(‘age’).gte(25).lte(35).limit(10).sort(‘age’).exec(function (err, docs) {
console.log(docs);

});

25
Author.find({ 'name.firstName': 'Tim' }, 'age', function (err, docs) { //docs is an array console.log(docs); });
Retrieving only certain fields Example: get the age of all documents with first name = 'Tim'
26
Populate?
Mongoose has a powerful aggregation operator called populate(), which allows you reference documents in other collections.
27
Book.find({}).populate('author').exec(function (err, data) { console.log(data); });
For exmaple, to get the books and their authors' details:
28
Author.updateOne({ 'name.firstName': 'Alex' }, { $set: { 'name.firstName': 'John' } }, function (err, doc) { console.log(doc); });
This document updates only the first document that matches the criteria.
29
Author.deleteOne({ 'name.firstName': 'Tim' }, function (err, doc) { console.log(doc); }); Author.deleteMany({ 'name.firstName': 'Tim' }, function (err, doc) { console.log(doc); });
deleteOne() function deletes a document based on condition (criteria). This function deletes all the documents that match the criteria.
30
Deployment workflow this unit- 3 stages
1.testing functionality in local machine 2.pushed to a VCS repository i.e Github - intermediary between IDE and GCP VM instances i.e production clon source code to VM instane- fetch dependencies- build and execute app IDE ->push->Github->clone-> GCP VM-> production!
31
Cloud computing models!
3 cloud computing models- IaaS PaaS SaaS
32
IaaS
Infrastructure as a Service- IaaS is a form of cloud computing that offers virtualized resources over the internet. In this form, you can create your network of resources: Virtual Machines (VMs), firewalls, and load balancers
33
PaaS
Platform as a Service-PaaS is a form of cloud computing where the infrastructure will be created and managed by the cloud vendor, and your responsibility as a developer is to provide the application's source code.
34
SaaS
Software as a Service- The cloud vendor will manage the infrastructure and application source code in this form.
35
Bootstrap
Bootstrap is the most popular CSS Framework for developing responsive and mobile-first websites.
36
Is integer?
var itemSchema = mongoose.Schema({ _id: mongoose.Schema.Types.ObjectId, itemName: String, quantity: { type: Number, validate: { validator: Number.isInteger, message: 'The quantity is not an integer value' } } });
37
Retrieve the first 50 documents with quantity between 100 and 150 inclusive.
Items.where('quantity').gte(100).lte(150).limit(50).exec(function (err, docs) { // Do something with Docs });
38
const mongoose = require("mongoose"); let doctorSchema = mongoose.Schema({ _id: { type: mongoose.Schema.Types.ObjectId, auto: true }, name: { firstName: { type: String, required: true, }, lastName: String, } }); Add a validator to the schema, so it only accepts documents with at least 5 characters in firstName. The message for invalid data is 'firstName length cannot be less than 5'.
const mongoose = require("mongoose"); let doctorSchema = mongoose.Schema({ _id: { type: mongoose.Schema.Types.ObjectId, auto: true }, name: { firstName: { type: String, required: true, validate:{ validator: function (firstNameValue){ return firstName.length >= 5; }, message = ''firstName length cannot be less than 5'. } }, lastName: String, } });
39
What are the roles on param in the following statement: module.exports = mongoose.model(param, mySchema);
param - unique name i.e String to reference collection , collection name in database.
40
How does populate work in mongoose? Support with example
Mongoose has a powerful aggregation operator called populate(), which allows you reference documents in other collections. For exmaple, to get the books and their authors' details: Book.find({}).populate('author').exec(function (err, data) { console.log(data); });