Mongoose Schema

>_ Constructor

Defines a schema with the definition. Definition of the schema need to in bson form. Means {fieldname :{field options}}

Syntax :- Schema(definition, [options])

Some key options are

  • autoIndex: bool - defaults to null (which means use the connection's autoIndex option)
  • capped: bool - defaults to false
  • collection: string - no default (collection name that we need)
  • versionKey: string - defaults to "__v"

example :-

var userSchema= new Schema({
  userid: {type:String, required:true, trim:true},
  chips: {type:Number}
});

Notes:-

  • Mongoose will add 's' letter to the collection . means if collection name is 'user' then it will be saved as 'users'.
  • If we need to give custon naming (without s) then use 'collection' option as
    new Schema({.....},{collection:user});

>_ Add()

If we want to add the filed to a defined schema we can do this using Add() method.

It's syntax is:
  add(obj,prefix)


Example:-
for the above userSchema now I need to add the fields

  1. 'regdate' to hold date of registration
  2. 'usertype' to indicate user is player or promoter (agent)
userSchema.add({
  regdate:Date, usertype:{type:string,default:'player'}
});

>_ get()

get the value of schema option. Means to know the value of an option which is defined for a schema we can use this.
For example I need to know does the collection is capped or not then I will use the get() method with capped option to know capped status.

Syntax:  get(optionkey)

Example:

console.log(userSchema.get('capped'));
console.log(userSchema.get('autoIndex));

>_ set()

Sets/gets a schema option.
Syntax:   set(key, [value])

Example:

userSchema.set('capped',false);
userSchema.set('strict',false);

>_ index()

Defines an index for the schema, means if we want to create an index to a schema then we can do using this method.
Compound index creation is not possible at schema definition (in schema() or add()) using index() method only we can create compound index.

Syntax:
index(fields, [options], [options.expires=null])

Note:-
Here options refers MongoDB index options like 'unique','sparse','name','expireAfterSeconds' etc.

Example:-

Create an index for the userid to userSchema.
userSchema.index({userid:1},{name:'useridindex',unique:true})

>_ method()

How to create an instance method for a schema in mongoose
Adds an instance method to documents constructed from Models compiled from this schema.
In order to add a method to a schema then we will use this.

Syntax:
   method(methodname, function code)

Example:-

var userSchema= new Schema({
  userid: {type:String, required:true, trim:true},
  chips: type:Double
});
userSchema.method('getInfo',function(){
  console.log(“userid:\t”+this.userid+”\tchips own:\t”+this.chips);
});
userSchema.method('getChips',function(){
  return this.chips;
});

Example to call those methods:-

var userModel = mongoose.model('users',userSchema);
var alex = new userModel({userid:'Alex',chips:10000,regdate:Date.now});
alex.getInfo();
console.log(alex.getChips());

>_ static()

How to create an instance method for a schema in mongoose
Adds static "class" methods to Models compiled from this schema.

Syntax:-
    static(method name,function def);
Example:-

userSchema.static('getSchemaName',function(){
  return 'users';
});
userSchema.static('saveDoc',function(query){
  // some coding
});

Way to call static methods:-

Schemaname.staticmethodname
Like
console.log(userSchema.getSchemaName());

>_ pre()

 

>_ post()

 

Copyright © 2018-2020 TutorialToUs. All rights reserved.