Why does mongoose always add an s to the end of my collection name

Why does mongoose always add an s to the end of my collection name

Asked on January 8, 2019 in MongoDB.
Add Comment


  • 3 Answer(s)

    You can try to do it. its trying to be a smart:

    var dataSchema = new Schema({..}, { collection: 'data' })
    
    
    Answered on January 8, 2019.
    Add Comment

    This is a API structure of mongoose.model:

    Mongoose#model(name, [schema], [collection], [skipInit])
    

    The following statement is What mongoose do is that, When no collection argument is passed, The Mongoose produces a collection name by pluralizing the model name. If you don’t like this behavior, either pass a collection name or set your schemas collection name option.

    For the example:

    var schema = new Schema({ name: String }, { collection: 'actor' });
    

    Or Else

    schema.set('collection', 'actor')
    

    Or Else

    var collectionName = 'actor'
    var M = mongoose.model('Actor', schema, collectionName);
    

     

    Answered on January 8, 2019.
    Add Comment

    We can add a string as a third argument to define the actual name for the collection. spanning your examples, to keep names as data and user respectively:

    var Dataset = mongoose.model('data', dataSchema, 'data');
    var User = mongoose.model('user', dataSchema, 'user');
    
    Answered on January 8, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.