Download presentation
Presentation is loading. Please wait.
Published byTerence Andrews Modified over 5 years ago
1
Advanced Topics in Functional and Reactive Programming: MongoDB, Mongoose
Majeed Kassis
2
MongoDB First released in 2007
Written in C++ MongoDB (from "humongous") is a cross-platform document- oriented database. Data storage is cheap! a NoSQL database Not a table-based relational database! Uses JSON-like documents with dynamic schemas instead. Document-oriented style.
3
NoSQL vs SQL: Terminology
MongoDB RDBMS Collection Table Document Row (Record) Index Embedded Document Join Reference Foreign Key
4
Document Documents are equivalent to records or rows of data in SQL.
While a SQL row can reference data in other tables, In Mongo documents usually combine that in a document Fields or attributes are similar to columns in a SQL table.
5
Collection Collections:
They are equivalent to tables in relational databases. They can hold multiple JSON documents.
6
Document Data Model: Example
Relational MongoDB { first_name: ‘Paul’, surname: ‘Miller’, city: ‘London’, location: [45.123,47.232], cars: [ { model: ‘Bentley’, year: 1973, value: , … }, { model: ‘Rolls Royce’, year: 1965, value: , … } } Instead of having two tables with a foreign keys, we have a document with a nested document inside it.
7
Benefits of the Document Model
Agility and flexibility Data model supports business change Rapidly iterate to meet new requirements Intuitive, natural data representation Eliminates ORM layer Developers are more productive SQL can be a daunting task: Schemas, ORM Layers Reduces the need for joins, disk seeks Programming is more simple Performance delivered at scale
8
MongoDB Features { first_name: ‘Paul’, surname: ‘Miller’,
Rich Queries Find Paul’s cars Find everybody in London with a car built between 1970 and 1980 { first_name: ‘Paul’, surname: ‘Miller’, city: ‘London’, location: [45.123,47.232], cars: [ { model: ‘Bentley’, year: 1973, value: , … }, { model: ‘Rolls Royce’, year: 1965, value: , … } } Geospatial Find all of the car owners within 5km of Trafalgar Sq. Text Search Find all the cars described as having leather seats Aggregation Calculate the average value of Paul’s car collection Map Reduce What is the ownership pattern of colors by geography over time? (is purple trending up in China?)
9
Referencing: Example Contacts { _id : 2, name : “Mark Mark”,
title : “VP, New Product Development”, company : “Apple Computer”, phone : “ ”, address : { “$ref”: Addresses, “$id”: 1 } Addresses { _id : ObjectId(1), street : “10260 Bandley Dr”, city : “Cupertino”, state : “CA”, zip_code : ”95014”, country : “USA” } Code: >var user = db.users.findOne({"name":“Mark Mark"}) //finds Contact >var dbRef = user.address //retrieves its ‘address’ object reference >db[dbRef.$ref].findOne({"_id":(dbRef.$id)}) //gets its Address object
10
Schemas are flexible! { name : “Steven Jobs”,
title : “VP, New Product Development”, company : “Apple Computer”, address : { street : “10260 Bandley Dr”, city : “Cupertino”, state : “CA”, zip_code : ”95014” }, phone : “ ” } { name : “Larry Page”, url : “ title : “CEO”, company : “Google!”, address : { street : “555 Bryant, #106”, city : “Palo Alto”, state : “CA”, zip_code : “94301” } phone : “ ”, fax : “ ”
11
MonogoDB implements CRUD
Create Operations Create or insert operations add new documents to a collection. If the collection does not exist, insert operations will create the collection. Read Operations Read operations retrieves documents from a collection. Queries a collection for documents. Update Operations Update operations modify existing documents in a collection. Delete Operations Delete operations remove documents from a collection.
12
Insert Operations: InsertOne/InsertMany
13
Read Operation: find
14
Update Operations:UpdateOne/UpdateMany/replaceOne
15
Delete Operations: deleteOne/deleteMany
16
Mongoose Mongoose provides: It includes:
a straight-forward schema-based solution to model application data. It includes: Built-in type casting Validation Query building Business Logic hooks Mongoose only adds schemas for the application data. Mongoose does not enforce the schemas on MongoDB!
17
Mongoose MongoDB Relationship
18
Mongoose: The Object-Document Modeler
Mongoose model objects provide structure to the data. Allows the programmer to manage the model inside the application. This is in contrary to SQL databases where management is done on the database itself, away from the programmer’s control. Models provide consistent naming and typing. Allows for easier interaction with data stored in the database. Mongoose was built as Object-Document Modeler (ODM). Which means it couples each document found in the database with an object suitable for it. That object is used to access and modify the database data specific for this object type.
19
How does Mongoose work? Mongoose opens a pool of five reusable connections when it connects to a MongoDB database. This pool of connections is shared between all requests! Best practice: Open the connection at application starts up. Leave it open until application shut down. Mongoose can handle multiple databases Each connected can be made to specific database inside MongoDB. var usrDB = mongoose.createConnection(dbURIUsr);
20
Setting up the MongoDB Installing and Running MongoDB:
npm install mongodb MongoDB Compass for DB visualization, and make some DB, lets say ‘mern’ Run mongoDB using “mongod” file Now MongoDB is ready for connecting Configuring MongoDB in Nodejs application: Always connect from server side JS file. Preferably ‘app.js’ or ‘server.js’ file, being your main server file. Create ‘configure.js’ file containing path to MongoDB server:
21
Getting Started with Mongoose
Installing: Useful Links: Package Info: API: Import: (both are applicable) import mongoose from 'mongoose'; const mongoose = require('mongoose'); $ npm install mongoose
22
Connecting to MongoDB using Mongoose
In the main server file: Where: We use Javascript Promises to be Mongoose Promises.
23
Mongoose Naming Conventions
Collection: Contains many Documents Contains many “entries”, “rows” Document: Contains one entry of data Schema: Used to model one entry of data A Schema contains multiple paths. Path: Path is a pair of key:value Each path is a one entry of the document. One value of the row.
24
Data Types used in Mongoose
String Convert it to uppercase/lowercase Trim data prior to saving A regular expression that can limit data allowed to be saved during the validation process An enum that can define a list of strings that are valid Number Max Min Date Buffer Used to store binary data Boolean Mixed Allows every type ObjectId Allows linking to other document Array List of a certain type.
25
Options for Mongoose Data Types
Each data type allows deciding the follows options: a default value a custom validation function indicate a field is required a get function that allows you to manipulate the data before it is returned as an object a set function that allows you to manipulate the data before it is saved to the database create indexes to allow data to be fetched faster
26
Creating a Schema var schema = new Schema({ name: String, binary: Buffer, living: Boolean, updated: { type: Date, default: Date.now }, age: { type: Number, min: 18, max: 65 }, mixed: Schema.Types.Mixed, _someId: Schema.Types.ObjectId, decimal: Schema.Types.Decimal128, array: [], ofString: [String], ofNumber: [Number], ofDates: [Date], ofBuffer: [Buffer], ofBoolean: [Boolean], ofMixed: [Schema.Types.Mixed], ofObjectId: [Schema.Types.ObjectId], ofArrays: [[]], ofArrayOfNumbers: [[Number]], nested: { stuff: { type: String, lowercase: true, trim: true } } }) Creating a new Schema is done by creating a Schema object. The name should be suffixed by “Schema” word to avoid confusion. Each entry in the Schema is made of “Key: Type” pair. Types can be one of many ⇉ You can also define default values, and also limit values. Complete guide:
27
Model: Mapping a Document
Models are constructors compiled from the Schema definitions. One instance of these models represent one document which can be saved and retrieved from the database. All document creation and retrieval is handled by these models.
28
Defining Models and Constructing Documents
Creating a Schema: Constructing Documents using defined Schema:
29
CRUD Operations Support – Complete API
Mongoose provides static helper functions for CRUD operations. Each of these functions returns a mongoose Query object Create: Model.save() Read: Model.find() Model.findById() Model.findOne()
30
CRUD Operations Support – Complete API
Update: Model.findByIdAndUpdate() Model.findOneAndUpdate() Model.replaceOne() Model.updateMany() Model.updateOne() Delete: Model.deleteMany() Model.deleteOne() Model.findByIdAndDelete() Model.findByIdAndRemove() Model.findOneAndDelete() Model.findOneAndRemove()
31
Querying the Database: find and where
Finding documents using find: Finding documents using where: Chaining: (this can be done with all functions) Note: “callback” is a function you define to be executed on the result. This is done due to the asynchronous nature of MongoDB functions. User.find({age: {$gte: 21, $lte: 65}}, callback); User.where('age').gte(21).lte(65).exec(callback); User .where('age').gte(21).lte(65) .where('name', /^b/i) ... etc
32
Queries: Chaining Conditions
Conditions on queries can be chained: Callback is a function that to be executed on the query results: User.find({ admin: true }).where('created_at').gt(monthAgo).exec(callback); let callback = function(err, users) { if (err) throw err; // show the admins in the past month console.log(users); }
33
Queries: Chaining Operations
Conditions and commands can be chained: This is the recommended programming practice. User.find({ admin: true }) .where('created_at') .gt(monthAgo) .exec() .then(users => console.log(users)) .catch(err => console.error(err));
34
Custom Helper Functions
Mongoose also provides the ability to configure several types of helper methods and properties. These can be used to further simplify working with data. They do not modify the database! Only the model itself. Virtual Property: Implemented by creating a new property for the model Requires implementing both get and set methods of the property Syntax: schemaObject.virtual(‘newPropertyName').get(callback) schemaObject.virtual(‘newPropertyName').set(callback)
35
Example Schema This schema will be used for the upcoming examples:
Simple schema, a user object. Two properties: first and last name. let mongoose = require('mongoose') let userSchema = new mongoose.Schema({ firstName: String, lastName: String }) module.exports = mongoose.model('User', userSchema)
36
Virtual Property Let’s create a virtual property called fullName which: Can be used to set values on firstName and lastName Can be retrieved as a combined value when read Implementation: Usage: userSchema.virtual('fullName').get(function() { return this.firstName + ' ' + this.lastName }) userSchema.virtual('fullName').set(function(name) { let str = name.split(' ') this.firstName = str[0] this.lastName = str[1] let model = new UserModel() // set using the new virtual property model.fullName = 'Thomas Anderson' console.log(model.toJSON()) // get the virtual property value console.log(model.fullName)
37
Virtual Methods: Instance and Static Methods
Instance Method: We can create custom helper methods on the schema and access them via the model instance. These methods will have access to the model object. Syntax: Static Method: Similar to instance methods, we can create static methods on the schema. They are accessed via the schema object itself! schemaObject.methods.newfunctionName = function() { //implementation } schemaObject.statics.newfunctionName = function() { //implementation }
38
Instance Method: Example
Let’s create a function to return the initials for the current user. We add a custom helper method called getInitials to the methods section of the schema Implementation: Usage: userSchema.methods.getInitials = function() { return this.firstName[0] + this.lastName[0] } let model = new UserModel({ firstName: 'Thomas', lastName: 'Anderson' }) let initials = model.getInitials() console.log(initials) // This will output: TA
39
Static Method Let’s create a method to retrieve all users in the database Implementation: Usage: userSchema.statics.getUsers = function() { return new Promise((resolve, reject) => { this.find((err, docs) => { if(err) { console.error(err) return reject(err) } resolve(docs) }) UserModel.getUsers() .then(docs => console.log(docs)) .catch(err => console.error(err))
40
Mongoose Middleware Middleware are functions that run at specific stages of a pipeline. Also called pre and post hooks These two functions are passed control during execution of asynchronous functions The first one is executed before and the other one is executed after Example for pre and post hooks for save function:
41
Mongoose Middleware Types
Mongoose supports middleware for the following operations: Aggregate Document Model Query
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.