Raja
Raja

Reputation: 3627

What are Mongoose (Nodejs) pluralization rules?

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();

});

Upvotes: 17

Views: 9878

Answers (3)

Orelsanpls
Orelsanpls

Reputation: 23575

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');

Upvotes: 2

Amol M Kulkarni
Amol M Kulkarni

Reputation: 21649

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

Upvotes: 10

Andz
Andz

Reputation: 2258

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.

Upvotes: 20

Related Questions