Madhumitha
Madhumitha

Reputation: 338

How to change collection name in mongoose.model?

Am new in Nodejs, Am trying to create user login system with mongoDB and passportjs. Here i want to change collection name.

i refer this video here

This is my code:

Database connection js

config/database.js

module.exports = {
    database : 'mongodb://localhost:27017/my_first_project',
    secret : 'Mysecret'
}

models/admin.js

const mongoose = require('mongoose');

let AdminSchema = mongoose.Schema({
    name:{
        type: String,
        required: true
    },
    email:{
        type: String,
        required: true
    },
    username:{
        type: String,
        required: true
    },
    password:{
        type: String,
        required: true
    },
    created_on:{
        type: Date,
        default: Date.now 
    },
    status:{
        type: Number,
        required: true,
        validate : {
            validator : Number.isInteger,
            message   : '{VALUE} is not an integer value'
        }
    }
});

const Admin = module.exports = mongoose.model('User', AdminSchema);

config/passport.js

const LocalStorage = require('passport-local').Strategy;
const User = require('../models/admin');
const config = require('../config/database');
const bcrypt = require('bcryptjs');

module.exports = function(passport){
    //Local Strategy
    passport.use(new LocalStorage(function(username, password, done){
        //Check username
        let query = {username:username};
        console.log(query);
        User.findOne(query, function(err, user){
            console.log(user);
            if(err) throw err;
            if(!user)
            {
                return done(null, false, {message: 'No user found'});
            }
            //check password
            bcrypt.compare(password, user.password, function(err, isMatch){
                if(err) throw err;
                if(isMatch){
                    return done(null, user);
                }else{
                    return done(null, false, {message: 'Wrong password'});
                }
            });
        });
    }));

    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });
      
    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user);
        });
    });
}

This is my database structure enter image description here

When i user correct username and password for login passportjs shows null.

Need to change collection name as admin.

I don't know how to ask this question. If am asking wrong sorry for all.

Upvotes: 5

Views: 7161

Answers (1)

Ashok
Ashok

Reputation: 2932

You can use this structure. API structure of mongoose.model is this:

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

which means

 mongoose.model('User', AdminSchema, 'admin'); 
 //Here 3rd argument 'admin': as collection name

You can do with this too

let AdminSchema = mongoose.Schema({
  username : String,
  password : String.
  ....... 
}, { collection: 'admin' });

See this link from the Mongoose documentation.

Upvotes: 9

Related Questions