MongoDB是一種文件導向数据库管理系统,由C++撰寫而成,以此来解决应用程序开发社区中的大量现实问题。
安装mongoose:npm install -g mongoose
[email protected] C:\Users\ucloud01\AppData\Roaming\npm\node_modules\mongoose
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected])
express:4.10.6
mongodb:2.6.6
nodejs:0.10.32
修改routes/index.js:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/connect', function(req, res) {
mongoose.connect("mongodb://localhost/test", function(err) {
if(err) {res.send(err.message);}
res.send("connect success");
});
});
module.exports = router;
运行并访问http://localhost:3000/connect可以看到
connect success的输出
修改routes/index.js:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Cat = mongoose.model('Cat', {
id: Number,
name: String
});
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/connect', function(req, res) {
mongoose.connect("mongodb://localhost/test", function(err) {
if(err) {res.send(err.message);}
res.send("connect success");
});
});
router.get('/insert', function(req, res) {
var kitty = new Cat({
id: 01,
name: "shawn"
});
kitty.save(function(err, data, numberAffected) {
if (err) {res.send(err)}
var html = ' new data is: ' + JSON.stringify(data) + '
';
html += 'num of data effected is: ' + numberAffected + '
';
res.send(html);
});
});
module.exports = router;
访问http://localhost:3000/insert
同时查看数据库内数据:
可以看到效果是新建了一个叫cats的表(文档)并插入了一条数据
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Cat = mongoose.model('Cat', {
id: Number,
name: String
});
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/connect', function(req, res) {
mongoose.connect("mongodb://localhost/test", function(err) {
if(err) {res.send(err.message);}
res.send("connect success");
});
});
router.get('/save', function(req, res) {
var kitty = new Cat({
id: 01,
name: "shawn"
});
kitty.save(function(err, data, numberAffected) {
if (err) {res.send(err)}
var html = ' new data is: ' + JSON.stringify(data) + '
';
html += 'num of data effected is: ' + numberAffected + '
';
res.send(html);
});
});
router.get('/find', function(req, res) {
Cat.find({id: 1},function(err, docs) {
if (err) {res.send(err)}
var html = 'data found: ' + JSON.stringify(docs);
res.send(html);
});
});
module.exports = router;
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Cat = mongoose.model('Cat', {
id: Number,
name: String
});
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/connect', function(req, res) {
mongoose.connect("mongodb://localhost/test", function(err) {
if(err) {res.send(err.message);}
res.send("connect success");
});
});
router.get('/save', function(req, res) {
var kitty = new Cat({
id: 01,
name: "shawn"
});
kitty.save(function(err, data, numberAffected) {
if (err) {res.send(err)}
var html = ' new data is: ' + JSON.stringify(data) + '
';
html += 'num of data effected is: ' + numberAffected + '
';
res.send(html);
});
});
router.get('/find', function(req, res) {
Cat.find({id: 1},function(err, docs) {
if (err) {res.send(err)}
var html = 'data found: ' + JSON.stringify(docs);
res.send(html);
});
});
router.get('/update', function(req, res) {
mongoose.connect("mongodb://localhost/test");
Cat.update({id: 1}, {$set:{name: "shawn2"}}, {upsert: true}, function(err, numberEffected, raw) {
if(err) {res.send(err);}
var html = '
data after updated: ' + JSON.stringify(raw) + '
';
console.log("get /update2");
html = html + 'num of data effected :' + numberEffected +'
';
res.send(html);
});
});
module.exports = router;
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Cat = mongoose.model('Cat', {
id: Number,
name: String
});
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/connect', function(req, res) {
mongoose.connect("mongodb://localhost/test", function(err) {
if(err) {res.send(err.message);}
res.send("connect success");
});
});
router.get('/save', function(req, res) {
var kitty = new Cat({
id: 01,
name: "shawn"
});
kitty.save(function(err, data, numberAffected) {
if (err) {res.send(err)}
var html = ' new data is: ' + JSON.stringify(data) + '
';
html += 'num of data effected is: ' + numberAffected + '
';
res.send(html);
});
});
router.get('/find', function(req, res) {
Cat.find({id: 1},function(err, docs) {
if (err) {res.send(err)}
var html = 'data found: ' + JSON.stringify(docs);
res.send(html);
});
});
router.get('/update', function(req, res) {
mongoose.connect("mongodb://localhost/test");
Cat.update({id: 1}, {$set:{name: "shawn2"}}, {upsert: true}, function(err, numberEffected, raw) {
if(err) {res.send(err);}
var html = '
data after updated: ' + JSON.stringify(raw) + '
';
console.log("get /update2");
html = html + 'num of data effected :' + numberEffected +'
';
res.send(html);
});
});
router.get('/delete', function(req, res) {
mongoose.connect("mongodb://localhost/test");
Cat.remove ({
id: 1
}, function(err) {
if(err) {res.send(err);}
res.send("delete suncces");
});
});
module.exports = router;
访问: