I am learning Node.js using Beginning Node.js written by Mr. Basarat Syed.
I have come across the following mongoose code snippet.
My question is about the last three lines with db.close() and how does the collection tanks come up without any statement about tanks.
var mongoose = require('mongoose');
var tankSchema = new mongoose.Schema({name: 'string', size: 'string'});
tankSchema.methods.print = function () {
console.log('I am', this.name, 'the', this.size)
};
var Tank = mongoose.model('Tank', tankSchema);
mongoose.connect('mongodb://127.0.0.1:27017/demo');
var db = mongoose.connection;
db.once('open', function callback() {
console.log('connected!');
var tony = new Tank({
name: 'tony',
size: 'small'
});
tony.print();
tony.save(function (err) {
if (err) throw err;
Tank.findOne({name: 'tony'}).exec(function (err, tank) {
tank.print();
//======Here are the three lines=======
db.collection('tanks').drop(function(){db.close();});// The one in the book.
//db.close();
//db.collection('t').drop(function(){db.close();});
});
});
});
My question is what is the difference among the last three lines?
Where did the collection('tanks') come from?
Why not just use db.close() like the second one?
It seems that I could use any legal string as the argument of collection on for example the third sentence 't'. How could this happen?