1.引入模块
var mongoose = require('mongoose ');.
2.连接数据库
mongoose .connect('mongodb://localhost/ittest');
3.设计文档结构Schema(保证数据的完整性)
var DataSchema = new mongoose.Schema({
name:{
type:String,
require:true
}
ip:{type:String},
address:{type:String},
time:{type:Date,default:Date.now}
});
4.将文档结构转化为模型
var usersModel = mongoose.model('UserInfo',DataSchema );
5.简单操作
(1)添加
var user = new usersModel({
name:...
,,,
});
user .save(function(err,ret){
if(err){
console.log(err)
}{
console.log(ret)
}
})
(2)查询数据
var cond= {
name:dd
}
user.find(cond, function(err,ret){
if(err) {
console.log(err)
} {
console.log(ret)
}
})