Working with MongoDB : Using Mongoose
Boilerplate to connect to MongoDB in Node.js using Mongoose ORM.
- Core image from Pixabay
Below are common model schemas used in projects.
Connect DB function
const config = require('config');
const db = config.get('mongoURI');
// const db = process.env.MONGO_URI
const connectDB = async () => {
try {
const conn = await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log(`MongoDB Connected at ${conn.connection.host}`);
} catch (err) {
console.error(err.message);
// Exit process with failure
process.exit(1);
}
};
module.exports = connectDB;
Data Schema Models
User Schema
const mongoose = require('mongoose');
const crypto = require('crypto');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('config')
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please add a name']
},
email: {
type: String,
required: [true, 'Please add an email'],
unique: true,
match: [
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
'Please add a valid email'
]
},
password: {
type: String,
required: [true, 'Please add a password'],
minlength: 6,
select: false
},
// Used for users separated by type
role: {
type: String,
enum: ['user', 'publisher'],
default: 'user'
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
// Encrypt password using bcrypt
UserSchema.pre('save', async function (next) {
if (!this.isModified('password')) {
next();
}
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
// Sign JWT and return
UserSchema.methods.getSignedJwtToken = function () {
return jwt.sign({ id: this._id }, config.get('jwtSecret'), {
expiresIn: '5 days'
})
}
// Match user entered password to hashed password in database
UserSchema.methods.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
module.exports = mongoose.model('User', UserSchema);
Post Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId
},
text: {
type: String,
required: true
},
name: {
type: String
},
avatar: {
type: String
},
likes: [
{
user: {
type: Schema.Types.ObjectId
}
}
],
comments: [
{
user: {
type: Schema.Types.ObjectId
},
text: {
type: String,
required: true
},
name: {
type: String
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
}
],
date: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('post', PostSchema);