MongoDB pattern model design and coding-mongoose

Source: Internet
Author: User
Tags findone mongodb

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.