Download presentation
Presentation is loading. Please wait.
Published byMargrethe Ludvigsen Modified over 5 years ago
1
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
2
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)
3
Basic Usage of Mongoose
Example: test4.js mongoose package Schema and model Connect and disconnect Basic operations Callback and Promise
4
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
5
Schema Example const mongoose=require('mongoose');
let userSchema = mongoose.Schema({ name: String, 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
6
Schema Types String Number Boolean Date Buffer ObjectId Decimal128
Schema.Types.ObjectId Decimal128 Schema.Types.Decimal128 Mixed Schema.Types.Mixed Array []
7
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
8
Additional Property Options
For all properties: required, default, validate … Type-specific: lowercase, uppercase, trim, min, max … Index-related: index, unique, sparse
9
Property Option Example
new Schema({ username: { type: String, required: true, unique: true, lowercase: true, trim: true, validate: function(value){ return value.length > 6; } });
10
Add Methods to Schema userSchema.methods.name = function(){
return this.firstName + ' ' + this.lastName; } userSchema.statics.findBy = function( , callback) { return this.find({ }, callback);
11
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)
12
Create Models User = mongoose.model('User', userSchema); or
name: String, String });
13
Create Models Create a model with the given schema
User = mongoose.model('User', userSchema); or User = mongoose.model('User', { name: String, 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
14
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 as Promise (i.e. has a then() method) More about queries
15
Ref and Populate Comparison to Jackson?? articleSchema = Schema({
author: { type: Schema.Types.ObjectId, ref: 'User' } }); articles = Article.find().populate('author'); Comparison to Jackson??
16
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
17
About Inheritance Example: inheritance-test.js
The documents of both models will be stored in the same collection The default discriminator field is __t
18
Readings Mongoose Documentation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.