Mongoose: The database's name is changed??!!!

Created on 9 Oct 2014  路  4Comments  路  Source: Automattic/mongoose

var Person = require("../model").Person;
var mongoose = require("mongoose");

mongoose.connect("mongodb://localhost/person");
var person = mongoose.model("Person", Person);

var p = new person({name: "inmove", age: 23});

p.save(function(err, result) {
if(err)
console.log(err);
else
console.log(result);

});

with this code , i think i should get a collection called .But, i got a collection called people?!!Could you plese tell me how did this happen?

use person
switched to db person
show tables
people
system.indexes

Most helpful comment

Fair warning, this feature is deprecated, precisely because its surprising and confusing. In my early days working with mongoose back in 2012 I once spent 3 hours at a 24 hour hackathon trying to figure out why my data wasn't going into the right collection.

All 4 comments

All model names are run through util.toCollectionName to get a collection name. By default:

mongoose.model("poll",   poll);   // polls
mongoose.model("person", person); // people (exception)
mongoose.model("sheep",  sheep);  // sheep  (uncountable word)

You can disable this:

// Disable the option:
var person = Schema({}, {pluralize: false});
mongoose.model("person", person);

// Hard code the collection name (1):
var person = Schema({}, {collection: "person"});
mongoose.model("person", person);

// Hard code the collection name (2):
var person = Schema({});
mongoose.model("person", person, "person");

I personally prefer it to be enabled.

Uh,thanks.

Fair warning, this feature is deprecated, precisely because its surprising and confusing. In my early days working with mongoose back in 2012 I once spent 3 hours at a 24 hour hackathon trying to figure out why my data wasn't going into the right collection.

I stumble upon this surprising behavior in 2020. I am confused

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmmalam picture dmmalam  路  93Comments

aartiles picture aartiles  路  47Comments

ChrisZieba picture ChrisZieba  路  76Comments

cosminn777 picture cosminn777  路  158Comments

bendytree picture bendytree  路  53Comments