0

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?

2
  • 1
    I don't get it. Can you please frame your questions more appropriately? What exactly is your question. Commented Jan 11, 2016 at 11:15
  • @GandalftheWhite Sorry for my bad frame about the questions. I have made some edit and not sure if it is clear now. Although X.L.Ant has made the picture much clear now. :) Commented Jan 11, 2016 at 15:32

1 Answer 1

2

When you save an instance of the Tank model, mongoose simply infers the collection name to tanks (lowercase Tank + the s suffix), and creates the collection if needed (i.e. if it doesn't already exist), unless you explicitly tell it another name to use, like this:

var tankSchema = new mongoose.Schema({name: 'string', size: 'string'}, { collection: 'myTanks' });

The following statement drops the tanks collection you created by saving a tank using the Tank model, and then closes the connection

db.collection('tanks').drop(function(){db.close();});

This statement only closes the connection, without doing anything else

db.close();

This one drop the collection t (which I guess doesn't exist in your case), and then again closes the connection

db.collection('t').drop(function(){db.close();});
Sign up to request clarification or add additional context in comments.

2 Comments

So the collection tanks has already be created by mongoose implicitly according to the Model name - Tank. Thank you very much! Much clear now. :)
Exactly. The collection is created when you try to save the first object, if it doesn't already exists. Edited to clarify this point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.