How to change collection name in mongoose.model?

雨燕双飞 提交于 2020-01-22 03:57:08

问题


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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/54878935/how-to-change-collection-name-in-mongoose-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!