<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md. Rezaul Islam Rana</title>
    <description>The latest articles on DEV Community by Md. Rezaul Islam Rana (@rexaul-rana).</description>
    <link>https://dev.to/rexaul-rana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1181543%2Fb5ec6d89-e8bd-42cc-bda4-1374d805ba51.jpeg</url>
      <title>DEV Community: Md. Rezaul Islam Rana</title>
      <link>https://dev.to/rexaul-rana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rexaul-rana"/>
    <language>en</language>
    <item>
      <title>Why Mongoose ?</title>
      <dc:creator>Md. Rezaul Islam Rana</dc:creator>
      <pubDate>Fri, 02 Feb 2024 17:35:10 +0000</pubDate>
      <link>https://dev.to/rexaul-rana/why-mongoose--1aba</link>
      <guid>https://dev.to/rexaul-rana/why-mongoose--1aba</guid>
      <description>&lt;p&gt;Mongoose is a popular ODM(Object Data Modeling) library for MongoDB with Node JS. ODM is a programming concept that means defining a schema for data in a way that aligns with the structure of the objects in application. Mongoose provides a higher level schema clearance over MongoDB and makes it easier for developers to work with mongoDB using JavaScript or Typescript.&lt;br&gt;
Here are some reasons why we use mongoose:&lt;br&gt;
&lt;strong&gt;Schema definition:&lt;/strong&gt; Everything in Mongoose starts with a schema. Schema means a blueprint that defines the structure of documents within a collection, including the fields, types, validation rules, and default values. This helps maintain consistency in data structure, making it easier to validate.&lt;/p&gt;

&lt;p&gt;import mongoose from 'mongoose';&lt;br&gt;
const { Schema } = mongoose;&lt;/p&gt;

&lt;p&gt;const blogSchema = new Schema({&lt;br&gt;
  title: String, // String is shorthand for {type: String}&lt;br&gt;
  author: String,&lt;br&gt;
  body: String,&lt;br&gt;
  comments: [{ body: String, date: Date }],&lt;br&gt;
  date: { type: Date, default: Date.now },&lt;br&gt;
  hidden: Boolean,&lt;br&gt;
  meta: {&lt;br&gt;
    votes: Number,&lt;br&gt;
    favs: Number&lt;br&gt;
  }&lt;br&gt;
});&lt;br&gt;
To use schema we have to convert it into a model.&lt;br&gt;
mongoose.model(modelName, schema)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Middleware support:&lt;/strong&gt; Middleware refers to functions that are executed at the specific points in the  lifecycle of documents.Mongoose provides some built-in hooks such as “pre” “post” which allow it to perform certain operations like saving ,updating and removing documents.&lt;br&gt;
Usage of “Pre” hooks—&lt;br&gt;
const schema = new Schema({ /* ... */ });&lt;br&gt;
schema.pre('save', function(next) {&lt;br&gt;
  // do stuff&lt;br&gt;
  next();&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;Usage of “post” hooks—&lt;br&gt;
schema.post('init', function(doc) {&lt;br&gt;
  console.log('%s has been initialized from the db', doc._id);&lt;br&gt;
});&lt;br&gt;
schema.post('validate', function(doc) {&lt;br&gt;
  console.log('%s has been validated (but not saved yet)', doc._id);&lt;br&gt;
});&lt;br&gt;
schema.post('save', function(doc) {&lt;br&gt;
  console.log('%s has been saved', doc._id);&lt;br&gt;
});&lt;br&gt;
schema.post('deleteOne', function(doc) {&lt;br&gt;
  console.log('%s has been deleted', doc._id);&lt;br&gt;
});&lt;/p&gt;

</description>
      <category>mongoose</category>
      <category>backend</category>
      <category>node</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
