What are Mongoose (Nodejs) pluralization rules?

前端 未结 3 1462
北恋
北恋 2020-12-18 20:12

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

相关标签:
3条回答
  • 2020-12-18 20:34

    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

    0 讨论(0)
  • 2020-12-18 20:39

    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.

    0 讨论(0)
  • 2020-12-18 20:45

    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');
    
    0 讨论(0)
提交回复
热议问题