3

I created a schema in mongoose and bound it to a model and specified the model's name as "user" . However, in the database it gets saved as "users". Can somebody expalin why?

//Schema for adding ne user
var newUser = new mongoose.Schema({
    "_id" : Number,
    "firstname":String,
    "lastname":String,
    "username":String,
    "password" : String
});
var user = mongoose.model("user" , newUser);
Paridhi Sharma
  • 121
  • 1
  • 5

1 Answers1

5

Mongoose is trying to be smart here. It consider the collection name to be a plural. like in your case it is converting "user" to "users"

You can however force it to be whatever you want:

var newUser = new mongoose.Schema({
    "_id" : Number,
    "firstname":String,
    "lastname":String,
    "username":String,
    "password" : String
}, { collection: 'user });
Vishu238
  • 673
  • 4
  • 17