Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Topics in Concurrency and Reactive Programming: MongoDB, Mongoose Majeed Kassis.

Similar presentations


Presentation on theme: "Advanced Topics in Concurrency and Reactive Programming: MongoDB, Mongoose Majeed Kassis."— Presentation transcript:

1 Advanced Topics in Concurrency 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

5 Collection

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: 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.

18 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);

19 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:

20 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

21 Connecting to MongoDB using Mongoose
In the main server file: Where: We use Javascript Promises to be Mongoose Promises.

22 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.

23 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.

24 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

25 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:

26 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.

27 Defining Models and Constructing Documents
Creating a Schema: Constructing Documents using defined Schema:

28 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

29

30

31

32


Download ppt "Advanced Topics in Concurrency and Reactive Programming: MongoDB, Mongoose Majeed Kassis."

Similar presentations


Ads by Google