var express=require(‘express’);
var mongoose=require(‘mongoose’);
var router=express.Router();
var Person=mongoose.model(‘Person’,{
id:Number,
name:String
});
router.get(‘/insert’, function(req, res){
var student=new Person({
id:1,
name:”huop”
});
mongoose.connect(“mongodb://localhost:27017/test”);
student.save(function(e, product, numberAffected) {
if (e) res.send(e.message);
var html=”
新增的数据为:” + JSON.stringify(product);
html +=”
影响的数据量为:” + numberAffected;
res.send(html);
});
});
router.get(‘/find’,function(request, response){
mongoose.connect(“mongodb://localhost:27017/test”);
Person.find({
id: 1
}, function(e, docs) {
if (e) response.send(e.message);
var html=”
查询到的数据为:” + JSON.stringify(docs);
response.send(html);
});
});