<?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: Rittwick Bhabak</title>
    <description>The latest articles on DEV Community by Rittwick Bhabak (@rittwickbhabak).</description>
    <link>https://dev.to/rittwickbhabak</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%2F503801%2Ff011d739-bc84-4e78-ac07-a1e18e342177.png</url>
      <title>DEV Community: Rittwick Bhabak</title>
      <link>https://dev.to/rittwickbhabak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rittwickbhabak"/>
    <language>en</language>
    <item>
      <title>Introduction to MongoDB</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Mon, 22 Nov 2021 08:17:05 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/introduction-to-mongodb-318i</link>
      <guid>https://dev.to/rittwickbhabak/introduction-to-mongodb-318i</guid>
      <description>&lt;h1&gt;
  
  
  1 Introduction to MongoDB
&lt;/h1&gt;

&lt;p&gt;MongoDB is a NoSQL Database. &lt;br&gt;
Instead of storing data in the form of table and rows, in mongoDB data in stored in the form of objects.&lt;br&gt;
Mongoose is ODM, which makes it easier to communicate with MongoDB.&lt;/p&gt;

&lt;p&gt;In this series we will learn about &lt;br&gt;
MongoDB and Mongoose&lt;br&gt;
CRUD application &lt;br&gt;
and a testing framework called Mocha.&lt;/p&gt;
&lt;h1&gt;
  
  
  2 Installing MongoDB Locally &amp;amp; connecting to MongoDB
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;npm init&lt;/code&gt; -- to track the dependencies.&lt;/li&gt;
&lt;li&gt;Install mongoose &lt;code&gt;npm install mongoose --save&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Connecting to database: Mongoose do not automatically knows about database. we've to tell it explicitly.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//connection.js
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/testaroo');

