17

I am a newbie to the Node.js, Mongoose and Expressjs. I have tried to create a table "feedbackdata" using the Mongoose in MongoDB via the following code. But it is created as "feedbackdata*s*". By Googling, I found that the Mongoose uses pluralization rules. Anyone please help me to remove the pluralization rules? or how my code should be for the "feedbackdata" table?

Below is my code:

app.post("/save",function(req,res){

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

mongoose.connection.on("open", function(){
    console.log("mongo connected \n");
});

// defining schemar variables
Schema = mongoose.Schema,   
ObjectId = Schema.ObjectId;

// define schema for the feedbackdata table
var feedback_schema = new Schema({
    _id: String,
    url:String,
    username:String,
    email:String,
    subscribe:String,
    types:String,
    created_date: { type: Date, default: Date.now },
    comments: String
});

// accessing feeback model object
var feedback_table = mongoose.model('feedbackdata', feedback_schema);
var tableObj = new feedback_table();

var URL = req.param('url');
var name = req.param('name');
var email = req.param('email');
var subscribe = req.param('subscribe');
var choices = req.param('choices');
var html = req.param('html');
var receipt = req.param('receipt');    
var feedbackcontent = req.param('feedbackcontent');

tableObj._id = 3;
tableObj.url = URL;
tableObj.username = name;
tableObj.email = email;
tableObj.subscribe = subscribe;
tableObj.types = choices;
tableObj.comments = feedbackcontent;

tableObj.save(function (err){
    if(err) { throw err; }else{ 
        console.log("Saved!");              
    }
    mongoose.disconnect();
})

res.write("<div style='text-align:center;color:green;font-weight:bold;'>The above values saved successfully! <br><a href='/start'>Go back to feedback form</a></div>");     

res.end();

});

evandrix
  • 6,041
  • 4
  • 27
  • 38
Raja
  • 3,477
  • 12
  • 47
  • 89
  • Also, you should remove _id: String, from var feedback_schema = new Schema({ if you want id to be auto generated – vytaute Mar 19 '21 at 06:19

3 Answers3

20

The pluralization rules are in this file: https://github.com/LearnBoost/mongoose/blob/master/lib/utils.js

You can add your schema name to the 'uncountables' list, then mongoose will not pluralize your schema name.

Andz
  • 2,228
  • 19
  • 13
  • 1
    Thanks. Where is **"uncountables"** list? I can't find it. Maybe the source of the link has been updated. – Mir-Ismaili Feb 20 '20 at 22:25
  • It looks like the pluralization was abstracted into its own package named `mongoose-legacy-pluralize`. [New location of the uncountables list](https://github.com/vkarpov15/mongoose-legacy-pluralize/blob/master/index.js) as of June 2020. – Ben Zenker Jun 17 '20 at 20:50
10

Provide the name for the collection in options while creating schema object, then Mongoose will not do pluralize your schema name.

e.g.

var schemaObj = new mongoose.Schema(
{
 fields:Schema.Type
}, { collection: 'collection_name'});

For more Info: http://mongoosejs.com/docs/guide.html#collection

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
2

The pluralization rules is here to ensure a specific naming convention.

The collection name should be plural, all lowercase and without spacing.

What are naming conventions for MongoDB?


I think you should ask yourself if you want to follow the main rule (ensured by mongoose as a default behavior) or get rid of it.

What are the perks ? What are the good points ?

You design first what is an user (User model) and then you store users into a collection. It totally make sense.

Your call.


If you ask yourself how to get the final name of the collection after the pluralization :

const newName = mongoose.pluralize()('User');
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69