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
use person
switched to db person
show tables
people
system.indexes
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
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.