var mongoose = require("mongoose");
// 连接字符串格式为mongodb://主机/数据库名
mongoose.connect('mongodb://localhost/test');
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 movie = new Movie({
title: '黑衣人三',
doctor: '史密斯',
year: 2018,
flash: 'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
country: '美国',
language: '英语',
summary: '好片'
})
//保存数据库
movie.save(function(err) {
if (err) {
console.log('保存失败')
return;
}
console.log('meow');
});
控制台出现下面的提示:
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/pr...
也没报错,但是数据就是插不进去
1) Add default port 27017 to the host address. That is, mongodb://localhost/test
is changed to mongodb://localhost:27017/test
2) Use code to test the connection to ensure connectivity. For example, add these sentences after mongoose.connect():
3) Check which collection the data is stored in. The pitfall I encountered before was that if I use your phrase
is written in a way that although you specify to save a collection named 'Movie', the collection actually saved may be a collection named 'Movies' (Mongoose automatically adds an s at the end) ). It sucks, but it does happen.
You can easily check it out by using commands such as show collections in Mongodb SHELL, or using visualization tools such as RoboMongo and MongoBooster.
Add
mongoose.Promise = global.Promise;
before
mongoose.connect('mongodb://localhost/test');
Data cannot be retrieved or saved
You should save less on the poster item
The prompt is telling you that mongoose no longer implements promises built-in and you need to add a third-party promise plug-in yourself
The document.save you use may be a method that returns a promise
You can try changing it to Module.create