nodejs+mongoose 增删改查
2017-05-09 00:00
232 查看
var mongoose = require('mongoose'); var conn = mongoose.connect('mongodb://10.16.12.95:40000/test'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; //定义坏环url对象 var urlBadLink = new Schema({ id :{ type:String }, host : { type:String }, total : { type:Number }, badCounts : { type:Number }, getRate :{ type:String } }); mongoose.model('url', urlBadLink); var URL = mongoose.model("url"); //插入数据 function save(id,host,total,badCounts){ var url = new URL(); url.id = id; url.host = host; url.total = total; url.badCounts = badCounts; //获取坏环率 url.getRate = String(Math.round(url.badCounts/url.total*10000)/100)+'%'; url.save(); } //查询数据 function showUrls(id){ //URL.find({id:id}).sort({'_id':-1}).limit(5).exec(function(err,docs){ URL.find({id:id}).exec(function(err,docs){ console.log(docs) }) } //插入数据 save('004','127.0.0.1',20,1); //查询 showUrls('004'); //复杂数据结构举例 var ExampleSchema = new Schema({ name:String, binary:Buffer, living:Boolean, updated:Date, age:Number, mixed:Schema.Types.Mixed,//该混合类型等同于 nested _id:Schema.Types.ObjectId, //主键 _fk:Schema.Types.ObjectId, //外键 array:[], arrOfString:[String], arrOfNumber:[Number], arrOfDate:[Date], arrOfBuffer:[Buffer], arrOfBoolean:[Boolean], arrOfMixed:[Schema.Types.Mixed], arrOfObjectId:[Schema.Types.ObjectId] , nested:{ stuff:String } });
相关文章推荐
- NODEJS mongoose 增删改查
- nodejs+mongoose简单关联+增删改查
- node.js结合mongoose对表单进行数据简单的增删改查
- easyui+nodejs+sqlserver增删改查实现
- node.js下mongoose简单操作实例
- Node.JS, Mongoose和Jade搭建OAuth2服务器 (一)
- node.js开发之express4.0使用mongoose连接mongodb
- Node.js开发——添加mongoose模块引发的异常
- node.js操作mysql(增删改查)
- nodejs+express+ejs+mongoose实例
- Node.js使用Mongoose包操作MongoDB数据库
- node.js下mongoose简单操作实例
- nodejs+express+ejs+mongoose实例
- nodejs+express+ejs+mongoose实例
- [node.js]express+mongoose+mongodb的开发笔记
- node.js操作mongodb数据库(增删查改分页)
- nodeJs+express+ejs+mongoose实现翻页原理1
- node.js对mongodb的增删查改
- NodeJS+Express+mongoose+Jade+bootstrap+jquery+inspector(调试工具)开发网站demo总结
- MONGOOSE – 让NODE.JS高效操作MONGODB(转载)