At this point, our site is not called a dynamic site, because the data you want is bogus, so now we're going to design the database model.
MongooseThe tool module we used was mongoose, and he was able to model MongoDB in such an operation that there were several concepts in mongoose, namely
Schema: pattern, in which we define the data, define the field pair type, such as the string type, or the numeric type model: The models, compile the model, compile the incoming schema, and then generate the constructor documents: document
schema-Mode definition
var mongoose = require ('mogoose'); var New Mongoose. Schema ({doctor:string, title:string, language:string, country:string, Year:number, summary:string})
model-Compilation model
var mongoose = require (' Mongoose '); var Movieschema = require ('./schemas/movie '); var Movie = Mongoose.model (' movie '= movie
With the database model, things become better, now to instantiate the document, just call the model, that is, the constructor, pass in a piece of data, and then call the Save method, you can pass this data into the database
// documents-Document Instantiation var Movie = require ('./model/movie '); var New Movie ({title:' mechanical warfare police ', Doctor:' Jose Padilla ', Year:2018}) movie.save (function (Err) { if(err) { return handleError (ERR); }})
Database query into a variety of, query batch, single, or specify the conditions of the query, then the batch query only need to call the model Find method, pass an empty object is OK
// Documents-Database Bulk Query var Movie = require ('./models/movie '); App.get ('/',function(req,res) {movie. Find ({}). EXEC ( function (err,movies) {res.render (' index ', {title:' Imooc home ', movies:movies} )})
In a single word, pass in a specific key, such as Movie.findone
Documents- Database single query var Movie = require ('./models/movie '); App.get ('/',function (req,res) {Movie. FindOne ({_id:id}). exec (function(err,movies) {res.render (' index ', {title:' Imooc home ', movies:movies} )})
Delete a single piece of data, call the model's Remove method directly, pass in a specific key and value to
Documents- Database single Delete var Movie = require ('./models/movie '); App.get ('/',function (req,res) {Movie. Remove ({_id:id},function(err,movie) { if(err) {Co Nsole.log (ERR); })})
Then we need to adjust the patterns and models, the hierarchy of their directories
Nodemongodb node_modules bower_components View Index.jade detail.jade admin.jade List.jade models Movie . JS Schemas movie.js app.js
Finally, we will implement the database additions and deletions, as well as the development of the backend logic
MongoDB pattern model design and coding-mongoose