<?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: Abhishek Kumar</title>
    <description>The latest articles on DEV Community by Abhishek Kumar (@4bhis1).</description>
    <link>https://dev.to/4bhis1</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%2F975092%2F11bf1584-af86-434d-ada2-ff782521ffe0.jpeg</url>
      <title>DEV Community: Abhishek Kumar</title>
      <link>https://dev.to/4bhis1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/4bhis1"/>
    <language>en</language>
    <item>
      <title>Create your own Schema Type in mongoose</title>
      <dc:creator>Abhishek Kumar</dc:creator>
      <pubDate>Tue, 28 Nov 2023 20:01:03 +0000</pubDate>
      <link>https://dev.to/4bhis1/create-your-own-schema-type-in-mongoose-71k</link>
      <guid>https://dev.to/4bhis1/create-your-own-schema-type-in-mongoose-71k</guid>
      <description>&lt;p&gt;To create a new schema type in mongoose, first let's setup our server, to get this example &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your terminal, and choose specific path. After choosing the path write &lt;code&gt;npm init -y&lt;/code&gt;. Give all the basic details about your project.&lt;/li&gt;
&lt;li&gt;Install some basic packages like &lt;code&gt;cors&lt;/code&gt;, &lt;code&gt;express&lt;/code&gt;, &lt;code&gt;mongoose&lt;/code&gt; by &lt;code&gt;npm i express mongoose cors&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After it get install write &lt;code&gt;code .&lt;/code&gt;. It'll open VSCode for you.&lt;/li&gt;
&lt;li&gt;Write the below code for your to make your server run&lt;/li&gt;
&lt;/ol&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%2Fzff20h2011zvbuz7flys.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%2Fzff20h2011zvbuz7flys.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
save this code in &lt;code&gt;Server.js&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This will make you a basic structure of server. After that run &lt;code&gt;node Server.js&lt;/code&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your server is now running, now to create the schema type of your own, you first need to make a schemaModel. Before creating schema model, lets discuss about what we are gonna make... (Do you thought any... if then please mention it in comments). For me I am thinking to make a Color schema type, which will automatically fill the Color to the new field being entered. Sounds good right? &lt;br&gt;
So before creating our own Color schemaField let's create a small user schema&lt;/p&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%2Fw2lk57ay1xnj3qoay15i.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%2Fw2lk57ay1xnj3qoay15i.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
save this code as &lt;code&gt;userModel.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after creating schema lets create a controller, to add the user&lt;/p&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%2Fj00m5ftn0vkhtiys2sk2.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%2Fj00m5ftn0vkhtiys2sk2.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
save this code as &lt;code&gt;userController.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now lets create a route on &lt;code&gt;Server.js&lt;/code&gt; file to call this api&lt;/p&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%2F6zhaluhnm24ajhx0b2gr.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%2F6zhaluhnm24ajhx0b2gr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;change your code accordingly, now finally create our own schema type name Color&lt;/p&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%2Fhdamtftvmxx3z3bwmtp6.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%2Fhdamtftvmxx3z3bwmtp6.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
save this code as colorSchema.js&lt;/p&gt;

&lt;p&gt;I'll explain the code after a while, lets make the necessary changes and give it a run... &lt;/p&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%2Fui0cisgv6oquat1nvfxs.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%2Fui0cisgv6oquat1nvfxs.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
make the changes accordingly in userModel.js&lt;/p&gt;

&lt;p&gt;now when you'll hit the api, it will automatically create a color in your data.&lt;/p&gt;

&lt;p&gt;Now come to the important part i.e being known what's happening in colorSchema.js file&lt;/p&gt;

&lt;p&gt;so whenever we're extending schemaType form mongoose,  the default function is triggered if no data is provided. In this case, it returns nothing. Then, the cast function is executed, where the val variable holds the current value. If val is present, it is returned; otherwise, a random color from the specified array is set.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>mongodb</category>
      <category>mongoose</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
