Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

node.js - 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 
");
});

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

});

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...