Mongoose — Force collection name



  • 3 Answer(s)

    You can try to do it.

    var UserInfo = new Schema({
    username : String,
    password : String
    }, { collection: 'userinfo' });
    

    To refer this link for higher information.

    Answered on January 8, 2019.
    Add Comment

    Pass the collectionName as the third argument when you are using mongoose 2.0.0

    mongoose.model('UserInfo', UserInfo, 'UserInfo');
    
    

     

    Answered on January 8, 2019.
    Add Comment

    The following statement are mongoose will add ‘s’ to collection name by default. If you want to avoid that just pass third argument as name of collection

    var mongoose = require('mongoose');
            var db = mongoose.connect('mongodb://localhost/testdb');
            var Schema = mongoose.Schema;
            var UserInfo = new Schema({
            username : String,
            password : String
            });
          mongoose.model('UserInfo', UserInfo, 'UserInfo')
        tan = new user();
            admin.username = "sss";
            admin.password = "ee";
            admin.save();
    
    Answered on January 8, 2019.
    Add Comment


  • Your Answer

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