var mongoose = require("mongoose");
//The connection string format is mongodb:// host/database name
mongoose.connect('mongodb://localhost/test');
var Schema = mongoose.Schema;
//skeleton template
var movieSchema = new Schema({
doctor : String,
title : String,
language : String,
country : String,
year : Number,
summary : String,
poster : String,
flash : String
})
//model
var Movie = mongoose.model('Movie', movieSchema);
//store data
var movie = new Movie({
Title:' Men in Black III',
Doctor:' Smith',
year: 2018,
flash: 'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
Country:' US',
Language:' English',
Summary:' good movie'
})
//Save Database
movie.save(function(err) {
if (err) {
Log ('save failed')
return;
bracket
console.log('meow');
});
The following prompt appears on the console:
Mongoose: mpromise (mongoose’s default promise library) is deprecated, plug in your own promise library instead:http://mongoosejs.com/docs/pr …
I didn’t say anything wrong, but the data just couldn’t get in.
1) Add the default port 27017 to the host address. That is,mongodb://localhost/test
Change tomongodb://localhost:27017/test2) Test the connection with codes to ensure connectivity. For example, add these words after mongoose.connect ():
mongoose.connection.on('connected', function(){ console.log('Connection success!' ); }); mongoose.connection.on('error', function(err){ Log ('connectionerror:' plus err); }); mongoose.connection.on('disconnected', function(){ console.log('Connection disconnected'); });
3) Check which collection inside the data are stored in. The pit I have encountered before is, if you use this sentence
var Movie = mongoose.model('Movie', movieSchema);
Although you specify to deposit a collection called’ Movie’, what you may actually deposit is called’ Movie’sMongoose automatically adds an s after it. It’s a pit, but it does happen.
Commands such as show collections can be used in Mongodb SHELL inside, or visual tools such as RoboMongo, MongoBooster can be easily checked out.