1.创建Schema
var schema = new mongoose.Schema({ userName:{type:String,require:true}, age:{type:Number,require:true}, hasCard:false })
2.创建model
var User = mongoose.model("user",schema);
3.在app.get()中调用查找方法find()
app.get("/data",function(req,res){ User.find({},function(err,doc){ if(err){ console.log(err); }else{ res.json(doc); } }); });
查找指定数据:
app.get("/data",function(req,res){ User.find({"userName":"zs"},function(err,doc){ if(err){ console.log(err); }else{ res.json(doc); } }); });