Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

});

like image 973
Raja Avatar asked Aug 29 '11 13:08

Raja


People also ask

Why we use $set in Mongoose?

In the doc, it mentioned that: By default, if you don't include any update operators in doc, Mongoose will wrap doc in $set for you. This prevents you from accidentally overwriting the document.

What does Mongoose do in node JS?

Mongoose is a Node. js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as SQLAlchemy for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.

What does findByIdAndDelete return?

The findByIdAndDelete() is a function in Mongoose used to find a document by the _id field and then remove the document from the collection. It returns the document removed or deleted.


2 Answers

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.

like image 160
Andz Avatar answered Oct 10 '22 02:10

Andz


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

like image 29
Amol M Kulkarni Avatar answered Oct 10 '22 01:10

Amol M Kulkarni