<?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: Dhiman_aman</title>
    <description>The latest articles on DEV Community by Dhiman_aman (@raynecoder).</description>
    <link>https://dev.to/raynecoder</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%2F935516%2F27065d43-db37-4251-a413-a7faeca4a630.png</url>
      <title>DEV Community: Dhiman_aman</title>
      <link>https://dev.to/raynecoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raynecoder"/>
    <language>en</language>
    <item>
      <title>API - Creating a Api in NodeJS with Prisma ORM &amp; MongoDB</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 07 Sep 2024 05:59:51 +0000</pubDate>
      <link>https://dev.to/raynecoder/api-creating-a-api-in-nodejs-with-prisma-orm-mongodb-1hc5</link>
      <guid>https://dev.to/raynecoder/api-creating-a-api-in-nodejs-with-prisma-orm-mongodb-1hc5</guid>
      <description>&lt;p&gt;To create an example API in Node.js with MongoDB using Prisma ORM, you'll first need to set up a Node.js project and configure Prisma to use MongoDB. Below is a step-by-step guide to create a basic API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Set Up Your Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initialize a Node.js Project&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;mkdir &lt;/span&gt;prisma-mongodb-example
   &lt;span class="nb"&gt;cd &lt;/span&gt;prisma-mongodb-example
   npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install Required Packages&lt;/strong&gt;:
