nodejs mongoose示例

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

nodejs mongoose示例_第1张图片


同时查看数据库内数据:

nodejs mongoose示例_第2张图片

可以看到效果是新建了一个叫cats的表(文档)并插入了一条数据

查询数据

修改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('/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;


访问http://localhost:3000/find

nodejs mongoose示例_第3张图片

修改数据

修改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('/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;

访问:
nodejs mongoose示例_第4张图片
数据库:
nodejs mongoose示例_第5张图片

删除数据

修改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('/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;
访问:
nodejs mongoose示例_第6张图片
实际中频繁读写的话连接一次数据库就可以了,因为mongoose是长连接

你可能感兴趣的:(Node.js,杂货,MongoDB,Node.js,mongoose)