MongoDB Download/install
Mongoose NPM Install--save Mongoose
Mongoose Database connection
Const MONGOOSE = require (' Mongoose ')
Const Db_rul = ' MONGODB://LOCALHOST:27017/KOA2 '
Mongoose.connect (Db_rul, function (err) {if (err) {Console.warn (' Database connection failed: ' +err)}else{console.log (' database successfully connected to: ' +db_rul )}}) Module.exports = Mongoose Koa-router route NPM install--save koa-router Const router = require (' koa-router ') const body Parser = require (' Koa-bodyparser ')
Const MONGOOSE = require ('.. /config ')
Const ROUTER = new Router ()
http://cnodejs.org/topic/548e54d157fd3ae46b233502
Define Create data table
Create a schemaconst Createschema = Mongoose. Schema ({name: {type:string, required:true},age: {type:number, required:true}}) Const User = Mongoose.model (' users ', C Reateschema) Data Simple lookup User.find ({}, function (Err, doc) {Console.log (DOC)})
New User.create ({name: ' Afacode ', age:19 + Math.random ()}, function (Err, doc) {if (err) {Console.log (err)}else{ Console.log (DOC)}}) First this way
Mongoose Operation MongoDB Notes (own notes, self-view)