Install &lt;code&gt;express&lt;/code&gt; for building the API, &lt;code&gt;prisma&lt;/code&gt; for ORM, and &lt;code&gt;@prisma/client&lt;/code&gt; for Prisma Client.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install &lt;/span&gt;express prisma @prisma/client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install Prisma CLI&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npx prisma init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Configure Prisma for MongoDB
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Configure &lt;code&gt;.env&lt;/code&gt; File&lt;/strong&gt;:
Update the &lt;code&gt;.env&lt;/code&gt; file generated by Prisma to include your MongoDB connection string. For example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   DATABASE_URL="mongodb://localhost:27017/mydatabase"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set Prisma Schema to Use MongoDB&lt;/strong&gt;:
Update the &lt;code&gt;prisma/schema.prisma&lt;/code&gt; file to use MongoDB as the provider and define a model. Here is an example schema:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   datasource db {
     provider = "mongodb"
     url      = env("DATABASE_URL")
   }

   generator client {
     provider = "prisma-client-js"
   }

   model User {
     id    String @id @default(auto()) @map("_id") @db.ObjectId
     name  String
     email String @unique
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Generate Prisma Client
&lt;/h3&gt;

&lt;p&gt;Run the following command to generate the Prisma Client based on the schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx prisma generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Create a Basic Express API
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create an &lt;code&gt;index.js&lt;/code&gt; File&lt;/strong&gt;:
This file will initialize the Express application and define the API routes.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

   &lt;span class="c1"&gt;// Create a new user&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
         &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="c1"&gt;// Get all users&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMany&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="c1"&gt;// Get a single user by ID&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUnique&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
         &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="c1"&gt;// Update a user by ID&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
         &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
         &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="c1"&gt;// Delete a user by ID&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
         &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User deleted successfully&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is running on http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Run the Application
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Run the Application&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Test the Endpoints&lt;/strong&gt;:
Use tools like Postman or cURL to test the CRUD operations for the &lt;code&gt;User&lt;/code&gt; model.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This example demonstrates how to create a basic CRUD API in Node.js with MongoDB using Prisma ORM. You can expand this example by adding more models, relationships, and validation logic as needed for your application.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>mongodb</category>
      <category>node</category>
      <category>prisma</category>
    </item>
    <item>
      <title>Migrate MongoDB to MySQL using Prism Key Differences</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 07 Sep 2024 05:56:41 +0000</pubDate>
      <link>https://dev.to/raynecoder/migrate-mongodb-to-mysql-using-prism-key-differences-2ma5</link>
      <guid>https://dev.to/raynecoder/migrate-mongodb-to-mysql-using-prism-key-differences-2ma5</guid>
      <description>&lt;p&gt;The &lt;code&gt;schema.prisma&lt;/code&gt; file defines the data models, relationships, and database connection details for Prisma. When using different databases like MongoDB and MySQL, the &lt;code&gt;schema.prisma&lt;/code&gt; file will have differences due to the unique characteristics and data types of each database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Differences Between &lt;code&gt;schema.prisma&lt;/code&gt; for MongoDB and MySQL
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Datasource Configuration&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;datasource&lt;/code&gt; block specifies the database provider (&lt;code&gt;mongodb&lt;/code&gt; or &lt;code&gt;mysql&lt;/code&gt;) and connection details. The provider will change depending on the database you are using.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MongoDB Configuration:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  datasource db {
    provider = "mongodb"
    url      = env("DATABASE_URL")
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MySQL Configuration:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  datasource db {
    provider = "mysql"
    url      = env("DATABASE_URL")
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. &lt;strong&gt;Data Model Differences&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;MongoDB and MySQL have different data types and ways of defining primary keys and relationships. Here are the primary differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Primary Key Definition:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;MongoDB&lt;/strong&gt;, the primary key is usually an &lt;code&gt;ObjectId&lt;/code&gt; and is mapped using the &lt;code&gt;@map("_id")&lt;/code&gt; directive.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;MySQL&lt;/strong&gt;, the primary key is typically an integer with auto-increment.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Data Type Differences:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MongoDB&lt;/strong&gt; supports data types like &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;DateTime&lt;/code&gt;, and custom types like &lt;code&gt;Json&lt;/code&gt; and &lt;code&gt;ObjectId&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MySQL&lt;/strong&gt; has a more structured type system, including &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;DateTime&lt;/code&gt;, &lt;code&gt;Decimal&lt;/code&gt;, and &lt;code&gt;String&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example of &lt;code&gt;schema.prisma&lt;/code&gt; for MongoDB:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  name  String
  email String @unique
  posts Post[]
}

model Post {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  title     String
  content   String
  published Boolean  @default(false)
  authorId  String   @db.ObjectId
  author    User?    @relation(fields: [authorId], references: [id])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Example of &lt;code&gt;schema.prisma&lt;/code&gt; for MySQL:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id    Int    @id @default(autoincrement()) // Integer auto-increment primary key
  name  String
  email String @unique
  posts Post[]
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String
  published Boolean  @default(false)
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary of Differences
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;MongoDB (&lt;code&gt;schema.prisma&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;MySQL (&lt;code&gt;schema.prisma&lt;/code&gt;)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Datasource Provider&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;provider = "mongodb"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;provider = "mysql"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Primary Key Definition&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;id String @id @default(auto()) @map("_id") @db.ObjectId&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;id Int @id @default(autoincrement())&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Data Types&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Supports &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;DateTime&lt;/code&gt;, etc.&lt;/td&gt;
&lt;td&gt;More structured, includes &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Foreign Key Relationship&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Foreign keys defined with &lt;code&gt;@db.ObjectId&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Foreign keys with &lt;code&gt;Int&lt;/code&gt; or other standard SQL types&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Autoincrement ID&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Not applicable (&lt;code&gt;@default(auto())&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@default(autoincrement())&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Unique Identifier&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;@unique&lt;/code&gt; for unique fields&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;@unique&lt;/code&gt; for unique fields&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The main differences in the &lt;code&gt;schema.prisma&lt;/code&gt; file between MongoDB and MySQL revolve around how primary keys are handled, the differences in data types, and the way relationships and fields are mapped to the database. When switching from MongoDB to MySQL, these differences must be adjusted to accommodate the underlying database system's constraints and data modeling.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to use the Zustand in ReactJS for State Management?</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 20 Jul 2024 14:36:58 +0000</pubDate>
      <link>https://dev.to/raynecoder/how-to-use-the-zustand-in-reactjs-for-state-management-1ed5</link>
      <guid>https://dev.to/raynecoder/how-to-use-the-zustand-in-reactjs-for-state-management-1ed5</guid>
      <description>&lt;p&gt;A small, fast, and scalable bearbones state management solution. Zustand has a comfy API based on hooks. It isn't boilerplatey or opinionated, but has enough convention to be explicit and flux-like.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Installation&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Install Zustand Library in your ReactJS Project.&lt;br&gt;
Zustand is available as a package on NPM for use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;1. Create a File store.js in src and paste below code
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import create from 'zustand';
import { devtools, persist } from 'zustand/middleware';

const useApp = ((set) =&amp;gt; ({
    count: 0,
    increment: () =&amp;gt; set((state) =&amp;gt; ({ count: state.count + 1 })),
    decrement: () =&amp;gt; set((state) =&amp;gt; ({ count: state.count - 1 })),
}));

const useAppStore = create(
    devtools(
        persist(useApp, {
            name: 'test'
        })
    )
)

export default useAppStore;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;2. Import the useAppStore in App.js File
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const { count, increment, decrement } = useAppStore();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;3. Create a two button for testing your increment and decrement
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;Count: {count}&amp;lt;/h1&amp;gt;
                &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
                &amp;lt;button onClick={decrement}&amp;gt;Decrement&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Done.&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Plese Vote and React on our posts.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Top SQL Server Interview Questions for 2024</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 20 Jul 2024 10:23:56 +0000</pubDate>
      <link>https://dev.to/raynecoder/top-sql-server-interview-questions-for-2024-4kg2</link>
      <guid>https://dev.to/raynecoder/top-sql-server-interview-questions-for-2024-4kg2</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. What is the Windows Authentication Mode in SQL Server?&lt;/strong&gt;&lt;br&gt;
This mode connects the server via a Windows account. The server uses the username and password for authentication. In this mode, SQL server authentication is disabled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Give an example of a function in an SQL server that returns the first non-null expression from more than one column in arguments.&lt;/strong&gt;&lt;br&gt;
Select COALESCE(sid, sname, marks) from the student;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Explain the one-to-many relationship in the SQL Server database.&lt;/strong&gt;&lt;br&gt;
When a single column value in one table has a minimum of one dependent column value in some other table, a one-to-many relationship exists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. What is the significance of CHECK in SQL Server?&lt;/strong&gt;&lt;br&gt;
CHECK constraint limits the values that can be placed inside a table’s column. This maintains integrity. The constraint is used column-wise to give specific values to that column. Example: CONSTRAINT CHK_Student CHECK (age&amp;lt;20) &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.How to find the 3rd highest marks from the Student table?&lt;/strong&gt;&lt;br&gt;
SELECT TOP 3 marks FROM (SELECT DISTINCT TOP 3 marks FROM student ORDER BY marks DESC) a ORDER BY marks&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. What is a trigger?&lt;/strong&gt;&lt;br&gt;
When a table event occurs, such as INSERT, DELETE, or UPDATE, triggers allow executing an SQL code batch. Triggers are managed by DBMS and can also execute stored procedures. For example, when a record is inserted in a database table, a trigger can be set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. When can records be deleted from a view in SQL Server?&lt;/strong&gt;&lt;br&gt;
Records can be deleted in a ‘simple’ view as it contains data from one table only.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. List down some of the features of MS SQL Server.&lt;/strong&gt;&lt;br&gt;
      It provides an easy and straightforward Syntax.&lt;br&gt;
      MS SQL uses transact SQL.&lt;br&gt;
      Query optimization is not supported.&lt;br&gt;
      The transaction process does not allow rollbacks&lt;br&gt;
      Clustering is not supported&lt;br&gt;
      Statements are executed serially.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Which command can be used to get the version of SQL Server?&lt;/strong&gt;&lt;br&gt;
To get the version of SQL Server, use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Select SERVERPROPERTY('productversion')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. In SQL Server, what is a user defined function?&lt;/strong&gt;&lt;br&gt;
A user defined function allows users to write their logic as per need. The advantage is that it is not limited to pre-defined functions and writing functions, simply complex SQL code. The return type is a table or a scalar value.&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
&lt;code&gt;Create function sample(@sid int)  &lt;br&gt;
returns table  &lt;br&gt;
as  &lt;br&gt;
return select * from s where Id = @sid&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11. Explain types of replication in SQL Server.&lt;/strong&gt;&lt;br&gt;
There are three types of replication as follows:&lt;/p&gt;

&lt;p&gt;Transactional replication- It is a process of data distribution from publisher to subscriber. Transactional replication can be used when data is changed frequently.&lt;br&gt;
Merge replication- It groups the data to a single centralized database from various sources. Merge replication is used in cases where central and branch databases need to update information simultaneously.&lt;br&gt;
Snapshot replication- This replication is the best way to replicate data that changes infrequently, and it is easiest to maintain. Example: Snapshot replication can be used for lists that are updated once per day and needs to be distributed from main server to branch servers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;12. Define referential integrity.&lt;/strong&gt;&lt;br&gt;
Every foreign key value must have a corresponding primary key value. The maintenance of this consistency between foreign and primary keys is known as referential integrity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. What are TCL Commands? and List down the TCL Commands available on SQL Server?&lt;/strong&gt;&lt;br&gt;
TCL or Transactional Control Language commands are used to manage different transactions taking place in a database. The three TCL commands are as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rollback&lt;/strong&gt;- This is used to restore the database to the last committed state&lt;br&gt;
&lt;strong&gt;Save Tran&lt;/strong&gt;- This saves the transaction, and the transaction can be rolled back to this point.&lt;br&gt;
&lt;strong&gt;Commit&lt;/strong&gt;- Saves the transaction permanently in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14. Write a SQL Server Query to get the letter ‘e’ in the name ‘Jenna’ from the student table.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Select CHARINDEX('e',NAME,0) from student where name='Jenna'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. As a SQL developer, how will you ensure that SQL server-based applications and databases perform well?&lt;/strong&gt;&lt;br&gt;
The volume of data, type of information stored, and data to be accessed must be checked. When a system is being upgraded, the present data should be analyzed, and the methods of accessing data should be checked to help understand problem design. Keeping the information about data is necessary when using a new system. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;16. When should Server-based cursors be used?&lt;/strong&gt;&lt;br&gt;
When you require to work on one record at any instance of time, instead of taking all the data from the table as bulk. Cursors’ performance is affected when large volumes of data are present.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;17. Tell us about the working of the FLOOR function.&lt;/strong&gt; &lt;br&gt;
FLOOR function rounds the given non-integer value to the previous least integer—for example, FLOOR(5.6) returns 5&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;18. What do you know about scheduled tasks in SQL Server?&lt;/strong&gt;&lt;br&gt;
Scheduled jobs or tasks automate processes that can be run at a prescribed time at a regular interval. By scheduling tasks, human intervention is reduced, and tasks can be carried out at any time in the order that the user wants.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;19. Mention a query that returns the list of triggers in a database.&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Select * from sys.objects where type='tr'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;20. Differentiate between rollback and commit.&lt;/strong&gt;&lt;br&gt;
When COMMIT is executed, all statements between BEGIN and COMMIT become persistent to the database. Whereas, when ROLLBACK is executed, all statements between ROLLBACK and BEGIN are reverted to the state. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;21. Explain how to create a table in SQL.&lt;/strong&gt;&lt;br&gt;
The following query is used to create a SQL table:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Create table name_of_table(  column1 datatype, column2 datatype )&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
`create table Student&lt;br&gt;&lt;br&gt;
    (  &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name varchar(20),  

DOB date,  

Marks nvarchar(5),  

Subject varchar(20)   
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22. What is the function of a foreign key in a database?&lt;/strong&gt;&lt;br&gt;
A foreign key is used to define a relationship between the parent and child table connected by columns. The foreign key is a constraint that ensures that the values of the child table appear in the parent table. The foreign key of one table is the primary key of the other, and a table can have several foreign keys. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student {ID, Name, Age, Contact, Gender, Add}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;24. Tell us the steps to hide SQL Server Instances.&lt;/strong&gt;&lt;br&gt;
To hide the SQL Server Instances, we need to make changes in SQL Server Configuration Manager, and to launch it, the following steps are needed:&lt;/p&gt;

&lt;p&gt;Select instance of SQL server&lt;br&gt;
Select properties after right-clicking&lt;br&gt;
Set Hide Instances to Yes and click on APPLY&lt;br&gt;
Post changes, restart the instance of SQL Server&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25. Explain the DBCC command and its use.&lt;/strong&gt; &lt;br&gt;
Database Consistency Checker (DBCC) checks the consistency of the database; It helps in reviewing and monitoring the maintenance of database, tables, and operation validation. For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DBCC CHECKALLOC&lt;/strong&gt; checks all pages in the database to ensure they are correctly allocated.&lt;br&gt;
&lt;strong&gt;DBCC CHECKDB&lt;/strong&gt; makes sure that indexes are correctly linked in the tables of the database.&lt;br&gt;
&lt;strong&gt;DBCC CHECKFILEGROUP&lt;/strong&gt; checks all file groups for damage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;26. Describe the SIGN function.&lt;/strong&gt;&lt;br&gt;
 The SIGN function is used to specify a number as positive, zero, or negative. It returns the following: SIGN (number)&lt;/p&gt;

&lt;p&gt;Returns – 1 if number &amp;lt;0, +1 if number&amp;gt;0 and 0 if number=0&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;27.Define alternate key.&lt;/strong&gt;&lt;br&gt;
When a table has more than one candidate key (i.e., candidate for primary keys), one becomes the primary key, and the rest are the alternate keys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;28. Define Join. What are the different types of joins?&lt;/strong&gt;&lt;br&gt;
Joins are used in SQL queries to describe how different tables are related. They also allow users to select data from one table depending on the data of the other table. The different types of joins are:&lt;/p&gt;

&lt;p&gt;INNER Joins&lt;br&gt;
OUTER Joins- LEFT OUTER, RIGHT OUTER, FULL OUTER&lt;br&gt;
CROSS Joins&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;29. Tell about the use of UPDATE STATISTICS.&lt;/strong&gt;&lt;br&gt;
 UPDATE STATISTICS is used to update information about the distribution of the key values for one or more statistic groups/collections in the indexed view or specified table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;30. Define Full backup.&lt;/strong&gt;&lt;br&gt;
The most common type of backup in SQL server is the complete backup of the database. It also includes part of the transaction logs for recovery.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;31. In SQL, what is meant by the identity column?&lt;/strong&gt; &lt;br&gt;
In SQL, an identity column generates numeric values automatically. These columns need not be indexed, and we can define the start and increment value of the identity column. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;32. Explain the UNIQUE KEY constraint.&lt;/strong&gt;&lt;br&gt;
The UNIQUE constraint maintains the uniqueness of records in the set of columns to ensure there are no duplicate values. This constraint enforces entity integrity. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;33. Define the process of de-normalization.&lt;/strong&gt;&lt;br&gt;
The process of de-normalization adds redundant data to a database in order to enhance the performance. This technique moved from higher to lower normal forms of the database. This speeds up the database access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;34. Show how table type constraint can be applied to a table.&lt;/strong&gt;&lt;br&gt;
Alter Table Name_of_the_Constraint&lt;/p&gt;

&lt;p&gt;Alter Table Constraint_1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;35. Differentiate between derived persistent attribute and derived attribute.&lt;/strong&gt;&lt;br&gt;
A derived attribute is obtained from values of other existing columns as its values do not exist on their own. A derived attribute that can be stored is a derived persistent attribute. teacher{Teach_ID, Name, ID}&lt;/p&gt;

&lt;p&gt;Here, ID is the foreign key for the teacher table.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Top 30 MongoDB Interview Questions and Answers</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 20 Jul 2024 09:52:57 +0000</pubDate>
      <link>https://dev.to/raynecoder/top-30-mongodb-interview-questions-and-answers-6a4</link>
      <guid>https://dev.to/raynecoder/top-30-mongodb-interview-questions-and-answers-6a4</guid>
      <description>&lt;p&gt;MongoDB Interview Questions and Answers: An Overview MongoDB is a document-oriented database. MongoDB is a powerful, scalable, and much more flexible database that provides high performance in case of a large volume of data and also leads to a NoSQL Database. MongoDB stores the data in a BSON (Binary JavaScript Object Notation) format.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. What is MongoDB?&lt;/strong&gt;&lt;br&gt;
Mongo-DB is a document database with high performance, high availability, and easy scalability. It is an open-source NoSQL database written in C++ language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What is NoSQL Database?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;NoSQL stands for Not Only SQL. NoSQL is a category of Database Management System (DBMS) that maintains all the rules of traditional RDBMS systems. It also does not use the conventional SQL syntaxes to fetch the data from the database. This type of database system is typically used in case of a very large volume of data. Some of the well-known NoSQL database systems are – Cassandra, BigTable, DynamoDB, MongoDB, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What are the types of NoSQL databases?&lt;/strong&gt;&lt;br&gt;
There are four types of NoSQL Database available:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document Database&lt;/strong&gt;&lt;br&gt;
This type of NoSQL database is always based on a Document-Oriented approach to store data. The main objective of the Document Database is to store all data of a single entity as a document and all documents can be stored as Collections. Some examples of Document Database – are MongoDB, CosmosDB, CouchDB, PostgreSQL, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key-Value Database&lt;/strong&gt;&lt;br&gt;
This type of database stores data in a schema-less way since key-value storage is the simplest way of storing data. A key can be a point to any type of data, like an object, string, or any other type of data. The main advantages of these databases are easy to implement and add data into. Example – Redis, DynamoDB, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Column Store Database&lt;/strong&gt;&lt;br&gt;
These types of databases store data in columns within a keyspace. The key space is always defined by a unique name, value, and timestamp. Example – Cassandra, BigTable, HBase, Vertica, HyperTable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Graph Store Database&lt;/strong&gt;&lt;br&gt;
These types of databases are mainly designed for data that can be easily represented as graph data. This means that data are interconnected with an undetermined number of data relations between them like family and social relations etc. Example – AllegroDB, GraphDB, OrientDB, Titan.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. What are the advantages of MongoDB?&lt;/strong&gt;&lt;br&gt;
The main advantages of MongoDB are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can deal with a high volume of data.&lt;/li&gt;
&lt;li&gt;It supports cross-platform&lt;/li&gt;
&lt;li&gt;It provides High Performance&lt;/li&gt;
&lt;li&gt;It is easily scalable&lt;/li&gt;
&lt;li&gt;It does not require any complex joins to retrieve data&lt;/li&gt;
&lt;li&gt;It supports both types of scaling – Horizontal &amp;amp; Vertical&lt;/li&gt;
&lt;li&gt;It is available on any cloud-based environment like Azure, AWS, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. What are Documents?&lt;/strong&gt;&lt;br&gt;
The document is the heart of MongoDB. In simple words, the document is an ordered set of keys with associated values. It is similar to the table rows in the RDBMS systems. It always maintains a dynamic scheme so that it does not require any predefined structure or fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. What is Collection?&lt;/strong&gt;&lt;br&gt;
A collection in MongoDB is a group of documents. If a document is the MongoDB analog of a row in an RDBMS, then a collection can be thought of as the analog to a table.&lt;br&gt;
Documents within a single collection can have any number of different “shapes.”, i.e. collections have dynamic schemas.&lt;br&gt;
Syntax&lt;br&gt;
&lt;code&gt;db.createCollection(name,options)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. What are Dynamic Schemas?&lt;/strong&gt;&lt;br&gt;
In MongoDB, Collections always have dynamic Schemas. Dynamic Schemas means the documents within a single collection may contain different types of structure or shapes. For example, both the below documents can be stored in a single collection:&lt;br&gt;
&lt;code&gt;{"message" : "Hello World"}&lt;br&gt;
{"id" : 10, "description" : "India"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. What is Mongo Shell?&lt;/strong&gt;&lt;br&gt;
MongoDB Shell is a JavaScript shell that allows us to interact with MongoDB instances using the command line. It is very useful to perform any administrative work along with any other data operations-related commands. Mongo Shell is automatically installed when we install MongoDB on our computers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. List out some features of MongoDB.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Indexing:&lt;/strong&gt; It supports generic secondary indexes and provides unique, compound, geospatial, and full-text indexing capabilities as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregation:&lt;/strong&gt; It provides an aggregation framework based on the concept of data processing pipelines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Special collection and index types:&lt;/strong&gt; It supports time-to-live (TTL) collections for data that should expire at a certain time&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File storage:&lt;/strong&gt; It supports an easy-to-use protocol for storing large files and file metadata.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sharding:&lt;/strong&gt; Sharding is the process of splitting data up across machines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Where can we use MongoDB?&lt;/strong&gt;&lt;br&gt;
MongoDB can be used in the following areas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content Management System&lt;/li&gt;
&lt;li&gt;Mobile Apps where data volume is very large and requires high readability of data.&lt;/li&gt;
&lt;li&gt;Data Management&lt;/li&gt;
&lt;li&gt;Big Data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;11. Which languages does MongoDB support?&lt;/strong&gt;&lt;br&gt;
Several languages are supported by MongoDB like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C++&lt;/li&gt;
&lt;li&gt;C&lt;/li&gt;
&lt;li&gt;C#&lt;/li&gt;
&lt;li&gt;Java&lt;/li&gt;
&lt;li&gt;Node.Js&lt;/li&gt;
&lt;li&gt;Perl&lt;/li&gt;
&lt;li&gt;PHP etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;12. What data types are supported by MongoDB?&lt;/strong&gt;&lt;br&gt;
MongoDB supports a wide range of data types in documents. Below are the available data types in the MongoDB.&lt;br&gt;
&lt;strong&gt;Data Types Descriptions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;String&lt;/strong&gt; It is the most commonly used data type. A string must be UTF-8 valid in MongoDB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integer&lt;/strong&gt; It is used to store numeric values. It may be either 32-bit or 64-bit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boolean&lt;/strong&gt; It is used to store Boolean data types. It's valued either true or false.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Double&lt;/strong&gt; It is used to store floating point values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arrays&lt;/strong&gt; This data type is used to store a list or multiple values in a single key&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Objects&lt;/strong&gt; This data type is used to store embedded data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Null&lt;/strong&gt; It is used to store null data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Date&lt;/strong&gt; This data type is used to store the current date or time value in Unix time format.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;13. What is ObjectId in MongoDB?&lt;/strong&gt;&lt;br&gt;
Each document stored in MongoDB must contain an “_id” key. The default value type of the “_id” is ObjectId. In a single collection, every document always contains a unique value of the “_id” field, so that every document can be identified easily. ObjectId always uses 12 bytes of storage. It always represents 24 hexadecimal digit string values.&lt;br&gt;
Objectld is composed of:&lt;br&gt;
Timestamp&lt;br&gt;
Client machine ID&lt;br&gt;
Client process ID&lt;br&gt;
3-byte incremented counter&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14. How to add data in MongoDB??&lt;/strong&gt;&lt;br&gt;
The basic method for adding data to MongoDB is “inserts”. To insert a single document, use the collection’s insertOne method:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.books.insertOne({"title" : "ScholarHat"})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For inserting multiple documents into a collection, use the method insertMany. This method enables passing an array of documents to the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. What is Capped Collection in MongoDB?&lt;/strong&gt;&lt;br&gt;
In MongoDB, Capped collections are fixed-size collections, and insert and retrieve data based on the insertion order. If a collection’s space is full, the oldest records will be overwritten by the new documents in the collection. So, to create a capped collection, the command will be –&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.createCollection(“CollectionName”, {“capped”:true, “size” : 100000})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;16. What is the Full-Text Index?&lt;/strong&gt;&lt;br&gt;
Full-text indexing is a search engine feature that enables you to perform text searches on a collection of documents within a database. Unlike traditional databases that search through text using the ‘LIKE’ query pattern match, full-text search engines tokenize the text in documents and build an index to allow very fast text search capabilities.&lt;/p&gt;

&lt;p&gt;Full-Text Index is one of the special types of Index in MongoDB for searching text within the documents. However, this type of indexing is expensive for use concerns. So, creating a full-text index on a busy collection can overload the MongoDB Server. So it is always recommended to use this type of index in an offline mode. To create a full-text index, the command is –&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;CollectionName&amp;gt;.ensureIndex({“name” : “text”})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;17. What is GridFS?&lt;/strong&gt;&lt;br&gt;
Since in MongoDB, every document size limit is 16 MB. So, if we want to insert any large binary data file, we need to use GridFS. GridFS is a mechanism through which we can store any type of large file data like an audio file, video file image, etc. It is just like a file system to store these large files and also, its related data stored in the MongoDB collection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;18. What is the purpose of using $group?&lt;/strong&gt;&lt;br&gt;
$group syntax is used to bundle or group the documents of a collection based on one or more fields. So, if we want to group the data that depends on one or more than one field, we need to pass those fields' name within the group method to create a group key and normally the group key name is “_id”.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"$group" : {"_id" : {"state" : "$state", "city" : "$city"}}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can use any type of arithmetic operator with the group command as below –&lt;br&gt;
&lt;code&gt;db.sales.aggregate(&lt;br&gt;
 {&lt;br&gt;
  "$group" : {&lt;br&gt;
  "_id" : "$country",&lt;br&gt;
  "totalRevenue" : {"$average" : "$revenue"},&lt;br&gt;
  "numSales" : {"$sum" : 1}&lt;br&gt;
 }&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;19. What is Replication?&lt;/strong&gt;&lt;br&gt;
Replication is the process that is responsible for keeping identical copies of our data in multiple servers and is always a recommended process for any type of production server. Replication always keeps our database safe even if the database server crashes or data is corrupted. With the additional copies, MongoDB maintains the copy of the data for disaster recovery, reporting, or backup purposes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;20. Why Replication is required in MongoDB?&lt;/strong&gt;&lt;br&gt;
In MongoDB, replication is required for the following reasons –&lt;br&gt;
To provide always the availability of data&lt;br&gt;
To secure our application data&lt;br&gt;
Recover the data from any type of disaster recovery&lt;br&gt;
In the Replication process, no downtime requires maintenance like backup, index rebuilds, etc.&lt;br&gt;
Replication can provide us with a read scaling means it will provide us with a copy of data only for real purposes. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;21. Explain the replica set.&lt;/strong&gt;&lt;br&gt;
It is a group of Mongo instances that maintains the same dataset. Replica sets provide redundancy and high availability and are the basis for all production deployments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22. What is the use of the map-reduce command?&lt;/strong&gt;&lt;br&gt;
Map-reduce is a way to perform aggregation.&lt;br&gt;
The Map function emits the key-value pair specified.&lt;br&gt;
The Reduce function combines the key-value pair and returns the aggregation result.&lt;br&gt;
Syntax&lt;br&gt;
&lt;code&gt;db.collection.mapReduce( &lt;br&gt;
function() {&lt;br&gt;
        emit(key,value);&lt;br&gt;
}, function(key, values) {return aggregatedResult}, { out: collection })&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;23. How to delete a Document in MongoDB?&lt;/strong&gt;&lt;br&gt;
The CRUD API in MongoDB provides deleteOne and deleteMany for this purpose. Both these methods take a filter document as their first parameter. The filter specifies a set of criteria to match against in removing documents.&lt;br&gt;
Example&lt;br&gt;
&lt;code&gt;db.list.deleteOne({"_id" : 9})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;24. Describe the process of Sharding.&lt;/strong&gt;&lt;br&gt;
Sharding is the process of splitting data up across machines. In other words, it is called “partitioning”. We can store more data and handle more load without requiring larger or more powerful machines, by putting a subset of data on each machine.&lt;/p&gt;

&lt;p&gt;In the given figure, RS0 and RS1 are shards. MongoDB’s sharding allows you to create a cluster of many machines (shards) and break up a collection across them, putting a subset of data on each shard. This allows your application to grow beyond the resource limits of a standalone server or replica set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25. What is the use of the pretty() method?&lt;/strong&gt;&lt;br&gt;
The pretty() method is used to show the results in a formatted way.&lt;br&gt;
Example&lt;br&gt;
Open your Mongo shell and create one collection with a few documents like the below:&lt;br&gt;
&lt;code&gt;db.list.insertMany([&lt;br&gt;
{_id : 1,name : "ScholarHat",age : 2,"employees" : { "JK" : 1, "SD" : 2 }},&lt;br&gt;
{_id : 2,name : "DotNetTricks",age : 10,"employees" : { "SK" : 3, "PC" : 4 }}&lt;br&gt;
])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.list.find()&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;It will insert these two documents into the collection list. Now, call db.list.find() to print out all documents in it.&lt;/p&gt;

&lt;p&gt;Output :&lt;code&gt;{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;`{ "_id" : 1, "name" : "ScholarHat", "age" : 2, "employees" : { "JK" : 1, "SD" : 2 } }&lt;/p&gt;

&lt;p&gt;{ "_id" : 2, "name" : "DotNetTricks", "age" : 10, "employees" : { "SK" : 3, "PC" : 4 } }`&lt;/p&gt;

&lt;p&gt;If the data size is too big, it becomes difficult to read. If you call the pretty() method, the result will be like this:&lt;/p&gt;

&lt;p&gt;Output : &lt;code&gt;{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
 "_id" : 1,&lt;br&gt;
 "name" : "ScholarHat",&lt;br&gt;
 "age" : 2,&lt;br&gt;
 "employees" : {&lt;br&gt;
  "JK" : 1,&lt;br&gt;
  "SD" : 2&lt;br&gt;
 }&lt;br&gt;
}&lt;br&gt;
{&lt;br&gt;
"_id" : 2,&lt;br&gt;
 "name" : "DotNetTricks",&lt;br&gt;
 "age" : 10,&lt;br&gt;
 "employees" : {&lt;br&gt;
  "SK" : 3,&lt;br&gt;
  "PC" : 4&lt;br&gt;
 }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;26. Explain the Replication Architecture in MongoDB.&lt;/strong&gt;&lt;br&gt;
In the above diagram, the PRIMARY database is the only active replica set member that receives write operations from database clients. The PRIMARY database saves data changes in the Oplog. Changes saved in the Oplog are sequential—i.e., saved in the order that they are received and executed. &lt;/p&gt;

&lt;p&gt;The SECONDARY database is querying the PRIMARY database for new changes in the Oplog. If there are any changes, then Oplog entries are copied from PRIMARY to SECONDARY as soon as they are created on the PRIMARY node.&lt;/p&gt;

&lt;p&gt;Then, the SECONDARY database applies changes from the Oplog to its data files. Oplog entries are applied in the same order they were inserted in the log. As a result, data files on SECONDARY are kept in sync with changes on PRIMARY. &lt;/p&gt;

&lt;p&gt;Usually, SECONDARY databases copy data changes directly from PRIMARY. Sometimes a SECONDARY database can replicate data from another SECONDARY. This type of replication is called Chained Replication because it is a two-step replication process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;27. What is Aggregation in MongoDB?&lt;/strong&gt;&lt;br&gt;
In MongoDB, aggregations are operations that process data records and return computed results. There are three ways to perform aggregation in MongoDB:&lt;br&gt;
Aggregation pipeline&lt;br&gt;
Map-reduce function&lt;br&gt;
Single-purpose aggregation methods&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;28. Differentiate MongoDB and RDBMS Parameters&lt;/strong&gt;&lt;br&gt;
RDBMS MongoDB&lt;br&gt;
&lt;strong&gt;Definition&lt;/strong&gt; : &lt;br&gt;
It is a relational database management system. It is a non-relational database management system Working&lt;br&gt;
Works on relationships between tables that use rows and columns A document-oriented system using documents and fields Hierarchical Data Storage Difficult to store hierarchical data In-built provision for storing hierarchical data Scalability&lt;br&gt;
Vertically scalable&lt;br&gt;
Vertically and horizontally scalable&lt;br&gt;
Performance&lt;br&gt;
Performance increases with an increase in RAM capacity&lt;br&gt;
Performance increases with an increase in processors&lt;br&gt;
&lt;strong&gt;Schema&lt;/strong&gt;&lt;br&gt;
Schema has to be pre-decided and designed; changes to the schema are difficult Dynamic creation and management of schema making the design flexible Support for Joins&lt;br&gt;
Supports complex joins No support for joins&lt;br&gt;
&lt;strong&gt;Query Language&lt;/strong&gt;&lt;br&gt;
Uses SQL for querying the database BSON is used for database querying Support for JavaScript&lt;br&gt;
No support for JavaScript-based clients to query the database Provision for Javascript-based clients to query the database&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;29. Is it possible to run multiple Javascript operations in a MongoDB instance?&lt;/strong&gt;&lt;br&gt;
Yes, we can run multiple JS operations in a MongoDB instance. Through the Mongo shell instance, we can specify the name of the JavaScript file to be run on the server. The file can contain any number of JS operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;30. Explain the Storage Engine in MongoDB.&lt;/strong&gt;&lt;br&gt;
The storage engine is a component of the database that manages how data is stored in both memory and disk. MongoDB provides support for multiple storage engines that help in better performance for different workloads. The default storage engine is WiredTiger (MongoDB3.2), which is well-suited for most workloads.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Send push notification in Mobile Using NodeJS with Firebase Service ?</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Mon, 08 Jul 2024 10:40:32 +0000</pubDate>
      <link>https://dev.to/raynecoder/how-to-send-push-notification-in-mobile-using-nodejs-with-firebase-service--52o5</link>
      <guid>https://dev.to/raynecoder/how-to-send-push-notification-in-mobile-using-nodejs-with-firebase-service--52o5</guid>
      <description>&lt;p&gt;To implement push notifications using Firebase Cloud Messaging (FCM) in a Node.js application, you need to handle FCM token storage and manage token updates for each user. Here's a step-by-step guide:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Set Up Firebase in Your Node.js Project
&lt;/h3&gt;

&lt;p&gt;First, you need to set up Firebase in your Node.js project.&lt;/p&gt;

&lt;p&gt;i. &lt;strong&gt;Install Firebase Admin SDK:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install &lt;/span&gt;firebase-admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ii. &lt;strong&gt;Initialize Firebase in your Node.js app:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;firebase-admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serviceAccount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path/to/your/serviceAccountKey.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initializeApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
     &lt;span class="na"&gt;credential&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;credential&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;serviceAccount&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Save FCM Token for Each User
&lt;/h3&gt;

&lt;p&gt;You need a database to store the FCM tokens. For this example, we'll use MongoDB.&lt;/p&gt;

&lt;p&gt;i. &lt;strong&gt;Install Mongoose:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install &lt;/span&gt;mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ii. &lt;strong&gt;Set Up Mongoose and Define User Schema:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongodb://localhost/your-database&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
     &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
     &lt;span class="na"&gt;fcmToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iii. &lt;strong&gt;API to Save/Update FCM Token:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/save-token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fcmToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fcmToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fcmToken&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fcmToken&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
         &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Token saved/updated successfully.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Internal Server Error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Send Notifications
&lt;/h3&gt;

&lt;p&gt;i. &lt;strong&gt;Function to Send Notification:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sendNotification&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="na"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="p"&gt;},&lt;/span&gt;
         &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fcmToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;};&lt;/span&gt;

       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;messaging&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Successfully sent message:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error sending message:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ii. &lt;strong&gt;API to Trigger Notification:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/send-notification&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sendNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Notification sent successfully.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Internal Server Error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Set up Firebase and MongoDB.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Create endpoints to save/update FCM tokens.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Create a function and endpoint to send notifications.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Save/Update Token:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:3000/save-token &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"userId": "user123", "fcmToken": "your-fcm-token"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Send Notification:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:3000/send-notification &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"userId": "user123", "message": {"title": "Hello", "body": "World"}}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you replace &lt;code&gt;"path/to/your/serviceAccountKey.json"&lt;/code&gt; with the actual path to your Firebase service account key JSON file. Also, ensure your MongoDB instance is running and replace the connection string with your actual MongoDB connection string.&lt;/p&gt;




</description>
    </item>
    <item>
      <title>Deploy MERN website on VPS Hostinger</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 10 Feb 2024 19:09:24 +0000</pubDate>
      <link>https://dev.to/raynecoder/deploy-mern-website-on-vps-hostinger-4g56</link>
      <guid>https://dev.to/raynecoder/deploy-mern-website-on-vps-hostinger-4g56</guid>
      <description>&lt;p&gt;Deploying a MERN (MongoDB, Express.js, React.js, Node.js) stack on a VPS (Virtual Private Server) like Hostinger involves several steps. Here's a general guide to help you get started:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Set up your VPS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in to your Hostinger account and access your VPS dashboard.&lt;/li&gt;
&lt;li&gt;Create a new VPS instance if you haven't already.&lt;/li&gt;
&lt;li&gt;Note down your VPS IP address and SSH credentials.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Connect to your VPS via SSH:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use an SSH client like PuTTY (Windows) or Terminal (Mac/Linux) to connect to your VPS using the provided SSH credentials.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Install necessary software:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update your package list: &lt;code&gt;sudo apt update&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install Node.js and npm: Follow Node.js installation instructions for your Linux distribution.&lt;/li&gt;
&lt;li&gt;Install MongoDB: You can follow the official MongoDB installation guide for your Linux distribution.&lt;/li&gt;
&lt;li&gt;Install a web server like Nginx to serve your React frontend.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Set up your MongoDB database:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure MongoDB to run as a service.&lt;/li&gt;
&lt;li&gt;Secure your MongoDB installation by setting up authentication and firewall rules.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Clone your MERN project:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Git to clone your MERN project repository onto your VPS.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Install dependencies and build your React app:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your project directory.&lt;/li&gt;
&lt;li&gt;Install backend dependencies: &lt;code&gt;npm install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to the client directory (where your React app resides) and install frontend dependencies: &lt;code&gt;npm install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Build your React app: &lt;code&gt;npm run build&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure Express.js server:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure your Express.js server is configured to serve the built React app from the &lt;code&gt;build&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Start your MERN stack:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start your Express.js server: &lt;code&gt;npm start&lt;/code&gt; or using a process manager like PM2 for better process handling.&lt;/li&gt;
&lt;li&gt;Start your MongoDB service if it's not already running.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure Nginx:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up Nginx as a reverse proxy to forward requests to your Express.js server.&lt;/li&gt;
&lt;li&gt;Create a new server block in your Nginx configuration file and configure it to point to your Express.js server.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Secure your server:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure firewall rules to only allow necessary incoming connections.&lt;/li&gt;
&lt;li&gt;Set up HTTPS using Let's Encrypt SSL certificates for secure communication.&lt;/li&gt;
&lt;li&gt;Harden your server by following security best practices.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Monitor and maintain your server:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up monitoring tools to track server performance and uptime.&lt;/li&gt;
&lt;li&gt;Regularly update your server's software and dependencies to patch security vulnerabilities.&lt;/li&gt;
&lt;li&gt;Back up your data regularly to prevent data loss.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Steps Summary to check the deployment&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;mkdir &amp;lt;project folder name&amp;gt;&lt;/code&gt; &lt;br&gt;
&lt;code&gt;cd &amp;lt;project folder&amp;gt;&lt;/code&gt; &lt;br&gt;
&lt;code&gt;git clone &amp;lt;repo url&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm install&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm run build&lt;/code&gt;&lt;br&gt;
&lt;code&gt;pm2 serve build/ 3000 --name "react-build" --spa&lt;/code&gt;&lt;br&gt;
&lt;code&gt;pm2 status&lt;/code&gt; &lt;br&gt;
&lt;code&gt;sudo ufw allow 3000&lt;/code&gt;&lt;br&gt;
&lt;code&gt;sudo ufw status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;for reactjs files&lt;br&gt;
&lt;code&gt;pm2 serve build/ 3000 --name "react-build" --spa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;for static file like html (inside the folder)&lt;br&gt;
&lt;code&gt;pm2 serve --spa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember to adapt these steps according to your specific project requirements and the configurations of your VPS provider. Additionally, it's essential to ensure the security and performance of your deployed application by implementing best practices and regularly monitoring your server.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JSON maping in ReactJS</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 21 Jan 2023 19:20:29 +0000</pubDate>
      <link>https://dev.to/raynecoder/json-maping-in-reactjs-1f84</link>
      <guid>https://dev.to/raynecoder/json-maping-in-reactjs-1f84</guid>
      <description>&lt;ul&gt;
&lt;li&gt;Create a JSON file with .js extension &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Data.js
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const JSONdata = [
  {
    id: 1,
    quote: "Life isn’t about getting and having, it’s about giving and being.",
    author: "Kevin Kruse",
  },
 {
    id: 2,
    quote: "Whatever the mind of man can conceive and believe, it can achieve.",
    author: "Napoleon Hill",
  },
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  App.js
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import JSONdata from "../Data";

const Home = () =&amp;gt; {
  return (
    &amp;lt;&amp;gt;
      {JSONdata.map((dataForMap) =&amp;gt; {
        return (
          &amp;lt;&amp;gt;
            &amp;lt;center&amp;gt;
              &amp;lt;h1&amp;gt;------------------&amp;lt;/h1&amp;gt;
              &amp;lt;p&amp;gt;{dataForMap.id}&amp;lt;/p&amp;gt;
              &amp;lt;p&amp;gt;{dataForMap.quote}&amp;lt;/p&amp;gt;
              &amp;lt;h4&amp;gt;{dataForMap.author}&amp;lt;/h4&amp;gt;
            &amp;lt;/center&amp;gt;
          &amp;lt;/&amp;gt;
        );
      })}
    &amp;lt;/&amp;gt;
  );
};

export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Finally Done !!!
&lt;/h1&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Use Live Time/Date in the ReactJS</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Sat, 21 Jan 2023 07:37:05 +0000</pubDate>
      <link>https://dev.to/raynecoder/use-live-timedate-in-the-reactjs-1fjh</link>
      <guid>https://dev.to/raynecoder/use-live-timedate-in-the-reactjs-1fjh</guid>
      <description>&lt;ul&gt;
&lt;li&gt;make the object of the Date() Method &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;const date = new Date();&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use effect to display the live time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    setInterval(() =&amp;gt; {&lt;br&gt;
      setDateState(new Date());&lt;br&gt;
    }, 1000);&lt;br&gt;
  }, []);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;give the some parameters in the return value 
&lt;code&gt;
{dateState.toLocaleString("en-US", {
      hour: "numeric",
      minute: "numeric",
      second: "2-digit",
      hour12: true,
    })}
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Complete Code of Time/Date
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { useState, useEffect } from "react";

const ClockAPI = () =&amp;gt; {
  const [dateState, setDateState] = useState(new Date());

  const t = new Date();
  const c = t.getHours() - 12;
  useEffect(() =&amp;gt; {
    setInterval(() =&amp;gt; {
      setDateState(new Date());
    }, 1000);
  }, []);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h1 className="mb-4 text-6xl font-extrabold tracking-tight leading-none  text-white-900 md:text-5xl lg:text-6xl dark:text-white"&amp;gt;
        {dateState.toLocaleString("en-US", {
          hour: "numeric",
          minute: "numeric",
          second: "2-digit",
          hour12: true,
        })}
      &amp;lt;/h1&amp;gt; 

    &amp;lt;/&amp;gt;
  );
};

export default ClockAPI;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Step the MongoDB compass for ReactJS</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Wed, 18 Jan 2023 16:30:30 +0000</pubDate>
      <link>https://dev.to/raynecoder/step-the-mongodb-compass-for-reactjs-4i8d</link>
      <guid>https://dev.to/raynecoder/step-the-mongodb-compass-for-reactjs-4i8d</guid>
      <description>&lt;ul&gt;
&lt;li&gt;Open the Compass Exe&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make a New Connection ( copy the URL for local host )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;strong&gt;Database&lt;/strong&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Like -&amp;gt; &lt;strong&gt;e-comm&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftdrsxmfn452dg95sgwkw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftdrsxmfn452dg95sgwkw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;strong&gt;Collection&lt;/strong&gt; ( &lt;em&gt;or in RDBMS called Table&lt;/em&gt; )&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;products&lt;/strong&gt; and &lt;strong&gt;users&lt;/strong&gt; is the collectiions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add the &lt;strong&gt;Data&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjxi02zx0lpre0vo92q7m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjxi02zx0lpre0vo92q7m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in insert document 📃 Add some demo data &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff1hb7h2exv45oajtvvni.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff1hb7h2exv45oajtvvni.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally Done&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to install MongoDB in Window ?</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Wed, 18 Jan 2023 16:21:13 +0000</pubDate>
      <link>https://dev.to/raynecoder/how-to-install-mongodb-in-window--22f8</link>
      <guid>https://dev.to/raynecoder/how-to-install-mongodb-in-window--22f8</guid>
      <description>&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Follow this link&lt;br&gt;
&lt;a href="https://www.mongodb.com/try/download/community" rel="noopener noreferrer"&gt;MongoDB Community&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download the .msi version in MongoDB &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step the Config files 📂 &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How to Create a Dynamic Routing using the Params() in ReactJS?</title>
      <dc:creator>Dhiman_aman</dc:creator>
      <pubDate>Fri, 13 Jan 2023 18:38:39 +0000</pubDate>
      <link>https://dev.to/raynecoder/how-to-create-a-dynamic-routing-using-the-params-in-reactjs-61k</link>
      <guid>https://dev.to/raynecoder/how-to-create-a-dynamic-routing-using-the-params-in-reactjs-61k</guid>
      <description>&lt;h2&gt;
  
  
  Use React Router Dom @6 Version to Route the Pages
&lt;/h2&gt;

&lt;h1&gt;
  
  
  Create One Page as Coin and use page in APP.js
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Routes, Route } from "react-router-dom";
import "./App.css";
import Home from "../src/Page/Home";
import User from "./Component/User";
import About from "./Page/About";
import Navbar from "./Component/Navbar";
import Coin from "../src/Page/Coin";

function App() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Navbar /&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;Home /&amp;gt;} exact /&amp;gt;
        &amp;lt;Route path="/coin/:d" element={&amp;lt;Coin /&amp;gt;}  /&amp;gt;
        &amp;lt;Route path="/about" element={&amp;lt;About /&amp;gt;}  /&amp;gt;
        &amp;lt;Route path="/user/:id" element={&amp;lt;User /&amp;gt;} /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  In the Coin Page
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from "react";
import { Link, useParams } from "react-router-dom";
import Loading from "../Component/Loading";

const Coin = () =&amp;gt; {
  const paramsData = useParams();
  const { d } = paramsData;
  const [u, sUser] = useState([]);
  const [check, setCheck] = useState(true);
  useEffect(() =&amp;gt; {
    fetch(`https://jsonplaceholder.typicode.com/users/${d}`)
      .then((res) =&amp;gt; res.json())
      .then((data) =&amp;gt; {
        sUser(data);
        //console.log(data);
        setCheck(false);
      });
  }, []);
  return (
    &amp;lt;&amp;gt;
      &amp;lt;center&amp;gt;
        {check ? (
          &amp;lt;Loading /&amp;gt;
        ) : (
          &amp;lt;&amp;gt;
            &amp;lt;p&amp;gt;
              &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt; {u.name}
            &amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;
              &amp;lt;b&amp;gt;User Name:&amp;lt;/b&amp;gt; {u.username}
            &amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;
              &amp;lt;b&amp;gt;Email: &amp;lt;/b&amp;gt;
              {u.email}
            &amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;
              &amp;lt;b&amp;gt;Website:&amp;lt;/b&amp;gt; {u.website}
            &amp;lt;/p&amp;gt;
            &amp;lt;Link to="/"&amp;gt;Go the Home&amp;lt;/Link&amp;gt;
          &amp;lt;/&amp;gt;
        )}
      &amp;lt;/center&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default Coin;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cloudcomputing</category>
      <category>docker</category>
      <category>cloud</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
