<?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: yashgupta18</title>
    <description>The latest articles on DEV Community by yashgupta18 (@yashgupta18).</description>
    <link>https://dev.to/yashgupta18</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%2F440501%2F71d6619c-a77a-4fc3-accd-a21e69e8a7ec.png</url>
      <title>DEV Community: yashgupta18</title>
      <link>https://dev.to/yashgupta18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yashgupta18"/>
    <language>en</language>
    <item>
      <title>Beginner:Mongoose  in Nodejs</title>
      <dc:creator>yashgupta18</dc:creator>
      <pubDate>Sun, 09 Aug 2020 18:50:07 +0000</pubDate>
      <link>https://dev.to/yashgupta18/beginner-mongoose-in-nodejs-1f67</link>
      <guid>https://dev.to/yashgupta18/beginner-mongoose-in-nodejs-1f67</guid>
      <description>&lt;h1&gt;
  
  
  Some beginner level introduction to Mongoose
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Mongoose
&lt;/h3&gt;

&lt;p&gt;A popular library which manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.&lt;br&gt;
Sounds Tough!!&lt;br&gt;
Let's just say it is used to create new objects(Models) in your database&lt;br&gt;
For Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const User = mongoose.model('User', { 
    name: {
        type:String,
        required: true,
        trim:true
    },
    age:{
        type:Number,
        default:0,
        validate(value){
            if(value&amp;lt;0){
                throw new Error("Age must be positive")
            }
        }
    },
 const user1 = new User({ 
     name: 'Yash',
     age:21  
 });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above piece of code can be used to create a new user with name Yash and age 21.&lt;/p&gt;

&lt;p&gt;type, default, required, trim are all Schema types. You can more about them &lt;a href="https://mongoosejs.com/docs/schematypes.html"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also use a very popular npm library-&lt;a href="https://www.npmjs.com/package/validator"&gt;Validator Library&lt;/a&gt; for advanced validations in our projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connection to local server
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This piece of code can be used to establish connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user1.save().then(()=&amp;gt;{
    console.log(user1)
}).catch((error)=&amp;gt;{
    console.log("Error",error)
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will then save the user in your database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.listen(port, ()=&amp;gt;{
    console.log('Server is up on port '+ port)
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If built correctly, you will get a console that server is running.&lt;/p&gt;

&lt;p&gt;THANK YOU FOR READING. HOPE YOU LIKE IT.&lt;/p&gt;

</description>
      <category>node</category>
      <category>mongodb</category>
      <category>productivity</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
