nodejs使用mongoose
var mongoose = require("mongoose"); // 连接字符串格式为mongodb://主机/数据库名 mongoose.connect('mongodb://cloud:pass9cuo@t.10000bee.com:27017/cloud'); var Schema = mongoose.Schema; //骨架模版 var movieSchema = new Schema({ doctor : String, title : String, language : String, country : String, year : Number, summary : String, poster : String, flash : String }) //模型 var Movie = mongoose.model('Movie', movieSchema); //存储数据 var moive = new Movie({ title: '黑衣人三', doctor: '史密斯', year: 2018 }) exports.mongotestsave = function (req, res) { moive.save(function(err) { if (err) { console.log('保存失败') return; } console.log('保存成功'); res.send('保存成功'); }); } exports.mongotestfind = function (req, res) {
//注意Movie要大写 Movie.find({year:2018}, function(err, doc){ if (err) { console.log('保存失败') return; } res.send(doc); }); }