標籤:nodejs red 自己的 creat mongod nec strong router body
mongodb下載/安裝
mongoose npm install --save mongoose
mongoose 資料庫連接
const mongoose = require(‘mongoose‘)
const DB_RUL = ‘mongodb://localhost:27017/koa2‘
mongoose.connect(DB_RUL, function(err){if(err){console.warn(‘資料庫連接失敗:‘+err)}else{console.log(‘資料庫成功串連到:‘+DB_RUL)}})module.exports = mongoose koa-router 路由 npm install --save koa-router const Router = require(‘koa-router‘)const bodyParser = require(‘koa-bodyparser‘)
const mongoose = require(‘../config‘)
const router = new Router()
http://cnodejs.org/topic/548e54d157fd3ae46b233502
定義建立資料表
//建立一個Schemaconst createSchema = mongoose.Schema({name: {type: String, required: true },age: {type: Number, required: true }})const User = mongoose.model(‘users‘, createSchema)資料的簡單尋找User.find({}, function(err, doc){ console.log(doc)})
建立User.create({name : ‘afacode‘,age: 19 + Math.random()}, function(err, doc){if(err){console.log(err)}else{console.log(doc) }})先這樣
mongoose 操作 mongodb 筆記 (自己的筆記,自己看的)