Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data,
provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
Download the appropriate MongoDB version for your Operating System from the MongoDB Website and follow their installation instructions
Create a file ./src/database.js
under the project root.
let mongoose = require('mongoose');
const server = '127.0.0.1:27017'; // REPLACE WITH YOUR DB SERVER
const database = 'fcc-Mail'; // REPLACE WITH YOUR DB NAME
class Database {
constructor() {
this._connect()
}
_connect() {
mongoose.connect(`mongodb://${server}/${database}`)
.then(() => {
console.log('Database connection successful')
})
.catch(err => {
console.error('Database connection error')
})
}
}
module.exports = new Database()
The require(‘mongoose’) call above returns a Singleton object. It means that the first time you call require(‘mongoose’), it is creating an instance of the Mongoose class and returning it. On subsequent calls, it will return the same instance that was created and returned to you the first time because of how module import/export works in ES6.
Create email Model:
let mongoose = require('mongoose')
let validator = require('validator')
let emailSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
lowercase: true,
validate: (value) => {
return validator.isEmail(value)
}
}
})
module.exports = mongoose.model('Email', emailSchema)
Let’s create an instance of the email model and save it to the database:
let EmailModel = require('./email')
let msg = new EmailModel({
email: 'divyanshrai27@gmail.com'
})
msg.save()
.then(doc => {
console.log(doc)
})
.catch(err => {
console.error(err)
})
The result is a document that is returned upon a successful save:
{
_id: has03948ytoeirlu98345dsf,
email: 'divyanshrai27@gmail.com',
__v: 0
}
Fetch record:
Let’s try to retrieve the record we saved to the database earlier. The model class exposes several static and instance methods to perform operations on the database. We will now try to find the record that we created previously using the find method and pass the email as the search term.
EmailModel
.find({
email: 'divyanshrai27@gmail.com' // search query
})
.then(doc => {
console.log(doc)
})
.catch(err => {
console.error(err)
})
The document returned will be similar to what was displayed when we created the record:
{
_id: 5a78fe3e2f44ba8f85a2409a,
email: 'divyanshrai27@gmail.com',
__v: 0
}
Hello Everyone, In recent years, Docker has revolutionized the way we develop, deploy, and manage software applications. With its lightweight and portable containerization technology, it enables developers to package their applications along with all their dependencies into a single unit, making it easier to run across different environments. This post will provide an overview of the concepts and components of Docker, helping you grasp the fundamentals of this powerful tool.
What are Webhooks? The ability of independent online systems to communicate with one another and share data is the core of what makes online services valuable today. In this post, will look at webhooks.