MongoDB Model

2

Hello! I'm starting to use MongoDB with Mongoose in NodeJS and I'm encountering a strange behavior when creating mesus Models in MongoDB. Example:

mongoose.connect('mongodb://localhost/db_teste');

const PersonSchema = new Schema({
    name: {
        first: String,
        last: String
    }
});

const Person = mongoose.model('Person', PersonSchema);

This code should create the Model "Person", but when I run a "show collections" in Mongo, via prompt, in my DB "db_teste" appears "People" instead of "Person". I also created a "test" Model and appeared "tests" (with an "s" more).

What's going on?

    
asked by anonymous 07.05.2017 / 00:46

1 answer

2

This is a default Mongoose behavior that defines the name of collection as being the plural of the name of model , so person turned people and teste turned testes .

You can override this behavior by manually setting the value of collection :

const PersonSchema = new Schema({
    name: {
        first: String,
        last: String
    }
}, { collection: 'person' });

Notice in the last line, where the name of collection was defined.

    
07.05.2017 / 01:27