mongoose.connection.once('open', function(){
    console.log('Connection is made');
}).on('error', function(error){
    console.log('Connection error', error);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  3 Models and Collection
&lt;/h1&gt;

&lt;p&gt;File: models/marioChar.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const MarioCharSchema = new Schema({
    name: String,
    weight: Number
})

const MarioChar = mongoose.model('mariochar', MarioCharSchema)

module.exports = MarioChar;

// Now we can do the following
// var myChar = new MarioChar({ name: "Rayan", weight: 100 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  4 Introduction to Mocha testing
&lt;/h1&gt;

&lt;p&gt;Mocha is a testing framework.&lt;br&gt;
We use it to perform within our application&lt;br&gt;
To make sure everything works correctly&lt;br&gt;
First install Mocha &lt;code&gt;npm install mocha --save&lt;/code&gt;&lt;br&gt;
Keep the test script &lt;code&gt;demo_test.js&lt;/code&gt; to a folder &lt;code&gt;/test&lt;/code&gt;.&lt;br&gt;
demo_test.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mocha = require('mocha');
const assert = require('assert');

describe('Some demo tests', function(){
    //Create tests
    it('adds two numbers together', function(){
        assert(2+3===5)
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the &lt;code&gt;test&lt;/code&gt; property to &lt;code&gt;mocha&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
and run &lt;code&gt;npm run test&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  5 Saving data to MongoDB
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mocha = require('mocha');
const assert = require('assert');
const MarioChar = require('../models/mariochar');

describe('Some demo tests', function(){

    //Create tests
    it('Saves a record to the database', function(done){
        var character = new MarioChar({
            name: "Mario",
        })
        character.save().then(function(){
            assert(character.isNew === false)
            done();
        })
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;character.save().then(function(){
        assert(character.isNew === false)
        done();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above function saves the &lt;code&gt;character&lt;/code&gt; in the database.&lt;br&gt;
&lt;code&gt;character.isNew&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt; when the object is created but not present in the database.&lt;br&gt;
and returns &lt;code&gt;false&lt;/code&gt; when the object is found in the database.&lt;br&gt;
As &lt;code&gt;save&lt;/code&gt; is an asynchronous function, to perform next tests we have to explicitly tell when it is finished through &lt;code&gt;done&lt;/code&gt; method.&lt;/p&gt;
&lt;h3&gt;
  
  
  Using ES6 promise instead using mogoose's own promise
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

// Using ES6 promise instead mogoose's promise
mongoose.Promise = global.Promise 

// Connect to mongodb
mongoose.connect('mongodb://localhost/testaroo');
mongoose.connection.once('open', function(){
    ...
}).on('error', function(error){
  ...  
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;At this point our test is being ran even before the connection is established.&lt;br&gt;
To do that we have to put our connection code inside &lt;code&gt;before&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before(function(done){
     mongoose.connect('mongodb://localhost/testaroo');
    mongoose.connection.once('open', function(){
        console.log('Conneciton is made');
        done();
    }).on('error', function(error){
        console.log('Connection error', error);
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we've also used &lt;strong&gt;done&lt;/strong&gt; to explicitly tell when the connection is established.&lt;/p&gt;

&lt;h1&gt;
  
  
  6 Dropping a Collection
&lt;/h1&gt;

&lt;p&gt;To drop a collection:&lt;br&gt;
&lt;code&gt;mongoose.connection.collections.mariochars.drop()&lt;/code&gt;&lt;br&gt;
note that we're writing &lt;code&gt;mariochars&lt;/code&gt; (pluralized) and not &lt;code&gt;mariochar&lt;/code&gt; but we've declared the collection as &lt;code&gt;mariochar&lt;/code&gt;. This is the way mongodb works.&lt;/p&gt;

&lt;p&gt;To drop the collection before every test so that the result of one test do not affect the other test by using &lt;code&gt;beforeEach&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');mongoose.Promise = global.Promise 
before(function(done){
    // Connect to mongodb
    ...
})
beforeEach(function(done){
    mongoose.connection.collections.mariochars.drop()
    done()
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  7 Finding Records
&lt;/h1&gt;

&lt;p&gt;To search we can use &lt;code&gt;find&lt;/code&gt; or &lt;code&gt;findOne&lt;/code&gt; method to the model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MarioChar.findOne({ name: "Mario"}).then(result =&amp;gt; {
    ...
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code, finds the first match where name==="Mario"&lt;br&gt;
Our finding_test.js file is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const assert = require('assert');
const MarioChar = require('../models/mariochar');

describe('Some demo tests', function(){

    beforeEach(function(done){
        var character = new MarioChar({
            name: "Mario",
        })
        character.save().then(function(){
            assert(character.isNew === false)
            done();
        })
    })

    //Create tests
    it('Finding a record form the database', function(done){
        MarioChar.findOne({ name: "Mario"}).then(result =&amp;gt; {
            assert(result.name === "Mario")
            done();
        })
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  8 ObjectId
&lt;/h1&gt;

&lt;p&gt;To retrieve data by ID from the collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MarioChar.findOne({ _id: character._id }).then(result =&amp;gt; {
    assert(result._id === character._id)
    done();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the above code is not going to work as &lt;br&gt;
&lt;code&gt;result._id===character._id&lt;/code&gt; is going to return &lt;code&gt;false&lt;/code&gt; as &lt;br&gt;
&lt;code&gt;result._id&lt;/code&gt; is of type &lt;code&gt;ObjectID&lt;/code&gt; and &lt;code&gt;character._id&lt;/code&gt; is of type &lt;code&gt;String&lt;/code&gt;. So we've to convert both of them to String and then compare.&lt;br&gt;
&lt;code&gt;result._id.toString()===character._id.toString()&lt;/code&gt;&lt;br&gt;
will return &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  9 Deleting Record
&lt;/h1&gt;

&lt;p&gt;Mongoose has 3 records to delete records&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char.remove()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MarioChar.remove()&lt;/code&gt; Inside the bracket we are going to pass the options&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MarioChar.findOneAndRemove()&lt;/code&gt; Inside the bracket we're going to pass the options&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use of &lt;code&gt;findOneAndRemove()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MarioChar.findOneAndRemove({ name:"Mario" }).then(function(){
            MarioChar.findOne({ name:"Mario" })
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  10 Updating Record
&lt;/h1&gt;

&lt;p&gt;Various mongoose methods&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;char.update()&lt;/li&gt;
&lt;li&gt;MarioChar.update()&lt;/li&gt;
&lt;li&gt;MarioChar.findOneAndUpdate()
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Saving the data
character = new MarioChar({
    name: "Mario",
    weight: 60
})
character.save().then(function(){
    ...
})

// Updating the data
MarioChar.findOneAndUpdate({ name:"Mario"}, { name:"new_name"}).then(function(){
    MarioChar.findOne({ name:"Mario" }).then(result =&amp;gt; {
        ...
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The the new changes will be injected and this will not overwrite the complete objects. This update results to the object:&lt;br&gt;
&lt;code&gt;{ name: "new_name", weight: 100 }&lt;/code&gt; and not just &lt;code&gt;{ name: "new_name" }&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  11 Relational Data
&lt;/h1&gt;

&lt;p&gt;Suppose, Author(name, age, books) and Book(title, pages) are two relations.&lt;br&gt;
In the following way we construct it in MongoDB&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const BookSchema = new Schema({
    title: String,
    pages: Number 
});

const AuthorSchema = new Schema({
    name: String,
    age: Number, 
    books: [BookSchema] 
})

const Author = mongoose.model('author', AuthorSchema);

module.exports = Author;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;books: [BookSchema]&lt;/code&gt; tells us that &lt;code&gt;books&lt;/code&gt; property is an array where the array elements will be of the form &lt;code&gt;BookSchema&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  12 Nesting Documents
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const assert = require('assert')
const Author = require('../models/author');

describe('Nesting Documents', function(){

    //Create tests
    it('Create an author with sub-documents', function(done){

        var pat = new Author({
            name: 'Patrick Ruffus',
            books:[{title:'Name of the wind', pages: 400}]
        });
        pat.save().then(function(){
            Author.findOne({name:'Patrick Ruffus'}).then(function(record){
                assert(record.books.length === 1);
                done();
            })
        })
    })

    it('Add a book to an existing author', function(done){

        var rus = new Author({
            name: 'Ruskin Bond',
            books:[{title:'The eyes have it', pages: 400}]
        });
        rus.save().then(function(){
            Author.findOne({name:'Ruskin Bond'}).then(function(record){
                record.books.push({title:"7 husband", pages:200});
                record.save().then(function(){
                    Author.findOne({ name: 'Ruskin Bond' }).then(function(record){
                        assert(record.books.length===2);
                        done();
                    })
                })
            })
        })
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Introduction to Node.js</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Mon, 22 Nov 2021 07:49:45 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/nodejs-part-1-introduction-and-set-up-4fnp</link>
      <guid>https://dev.to/rittwickbhabak/nodejs-part-1-introduction-and-set-up-4fnp</guid>
      <description>&lt;h1&gt;
  
  
  1. Introduction, Set up and Basics
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WybOd9Cu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oab19bjta4rd873rr3i3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WybOd9Cu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oab19bjta4rd873rr3i3.png" alt="1" width="595" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check node version in my computer: &lt;code&gt;node -v&lt;/code&gt;&lt;br&gt;
To start node console: &lt;code&gt;node&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Running a javascript file 'test.js'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//filename test.js
const name = 'rittwick'
console.log(name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Command to run this file: &lt;code&gt;node test&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Basics
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. The &lt;strong&gt;global&lt;/strong&gt; object
&lt;/h2&gt;

&lt;p&gt;There is an object called &lt;em&gt;global&lt;/em&gt; in node. The same object in browser console is called &lt;em&gt;window&lt;/em&gt;.&lt;br&gt;
Checkout &lt;code&gt;console.log(global);&lt;/code&gt; in node console.&lt;br&gt;
This object contains functions like &lt;code&gt;setInterval&lt;/code&gt;, &lt;code&gt;setTimeout&lt;/code&gt; etc.&lt;br&gt;
File &lt;code&gt;globaltut.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const interval = setInterval(() =&amp;gt; {
    console.log('In the interval');
}, 1000);

setTimeout(() =&amp;gt; {
    console.log("In the timeout")
    clearInterval(interval);
}, 5000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In the interval
In the interval
In the interval
In the interval
In the timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finding the filename and directory name in node
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(__dirname);
console.log(__filename);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some attributes are present in node and not in window and some are present in window and not in node global object.For example 'document' is present in window object and not in node.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Modules and Require
&lt;/h2&gt;

&lt;p&gt;We've two files: &lt;code&gt;people.js&lt;/code&gt; and &lt;code&gt;data.js&lt;/code&gt;&lt;br&gt;
people.js contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const persons = ['arun', 'shankar', 'vinod', 'irfan'];
console.log(`From people.js: ${persons}`);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;data.js contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const xyz = require('./people');
console.log(`From data.js: `, xyz);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;From people.js: arun,shankar,vinod,irfan
From data.js:  {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way persons array is not available in data.js. We have to manually send from person.js to data.js. &lt;/p&gt;

&lt;p&gt;people.js have to contain a line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.export = persons;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then only person is accessible to data.js.&lt;br&gt;
Note: &lt;em&gt;In require statement the path should be a relative path not an absolute path&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Exporting multiple things from person.js to data.js
&lt;/h3&gt;

&lt;p&gt;people.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];

module.exports = {
    personsList: persons,
    agesList: ages
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;data.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const xyz = require('./people');
console.log(xyz.personsList);
console.log(xyz.agesList);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output on running &lt;code&gt;node data&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ 'arun', 'shankar', 'vinod', 'irfan' ]
[ 12, 22, 44, 9 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;persons&lt;/code&gt; array of people.js --&amp;gt; &lt;code&gt;personsList&lt;/code&gt; array of data.js &lt;/p&gt;

&lt;p&gt;To call &lt;code&gt;persons&lt;/code&gt; array of people.js as &lt;code&gt;persons&lt;/code&gt; in data.js:&lt;br&gt;
people.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.export = {
    persons: persons,
    ages: ages
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a shortcut of these method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.export = {
    persons, ages
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there are different types of accessing tricks:&lt;br&gt;
Trick 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];

module.exports = {
    persons, ages
}


// data.js
const xyz = require('./path_of_people')
console.log(xyz.persons);
console.log(xyz.ages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trick 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];

module.exports = {
    persons, ages
}


// data.js
const { persons, ages } = require('./path_of_people')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trick 3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];

module.exports = {
    personsList: persons,
    agesList: ages
}


// data.js
const { personsList } = require('./path_of_people')
//agesList is not accessible now. Only personsList is imported here.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;node also has some built in modules: For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// OS MODULE
const os = require('os');
const os = require('os');
console.log(os.platform());
console.log(os.homedir());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another important built in module is filesystem module&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The filesystem
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Reading the file
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
fs.readFile('./textfile.txt', (err, data) =&amp;gt; {
    if(err){
        console.log('Some error happened');
    }else{
        console.log(data);
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Buffer 6c 6f 72 65 6d 20 69  ... 94 more bytes&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;data&lt;/code&gt; is a package of objects. To read it we have to convert it into string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(data);
//Output: &amp;lt;Buffer 6c 6f 72 65 6d 20 94 more bytes&amp;gt;

console.log(data.toString());
// Output: This is a nice tutorial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;readFile&lt;/code&gt; is asynchronous function. Following code reveals that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
fs.readFile('./textfile.txt', (err, data) =&amp;gt; {
        console.log(data.toString());
})
console.log('last line of the code')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;last line of the code
This is a nice tutorial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Wirting the file
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fs.writeFile('./new.txt', 'hello rittwick', () =&amp;gt; {
    console.log('File was written');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;writeFile&lt;/code&gt; is also a asynchronous function. This function overwrites the file if the file already exists and creates, writes the file if the file already not exists.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Creating and Removing Directories
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fs.mkdir('./assets', (err)=&amp;gt;{
        if(err){
            console.log('Some error happened 1');
        }else{
            console.log('Folder created');
        }
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mkdir&lt;/code&gt; creates the folder if does not exists. If exists then returns error.&lt;/p&gt;

&lt;p&gt;Creating the folder if not exists and deleting otherwise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(!fs.existsSync('./assets')){
    fs.mkdir('./assets', (err)=&amp;gt;{
        if(err){
            console.log('Some error happened 1');
        }else{
            console.log('Folder created');
        }
    })
}else{
    fs.rmdir('./assets', (err) =&amp;gt; {
        if(err){
            console.log('Some error happened 2');
        }else{
            console.log('Folder Deleted');
        }
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. Deleting Files
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(fs.existsSync('./deleteme.txt')){
    fs.unlink('./deleteme.txt', err =&amp;gt; {
        if(err){
            console.log('Some error occurred ');
        }else{
            console.log('File deleted successful');
        }
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;unlink&lt;/code&gt; deletes the file if exists otherwise returns an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Streams and Buffers
&lt;/h2&gt;

&lt;p&gt;Starts using data before it has finished loading completely. For example loading a chunk of data and watching the video before the video fully loads.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reading a stream
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const readStream = fs.createReadStream('./docs/huge.txt');
readStream.on('data', (chunk)=&amp;gt;{
    console.log('-------NEW CHUNK---------');
    console.log(chunk);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output some buffers. To get a readable text:&lt;br&gt;
&lt;code&gt;console.log(chunk.toString())&lt;/code&gt;&lt;br&gt;
OR there we can directly encode it to a readable string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const readStream = fs.createReadStream('./docs/huge.txt', { encoding:'utf8' });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Writing a Stream
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const readStream = fs.createReadStream('./docs/huge.txt', { encoding:'utf8' });
const writeStream = fs.createWriteStream('./docs/newhuge.txt');

readStream.on('data', (chunk)=&amp;gt;{
    writeStream.write('\nNEW CHUNK\n');
    writeStream.write(chunk);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Piping
&lt;/h4&gt;

&lt;p&gt;Reading streams and writing it into a writestream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const readStream = fs.createReadStream('./docs/huge.txt', { encoding:'utf8' });
const writeStream = fs.createWriteStream('./docs/newhuge.txt');

readStream.pipe(writeStream);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  3. Client and Servers
&lt;/h1&gt;

&lt;h4&gt;
  
  
  Creating a server in node
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const http = require('http');

const server = http.createServer((req, res)=&amp;gt;{
    console.log('Everytime a request is made, I pops up.');
})

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

&lt;/div&gt;



&lt;p&gt;The callback function in the above code block runs every time a request is made.&lt;br&gt;
The arguments of the callback function are &lt;code&gt;req&lt;/code&gt; and &lt;code&gt;res&lt;/code&gt;. In &lt;code&gt;req&lt;/code&gt;, the information about incoming request is present and through &lt;code&gt;req&lt;/code&gt; the response is sent back.&lt;/p&gt;

&lt;p&gt;This alone is not doing anything. The &lt;code&gt;server&lt;/code&gt; have to actively listen to the requests.&lt;br&gt;
&lt;code&gt;server.listen()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.listen(3000, 'localhost', ()=&amp;gt;{
    console.log('Listening to port number 3000')
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  4. Requests &amp;amp; Responses
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1.The Request Object
&lt;/h2&gt;

&lt;p&gt;Getting the request url and request method: &lt;code&gt;req.url&lt;/code&gt; &amp;amp; &lt;code&gt;req.method&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    console.log(req.url, req.method);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. The Response Object
&lt;/h2&gt;

&lt;p&gt;Sending response is a three step process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;setting the response header&lt;/li&gt;
&lt;li&gt;writing the response&lt;/li&gt;
&lt;li&gt;sending the response
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    //Setting response header
    res.setHeader('Content-type', 'text/plain');

    //Writing the response
    res.write(`Hello, Rittwick!`);

    //Sending the response
    res.end();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code we're just sending plain text. We can also send html.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    console.log(req.url, req.method);
    //Setting response header to 'text/html'
    res.setHeader('Content-type', 'text/html');

    res.write('&amp;lt;h1&amp;gt;Hello! Rittwick&amp;lt;/h1&amp;gt;');

    res.end();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browse automatically adds the other tags body, html and head. We can overwrite them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    res.setHeader('Content-type', 'text/html');

    res.write('&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Hello page &amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;');
    res.write('&amp;lt;h1&amp;gt;Hello! Rittwick&amp;lt;/h1&amp;gt;');

    res.end();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Sending html files
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    res.setHeader('Content-type', 'text/html');

    fs.readFile('./views/index.html', (err, data) =&amp;gt; {
        if(err){
            res.end();
        }else{
            res.write(data);
            res.end();
        }
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of doing the following in two lines, we can do it in one line &lt;code&gt;res.end(data)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res.write(data)
res.end()

Instead the shortcut
res.end(data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we are sending the only one data, like the above then only this shortcut can be applied.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Basic Routing
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    res.setHeader('Content-type', 'text/html');
    let path = './views/';
    switch(req.url){
        case '/':
            path += 'index.html';
            break;
        case '/about':
            path += 'about.html';
            break;
        default:
            path += '404.html';
            break;
    }
    fs.readFile(path, (err, data) =&amp;gt; {
        res.end(data);
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Status Codes
&lt;/h2&gt;

&lt;p&gt;Setting status codes is very easy.&lt;br&gt;
&lt;code&gt;res.statusCode=&amp;lt;your_status_code&amp;gt;&lt;/code&gt; for example &lt;code&gt;res.statusCode=200&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the following code block example, status code is set according to need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res)=&amp;gt;{
    res.setHeader('Content-type', 'text/html');
    let path = './views/';
    switch(req.url){
        case '/':
            path += 'index.html';
            res.statusCode = 200;
            break;
        case '/about':
            path += 'about.html';
            res.statusCode = 200;
            break;
        default:
            path += '404.html';
            res.statusCode = 404;
            break;
    }
    fs.readFile(path, (err, data) =&amp;gt; {
            res.end(data);
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some more important points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;100 range - Informative codes for browser&lt;/li&gt;
&lt;li&gt;200 range - Success codes&lt;/li&gt;
&lt;li&gt;300 range - redirection codes&lt;/li&gt;
&lt;li&gt;400 range - User side errors&lt;/li&gt;
&lt;li&gt;500 range - Server errors&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200 - OK&lt;/li&gt;
&lt;li&gt;301 - Resource moved&lt;/li&gt;
&lt;li&gt;404 - Page not found&lt;/li&gt;
&lt;li&gt;500 - Internal server error&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Redirects
&lt;/h2&gt;

&lt;p&gt;Suppose my website has an established route '/about'. I wish someone who also visits to '/about-me' get redirected to '/about'. The below code does that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First set the statuscode to 301 &lt;code&gt;res.statusCode=301;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then do &lt;code&gt;res.setHeader('Location', '/about');&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Send the request &lt;code&gt;res.end()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        case '/about':
            path += 'about.html';
            res.statusCode = 200;
            break;
        case '/about-me':
            res.statusCode = 301;
            res.setHeader('Location', '/about');
            res.end();
            break;
        default:
            path += '404.html';
            res.statusCode = 404;
            break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>6. Expressions, Statements, if-then-else , Methods, Method Overloading</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Thu, 10 Dec 2020 05:48:30 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/6-expressions-statements-if-then-else-methods-method-overloading-3afa</link>
      <guid>https://dev.to/rittwickbhabak/6-expressions-statements-if-then-else-methods-method-overloading-3afa</guid>
      <description>&lt;h1&gt;
  
  
  1. Expressions
&lt;/h1&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int score = 100;
if (score &amp;gt; 99){
    System.out.println("You got the high score!");
    score = 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code &lt;code&gt;score = 100&lt;/code&gt;, &lt;code&gt;You got the high score!&lt;/code&gt; and &lt;code&gt;score = 0&lt;/code&gt; are expressions only.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Keywords
&lt;/h1&gt;

&lt;p&gt;Java has 52 reserved key words&lt;/p&gt;

&lt;h1&gt;
  
  
  3. if-then-else
&lt;/h1&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(score&amp;gt;100) {
    return "Grade A";
} else if(score &amp;gt; 50) {
    return "Grade B";
} else {
    return "Grade C";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  4. Method
&lt;/h1&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static String greetUser(String username) {
        return "Welcome" + username;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  5. Method Overloading
&lt;/h1&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public static int loggedIn(int a, int b) {
        return 0;
    }
    public static int loggedIn(int a) {
        return a + 5;
    }
    public static boolean loggedIn(int a) {
        return true;
    }
    public static String loggedIn(String name) {
        return name + " is dancing";
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of the above are valid method overloading, but the example below is not valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public static int loggedIn(int a, int b) {
        return 0;
    }
    public static boolean loggedIn(int a, int b) {
        return true;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an error.&lt;/p&gt;

&lt;p&gt;Another example of method overloading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public static int sum (int a, int b, int c, int d) {
        return sum(a,b,c) + d;
    }
    public  static int sum (int a, int b, int c) {
        return sum(a,b) + c;
    }
    public static int sum (int a, int b) {
        return a + b;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Method overloading is a feature that allows us to have more than one method with the same name, so long as we use different parameters.&lt;br&gt;
It is the ability to create multiple methods of the same name with different implementations.&lt;br&gt;
Calls to an overloaded method will run a specific implementation of that method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {
    System.out.println("Hello!");
    System.out.println(5);
    System.out.println(10.75);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;println&lt;/code&gt; method is a great example of method overloading in the Java language. There are actually 10 methods with name &lt;strong&gt;println&lt;/strong&gt;. We can print an integer, double, string, and so on...&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>5. If-then block in JAVA</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Thu, 03 Dec 2020 06:11:31 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/5-if-then-block-in-java-gen</link>
      <guid>https://dev.to/rittwickbhabak/5-if-then-block-in-java-gen</guid>
      <description>&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        boolean isAlien = false;
        if(isAlien == false)
            System.out.println("It is not an alien!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            if(isAlien == false){
            System.out.println("It is not an alien!");
            System.out.println("and I'm scared of aliens");
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        int a = 60;
        int b = 70;
        if(a &amp;lt; b &amp;amp;&amp;amp; a &amp;lt; 100){
            System.out.println("I am happy");
        }
        if(a&amp;gt;50 || b&amp;lt;30) {
            System.out.println("Logical OR operator");
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assignment vs Equal to Operator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        boolean isCar = false;
        if( isCar = true ) {
            System.out.println("This is not supposed to happen");
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;if( isCar = true )&lt;/code&gt; in this line, &lt;code&gt;isCar = true&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;, similarly &lt;code&gt;isValue=50&lt;/code&gt; returns &lt;code&gt;50&lt;/code&gt;&lt;br&gt;
So, the following code is not a valid code but the above code is valid as &lt;code&gt;if&lt;/code&gt; statement accepts &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        int newValue = 50;
        if(newValue = 50) {
            System.out.println("This is true");
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
    </item>
    <item>
      <title>4. Operators, Operands, Expression, Operator Precedence </title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Thu, 03 Dec 2020 06:04:04 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/4-operators-operands-expression-184h</link>
      <guid>https://dev.to/rittwickbhabak/4-operators-operands-expression-184h</guid>
      <description>&lt;p&gt;Example of operators&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        int result = 1 + 2; // addition
        result = result - 1; // subtraction
        result = result * 10; // multiplication
        result = result / 5; // division
        result = result % 3; // modulus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Abbreviating of operators
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // result = result + 1;
        result++;

        // result = result - 1;
        result--;

        // result = result + 2;
        result += 2;

        // result = result * 10;
        result *= 10;

        // result = result / 3;
        result /= 3;

        // result = result % 2;
        result %= 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Arithmetic operators
&lt;/h1&gt;

&lt;p&gt;+, -, *, /, %, ++, --&lt;/p&gt;

&lt;h1&gt;
  
  
  Assignment operators
&lt;/h1&gt;

&lt;p&gt;=, +=, -=, *=, /=, %=, &amp;amp;=, |=, ^=, &amp;gt;&amp;gt;=, &amp;lt;&amp;lt;=&lt;/p&gt;

&lt;h1&gt;
  
  
  Comparison operators
&lt;/h1&gt;

&lt;p&gt;==, !=, &amp;gt;, &amp;lt;, &amp;gt;=, &amp;lt;=&lt;/p&gt;

&lt;h1&gt;
  
  
  Logical operators
&lt;/h1&gt;

&lt;p&gt;&amp;amp;&amp;amp;, ||, !&lt;/p&gt;

&lt;h1&gt;
  
  
  Bitwise operators
&lt;/h1&gt;

&lt;p&gt;&amp;amp;, |, ~, ^, &amp;lt;&amp;lt;, &amp;gt;&amp;gt;, &amp;gt;&amp;gt;&amp;gt;&lt;/p&gt;

&lt;p&gt;Another operator is &lt;strong&gt;Ternary Operator&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;boolean loggedIn = false;
String userStatus = loggedIn ? "User is loggedIn" : "User is not logged in";
System.out.println(userStatus);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More details can be found in&lt;br&gt;
&lt;a href="https://www.w3schools.com/java/java_operators.asp"&gt;w3schools.com&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html"&gt;oracle.com&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Java Operator Precedence
&lt;/h1&gt;

&lt;p&gt;&lt;a href="http://www.cs.bilkent.edu.tr/~guvenir/courses/CS101/op_precedence.html"&gt;Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>3. String in JAVA</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Wed, 02 Dec 2020 07:19:27 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/3-string-in-java-5fp1</link>
      <guid>https://dev.to/rittwickbhabak/3-string-in-java-5fp1</guid>
      <description>&lt;p&gt;A &lt;strong&gt;string&lt;/strong&gt; is a sequences of characters. In the case of the &lt;strong&gt;char&lt;/strong&gt;, char contains a single character only.&lt;br&gt;
A &lt;strong&gt;String&lt;/strong&gt; can contain a sequence of characters.&lt;br&gt;
Exmaple:&lt;br&gt;
&lt;code&gt;String name = "Rittwick Bhabak"&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3 Strings in Java are Immutable
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String name = "Rittwick";
String name = name + "Bhabak";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, &lt;code&gt;name&lt;/code&gt; doesn't get appended  the value "Bhabak" instead a new &lt;strong&gt;String&lt;/strong&gt; is created which consists of the previous value of &lt;code&gt;name&lt;/code&gt; plus "Bhabak".&lt;br&gt;
The net result is same, &lt;code&gt;name&lt;/code&gt; has the right values, however, a new &lt;code&gt;String&lt;/code&gt; got created and the old one got discarded.&lt;br&gt;
So, &lt;strong&gt;String&lt;/strong&gt; is &lt;strong&gt;immutable&lt;/strong&gt; in Java.&lt;br&gt;
There are a class in Java called &lt;strong&gt;StringBuffer&lt;/strong&gt; which can be changed.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>2. Variables, Datatypes, Operators in JAVA</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Wed, 02 Dec 2020 07:10:04 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/2-variables-datatypes-operators-in-java-14p0</link>
      <guid>https://dev.to/rittwickbhabak/2-variables-datatypes-operators-in-java-14p0</guid>
      <description>&lt;h1&gt;
  
  
  1. Integer
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {
        int myFirstNumber = 5;
        System.out.println(myFirstNumber);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Left of the &lt;code&gt;=&lt;/code&gt; sign have to be an expression.&lt;br&gt;
&lt;strong&gt;Expression&lt;/strong&gt; is construct that evaluates to a single value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int myMinIntValue = Integer.MIN_VALUE;&lt;/code&gt; &lt;br&gt;
Here &lt;code&gt;Integer&lt;/code&gt; is a wrapper class. Java uses the concept of a &lt;strong&gt;Wrapper&lt;/strong&gt; class for all eight primitive types - In the case of an int, we can use &lt;code&gt;Integer&lt;/code&gt;, and by doing that it gives us ways to perform operations on an int.&lt;br&gt;
In this case we're using the &lt;strong&gt;MIN_VALUE&lt;/strong&gt; to get Java to tell us the minimum int that can be stored.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int myInt = 2147483648;&lt;/code&gt; this is not human readable. It's human readable version is &lt;code&gt;int myInt = 2_147_483_648;&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Byte
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;byte myMinByteValue = Byte.MIN_VALUE;
byte myMabyteValueByteValue = Byte.MAX_VALUE;
System.out.println("Byte Minimum Value " + myMinByteValue);
System.out.println("Byte MabyteValueimum Value " + myMabyteValueByteValue);

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

&lt;/div&gt;

&lt;h1&gt;
  
  
  3. Short
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;short myMinShortValue = Short.MIN_VALUE;
short myMabyteValueShortValue = Short.MAX_VALUE;
System.out.println("Short Minimum Value " + myMinShortValue);
System.out.println("Short MabyteValueimum Value " + myMabyteValueShortValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  4. Long
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;long myLongValue = 100L;
long myMinLongValue = Long.MIN_VALUE;
long myMabyteValueLongValue = Long.MAX_VALUE;
System.out.println("Long Minimum Value " + myMinLongValue);
System.out.println("Long MabyteValueimum Value " + myMabyteValueLongValue);
long bigLongLiteralValue = 2_147_483_647_234L;
System.out.println("Big Long Literal Value " + bigLongLiteralValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  5. Float
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;float myMinFloat = Float.MIN_VALUE;
float myMaxFloat = Float.MAX_VALUE;
System.out.println("Float Minimum Value: " + myMinFloat);
System.out.println("Float Maximum Value: " + myMaxFloat);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  6. Double
&lt;/h1&gt;

&lt;p&gt;Java by default takes a floating point number as &lt;strong&gt;double&lt;/strong&gt; and whole number as &lt;strong&gt;int&lt;/strong&gt;. &lt;br&gt;
&lt;strong&gt;double&lt;/strong&gt; is faster than &lt;strong&gt;float&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;double myMinDouble = Double.MIN_VALUE;
double myMaxDouble = Double.MAX_VALUE;
System.out.println("Double Minimum Value: " + myMinDouble);
System.out.println("Double Maximum Value: " + myMaxDouble);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int myIntValue = 5;
// float myFloatValue = (float) 5.25;
// float myFloatValue = 5.25f;
float myFloatValue = 5f / 3f;
double myDoubleValue = 5.00 / 3.00;
System.out.println("MyIntValue= " + myIntValue);
System.out.println("MyFloatValue= " + myFloatValue);
System.out.println("MyDoubleValue= " + myDoubleValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case of double we can use the underscore notation also for example : &lt;code&gt;double number = 2_000_000.4_567_891d;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In general float and double are great for general floating point operations. But both are not great where precise calculations are required - this is due to a limitation with how floating point numbers are stored and not a Java problem such.
Java has a class called &lt;strong&gt;BigDecimal&lt;/strong&gt; that overcomes this.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  7. Char
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;char myChar = 'D';
char myUnicodeChar = '\u0044';
System.out.println(myChar);
System.out.println(myUnicodeChar);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  8. Boolean
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;boolean myTureBooleanValue = true;
boolean myFalseBooleanValue = false;
boolean isCustomerAdult = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
    </item>
    <item>
      <title>#13 Nesting Documents</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Sat, 28 Nov 2020 07:22:23 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/13-nesting-documents-2np5</link>
      <guid>https://dev.to/rittwickbhabak/13-nesting-documents-2np5</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const assert = require('assert')
const Author = require('../models/author');

describe('Nesting Documents', function(){

    //Create tests
    it('Create an author with sub-documents', function(done){

        var pat = new Author({
            name: 'Patrick Ruffus',
            books:[{title:'Name of the wind', pages: 400}]
        });
        pat.save().then(function(){
            Author.findOne({name:'Patrick Ruffus'}).then(function(record){
                assert(record.books.length === 1);
                done();
            })
        })
    })

    it('Add a book to an existing author', function(done){

        var rus = new Author({
            name: 'Ruskin Bond',
            books:[{title:'The eyes have it', pages: 400}]
        });
        rus.save().then(function(){
            Author.findOne({name:'Ruskin Bond'}).then(function(record){
                record.books.push({title:"7 husband", pages:200});
                record.save().then(function(){
                    Author.findOne({ name: 'Ruskin Bond' }).then(function(record){
                        assert(record.books.length===2);
                        done();
                    })
                })
            })
        })
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>#12 Relational Data</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Sat, 28 Nov 2020 06:27:32 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/12-relational-data-1h8i</link>
      <guid>https://dev.to/rittwickbhabak/12-relational-data-1h8i</guid>
      <description>&lt;p&gt;Suppose, Author(name, age, books) and Book(title, pages) are two relations.&lt;br&gt;
In the following way we construct it in MongoDB&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const BookSchema = new Schema({
    title: String,
    pages: Number 
});

const AuthorSchema = new Schema({
    name: String,
    age: Number, 
    books: [BookSchema] 
})

const Author = mongoose.model('author', AuthorSchema);

module.exports = Author;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;books: [BookSchema]&lt;/code&gt; tells us that &lt;code&gt;books&lt;/code&gt; property is an array where the array elements will be of the form &lt;code&gt;BookSchema&lt;/code&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>#11 Updating Record</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Sat, 28 Nov 2020 04:34:09 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/11-updating-record-17hn</link>
      <guid>https://dev.to/rittwickbhabak/11-updating-record-17hn</guid>
      <description>&lt;p&gt;Various mongoose methods&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;char.update()&lt;/li&gt;
&lt;li&gt;MarioChar.update()&lt;/li&gt;
&lt;li&gt;MarioChar.findOneAndUpdate()
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Saving the data
character = new MarioChar({
    name: "Mario",
    weight: 60
})
character.save().then(function(){
    ...
})

// Updating the data
MarioChar.findOneAndUpdate({ name:"Mario"}, { name:"new_name"}).then(function(){
    MarioChar.findOne({ name:"Mario" }).then(result =&amp;gt; {
        ...
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The the new changes will be injected and this will not overwrite the complete objects. This update results to the object:&lt;br&gt;
&lt;code&gt;{ name: "new_name", weight: 100 }&lt;/code&gt; and not just &lt;code&gt;{ name: "new_name" }&lt;/code&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>#10 Deleting Record</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Sat, 28 Nov 2020 04:09:58 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/10-deleting-record-1jn5</link>
      <guid>https://dev.to/rittwickbhabak/10-deleting-record-1jn5</guid>
      <description>&lt;p&gt;Mongoose has 3 records to delete records&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char.remove()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MarioChar.remove()&lt;/code&gt; Inside the bracket we are going to pass the options&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MarioChar.findOneAndRemove()&lt;/code&gt; Inside the bracket we're going to pass the options&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use of &lt;code&gt;findOneAndRemove()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MarioChar.findOneAndRemove({ name:"Mario" }).then(function(){
            MarioChar.findOne({ name:"Mario" })
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>#9 ObjectId</title>
      <dc:creator>Rittwick Bhabak</dc:creator>
      <pubDate>Sat, 28 Nov 2020 03:38:57 +0000</pubDate>
      <link>https://dev.to/rittwickbhabak/9-objectid-2fp9</link>
      <guid>https://dev.to/rittwickbhabak/9-objectid-2fp9</guid>
      <description>&lt;p&gt;To retrieve data by ID from the collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MarioChar.findOne({ _id: character._id }).then(result =&amp;gt; {
    assert(result._id === character._id)
    done();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the above code is not going to work as &lt;br&gt;
&lt;code&gt;result._id===character._id&lt;/code&gt; is going to return &lt;code&gt;false&lt;/code&gt; as &lt;br&gt;
&lt;code&gt;result._id&lt;/code&gt; is of type &lt;code&gt;ObjectID&lt;/code&gt; and &lt;code&gt;character._id&lt;/code&gt; is of type &lt;code&gt;String&lt;/code&gt;. So we've to convert both of them to String and then compare.&lt;br&gt;
&lt;code&gt;result._id.toString()===character._id.toString()&lt;/code&gt;&lt;br&gt;
will return &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
