Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
Moving from Access Databases to a Visual Studio/SQL Server Solutions Andrew Couch UK Access User Group asc associates
Advertisements

Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Database A collection of related information stored on a computer and organized in a manner that allows access, retrieval, and use of that data.
Introduction to Databases CIS 5.2. Where would you find info about yourself stored in a computer? College Physician’s office Library Grocery Store Dentist’s.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Tutorial 11: Connecting to External Data
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
MongoDB An introduction. What is MongoDB? The name Mongo is derived from Humongous To say that MongoDB can handle a humongous amount of data Document.
Instructions: 1)Review the attached ppt, which is posted on the Media Center’s Research Assignments page. 2)Create one or two search queries,
Thanks to Bill Arms, Marti Hearst Documents. Last time Size of information –Continues to grow IR an old field, goes back to the ‘40s IR iterative process.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Aggregation.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Access: Queries Ad-hoc Reporting Chapter T. Access Queries Queries Access Properties Sorting Selection Criteria Calculations.
By: Rick Varella, Cuong Nguyen, & Henry Giathi. What is Books Zen Books? Books Zen Books is an e-commerce web application designed to allow students to.
22 November Databases. Presentations Tega: news 1954 Prediction.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Experience with XML Schema Ashok Malhotra Schema Usage  Mapping XML Schema and XML documents controlled by the Schema to object classes and instances.
Introduction to MongoDB. Database compared.
SEMI-STRUCTURED DATA (XML) 1. SEMI-STRUCTURED DATA ER, Relational, ODL data models are all based on schema Structure of data is rigid and known is advance.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
CS422 Principles of Database Systems Object-Oriented Features in DBMS Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Chengyu Sun California State University, Los Angeles
CS320 Web and Internet Programming SQL and MySQL
CS422 Principles of Database Systems Course Overview
CS520 Web Programming Spring – Inversion of Control
Chengyu Sun California State University, Los Angeles
Dineesha Suraweera.
Twitter & NoSQL Integration with MVC4 Web API
Aggregation Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together,
Template library tool and Kestrel training
ISC440: Web Programming 2 Server-side Scripting PHP 3
Thanks to Bill Arms, Marti Hearst
Intro to NoSQL Databases
Advanced Topics in Concurrency and Reactive Programming: MongoDB, Mongoose Majeed Kassis.
CS5220 Advanced Topics in Web Programming Spring – Web MVC
CS320 Web and Internet Programming Cookies and Session Tracking
How to Use “Indian Citation Index (ICI)”
CS5220 Advanced Topics in Web Programming Angular – Routing
CS3220 Web and Internet Programming Cookies and Session Tracking
CS5220 Advanced Topics in Web Programming JavaScript Basics
CS2011 Introduction to Programming I Objects and Classes
Intro to NoSQL Databases
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Introduction to MongoDB
CS3220 Web and Internet Programming SQL and MySQL
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS5220 Advanced Topics in Web Programming More Node.js
CS5220 Advanced Topics in Web Programming Angular – Services
Rules and Tips for Good Web Programming (and Programming in General)
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming SQL and MySQL
CS5220 Advanced Topics in Web Programming Angular – TypeScript
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming JavaScript Basics
CS5220 Advanced Topics in Web Programming More Node.js
Intro to NoSQL Databases
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS4540 Special Topics in Web Development SQL and MS SQL
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS5220 Advanced Topics in Web Programming Object-Document Mapping with Mongoose Chengyu Sun California State University, Los Angeles

Why Mongoose? Document != Object ObjectID is not object reference No methods Simplify data access and validation (similar validation capabilities were added to MongoDB in 3.6)

Basic Usage of Mongoose Example: test4.js mongoose package Schema and model Connect and disconnect Basic operations Callback and Promise

Schema and Model Mongoose Java Schema Data Model Class Model DAO Fields Methods Data Model Class Fields Methods Constructors Model Constructors Data access methods DAO Data access methods

Schema Example const mongoose=require('mongoose'); let userSchema = mongoose.Schema({ name: String, email: String }); Map to a collection in MongoDB Defines the structure of the documents in the collection: properties, methods, constraints, and indexes A _id field of ObjectId is created by default

Schema Types String Number Boolean Date Buffer ObjectId Decimal128 Schema.Types.ObjectId Decimal128 Schema.Types.Decimal128 Mixed Schema.Types.Mixed Array []

Schema Type Example Schema Schema new Schema({ title: String, text: String, date: Date, authorId: Schema.Types.ObjectId, tags: [String], comments: [{ author: { id: Schema.Types.ObjectId, name: String }, date: Date }] }); Schema Schema

Additional Property Options For all properties: required, default, validate … Type-specific: lowercase, uppercase, trim, min, max … Index-related: index, unique, sparse

Property Option Example new Schema({ username: { type: String, required: true, unique: true, lowercase: true, trim: true, validate: function(value){ return value.length > 6; } });

Add Methods to Schema userSchema.methods.name = function(){ return this.firstName + ' ' + this.lastName; } userSchema.statics.findByEmail = function(email, callback) { return this.find({email:email}, callback);

Model Models are constructors created from schema definitions Models can be used to create objects that are mapped to documents in database Models have built-in methods that access database and convert between objects and documents (like DAO in ORM)

Create Models User = mongoose.model('User', userSchema); or name: String, email: String });

Create Models Create a model with the given schema User = mongoose.model('User', userSchema); or User = mongoose.model('User', { name: String, email: String }); Create a model with the given schema Register the model with Mongoose under the given name (so it can be retrieved later) Return the model

CRUD with Model Example: blogs-test.js Use of Model methods and Model prototype methods Callback and Promise Model.find() returns a Query Query can be used a Promise (i.e. has a then() method) Query building

Ref and Populate Comparison to Jackson?? articleSchema = Schema({ author: { type: Schema.Types.ObjectId, ref: 'User' } }); articles = Article.find().populate('author'); Comparison to Jackson??

Inheritance userSchema = new Schema({name: String}); User = mongoose.model('User', userSchema); Student = User.discriminator('Student', new Schema({cin, String}) ); Model.discriminator() creates a new model with the given name by combining the given schema with the model's schema

About Inheritance Example: inheritance-test.js The documents of both models will be stored in the same collection The default discriminator field is __t Model.find() is not polymorphic though

Readings Mongoose Documentation