<?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: iamanand07</title>
    <description>The latest articles on DEV Community by iamanand07 (@iamanand07).</description>
    <link>https://dev.to/iamanand07</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%2F1117919%2F26ff6272-c56a-45e4-9760-29e430d86285.png</url>
      <title>DEV Community: iamanand07</title>
      <link>https://dev.to/iamanand07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamanand07"/>
    <language>en</language>
    <item>
      <title>Hash! A magical datastructure</title>
      <dc:creator>iamanand07</dc:creator>
      <pubDate>Fri, 15 Sep 2023 16:34:39 +0000</pubDate>
      <link>https://dev.to/iamanand07/hash-a-magical-datastructure-4gc2</link>
      <guid>https://dev.to/iamanand07/hash-a-magical-datastructure-4gc2</guid>
      <description>&lt;p&gt;Hashing is a process of converting data into a fixed-length string of characters, which is typically a hexadecimal or binary representation. This fixed-length string is commonly known as a "hash value" or "hash code."&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of hash
&lt;/h2&gt;

&lt;p&gt;It consists of key components,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key&lt;/strong&gt;: It refers to the text that we want to hash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash function&lt;/strong&gt;: A hash function (H(x)) is a mathematical algorithm that takes the key as input and produces a fixed-length hash value as output&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;H(x)= x mod 7&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;where x is the key that we want to hash.&lt;br&gt;
In this example , hash function we have taken 7 because we limit the hashtable size to 7. We can also modify the value as per our needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash Table&lt;/strong&gt;: A Hash table is a data structure that maps keys to values using a hash function. Hash stores the data in an array where each data value has its unique index.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why do we need hash?
&lt;/h2&gt;

&lt;p&gt;In programming to insert, search, and delete data we have data structures like,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the array, the time complexity for insertion ,deletion and search takes O(n). But if we can reduce the time to O(log n) by search an element in a sorted order by binary search but then insertion and deletion becomes costly. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked Lists&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This also takes linear time complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binary Search Tree&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This takes logarithmic time in insertion ,deletion ,and search.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This takes constant time for every operations.&lt;br&gt;
So this will increase the efficiency of the program and retrieve information in no time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Collision
&lt;/h2&gt;

&lt;p&gt;Collision is a situation in which where two keys having same hash values.&lt;br&gt;
For example,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;H(x)= x mod 7&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;H(13)= 13 mod 7 =&amp;gt; 6&lt;br&gt;
  H(20)= 20 mod 7 =&amp;gt; 6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the program don't know where to store the second hash value.&lt;br&gt;
This will lead to collision. &lt;/p&gt;
&lt;h2&gt;
  
  
  Collision Handling Techniques
&lt;/h2&gt;

&lt;p&gt;Handling hash collisions is essential in hash tables, where each key needs to be mapped to a unique location  within the table. Several techniques are commonly used to address hash collisions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chaining&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This make each cell of the hash table point to a linked list of records that have the same hash function value. Chaining requires additional memory outside the table.&lt;/p&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;We have some elements like 14,21,49,50. Our hash function is &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;H(x) = x mod 7&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;H(14) = 14 mod 7 =&amp;gt; 0&lt;br&gt;
  H(21) = 21 mod 7 =&amp;gt; 0&lt;br&gt;
  H(49) = 49 mod 7 =&amp;gt; 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In 0th position of the hash table the values are stored like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;14 -&amp;gt; 21 -&amp;gt; 49
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this we clearly see that there is a collision that three key have the same hash value so the value can be stored using linked lists to store the series of values having same hash value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open Addressing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All elements are stored in the hash table itself. Each table entry contains either a record or NIL. &lt;br&gt;
This is of three types,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Linear probing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Quadratic probing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Double probing&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;We have the same elements 14,21,49,50. Our hash function is &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;H(x) = x mod 7&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;H(14) = 14 mod 7 =&amp;gt; 0&lt;br&gt;
  H(21) = 21 mod 7 =&amp;gt; 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here the value of 14 is stored in 0th position and 21 can be stored in 1th position.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>datastructures</category>
      <category>java</category>
      <category>hash</category>
    </item>
    <item>
      <title>Getting Started With MongoDB</title>
      <dc:creator>iamanand07</dc:creator>
      <pubDate>Wed, 12 Jul 2023 07:35:50 +0000</pubDate>
      <link>https://dev.to/iamanand07/getting-started-with-mongodb-353n</link>
      <guid>https://dev.to/iamanand07/getting-started-with-mongodb-353n</guid>
      <description>&lt;p&gt;Before getting started with MongoDB we have to know &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what really a database is,&lt;/li&gt;
&lt;li&gt;Why we use databases,&lt;/li&gt;
&lt;li&gt;What are the different types of databases are available,&lt;/li&gt;
&lt;li&gt;How to choose the database that suits our requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;WHAT IS A DATABASE?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Database is a collection of tables which have the information stored in rows and columns, in which we can do whatever CRUD(&lt;strong&gt;CREATE,READ,UPDATE,DELETE&lt;/strong&gt;) operations we need.&lt;br&gt;
simple right!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHY WE USE DATABASE?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we want to store large amount of data we need to have a database which is acts as a container to store our data in a understandable manner.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are the different types of databases are available?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following are the different types of databases available,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Centralized database.&lt;/li&gt;
&lt;li&gt;Distributed database.&lt;/li&gt;
&lt;li&gt;Personal database.&lt;/li&gt;
&lt;li&gt;End-user database.&lt;/li&gt;
&lt;li&gt;Commercial database.&lt;/li&gt;
&lt;li&gt;NoSQL database.&lt;/li&gt;
&lt;li&gt;Operational database.&lt;/li&gt;
&lt;li&gt;Relational database.&lt;/li&gt;
&lt;li&gt;Cloud database.&lt;/li&gt;
&lt;li&gt;Object-oriented database.&lt;/li&gt;
&lt;li&gt;Graph database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To know more about types of databases&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://www.tutorialspoint.com/Types-of-databases" rel="noopener noreferrer"&gt;
      tutorialspoint.com
    &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;How to choose the database that suits our requirements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For choosing a database for our project we have to think about five important aspects they are &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integration.&lt;/li&gt;
&lt;li&gt;Scaling Requirement.&lt;/li&gt;
&lt;li&gt;Support Consideration.&lt;/li&gt;
&lt;li&gt;CAP Consideration(Consistency, Availability, and Partition tolerance).&lt;/li&gt;
&lt;li&gt;Schemas or Data Model.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;INTRODUCTION TO MONGODB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A MongoDB is a container for all the collections, where Collection is a bunch of MongoDB documents similar to tables in RDBMS and Document is made up of the fields similar to a tuple in RDBMS, but it has a dynamic schema here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cFPF2dl8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg8npzzbzz9x6271l9sk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cFPF2dl8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg8npzzbzz9x6271l9sk.jpg" alt="Image description" width="640" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MongoDB is a NoSQL cross-platform document-oriented database.&lt;/li&gt;
&lt;li&gt;MongoDB is one of the popular databases which is written in C++.&lt;/li&gt;
&lt;li&gt;MongoDB stores data as BSON documents.&lt;/li&gt;
&lt;li&gt;BSON is a binary representation of JSON documents.&lt;/li&gt;
&lt;li&gt;It organized as Database-&amp;gt;Collections-&amp;gt;Documents.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;BASIC OPERATIONS PERFORMED IN MONGODB&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Database.&lt;/li&gt;
&lt;li&gt;Drop Database.&lt;/li&gt;
&lt;li&gt;Create Collection.&lt;/li&gt;
&lt;li&gt;Drop Collection.&lt;/li&gt;
&lt;li&gt;Insert Document.&lt;/li&gt;
&lt;li&gt;Query Document.&lt;/li&gt;
&lt;li&gt;Update Document.&lt;/li&gt;
&lt;li&gt;Delete Document.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. Create Database.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To create a new database in mongodb we can use the command &lt;br&gt;
&lt;code&gt;use DATABASE_NAME&lt;/code&gt;. The command will create a new database if it doesn't exist, otherwise, it will return the existing database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;use student
switched to student
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have created a database student with use command &lt;strong&gt;&lt;em&gt;COOL!!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Drop Database.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To drop a existing database in mongodb we can use the command &lt;br&gt;
&lt;code&gt;db.dropDatabase()&lt;/code&gt;. The command will remove a database if it does exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;use student
switched to student
&amp;gt;db.dropDatabase();
{ "ok" : 1 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we have entered into the database then we can use dropDatabase command to delete our existing database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create Collection.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To create collection in MongoDB , we can use &lt;code&gt;db.createCollection(name, options)&lt;/code&gt; &lt;br&gt;
In this command, &lt;code&gt;name&lt;/code&gt; is the name of the collection to be created. &lt;code&gt;options&lt;/code&gt; are a document and are used to specify the configuration of collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;db.createCollection("newCollection")
{ "ok" : 1 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see list of collections by &lt;code&gt;show collections&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Drop Collection.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To drop a existing collection in mongodb we can use the command &lt;br&gt;
&lt;code&gt;drop()&lt;/code&gt;. The command will remove a collection if it does exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;db.newCollection.drop()
true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;newCollection&lt;/code&gt; is the name of the collection. &lt;code&gt;true&lt;/code&gt; is the value that returned that the collection is dropped successfully.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Insert Document.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two ways for inserting a document in mongodb.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserts a single document.&lt;/li&gt;
&lt;li&gt;Inserts multiple documents.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;To insert a single document we can use &lt;code&gt;db.collection.insertOne()&lt;/code&gt;.It inserts a single document into a collection.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; db.newCollection.insertOne({
    title: 'My first Blog',
    description: 'Insert a single document to a collection',
    by: 'Anand',
    url: 'https://dev.net',
    tags: ['NoSQL', 'database', 'beginner'],
    likes: 85
 });
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5e5972397596f4d696a78944")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To insert multiple documents we can use &lt;code&gt;db.collection.insertMany()&lt;/code&gt;.It inserts multiple documents into a collection.It will pass an array of documents to the method.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.newCollection.insertMany([
   {
    title: 'My first Blog',
    description: 'Insert a single document to a collection',
    by: 'Anand',
    url: 'https://dev.net',
    tags: ['NoSQL', 'database', 'beginner'],
    likes: 85
   },

   {
    title: 'My second Blog',
    description: 'Insert multiple documents to a collection',
    by: 'Anand',
    url: 'https://dev.net',
    tags: ['NoSQL', 'database', 'beginner'],
    likes: 85


   }
]);

{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5e5972397596f4d696a78944"),
                ObjectId("5e7572397455f4d696a78597")
        ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6.QUERY DOCUMENT.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To retrieve the document that we just inserted, query 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;&amp;gt;db.newCollection.find().pretty();
{
    "id": ObjectId("5e5972397596f4d696a78944")
    "title": 'My first Blog',
    "description": 'Insert a single document to a collection',
    "by": 'Anand',
    "url": 'https://dev.net',
    "tags": [
             'NoSQL',
             'database', 
             'beginner'
],
    "likes": 85
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;find()&lt;/code&gt; method will display all the documents in a non-structured way.&lt;br&gt;
&lt;code&gt;pretty()&lt;/code&gt; method will display all the documents in a understandable manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Update Document.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are three ways to update a document in mongodb , they are&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.newCollection.updateOne(&amp;lt;filter&amp;gt;, &amp;lt;update&amp;gt;, &amp;lt;options&amp;gt;)
db.newCollection.updateMany(&amp;lt;filter&amp;gt;, &amp;lt;update&amp;gt;, &amp;lt;options&amp;gt;)
db.newCollection.replaceOne(&amp;lt;filter&amp;gt;, &amp;lt;update&amp;gt;, &amp;lt;options&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;8.DELETE DOCUMENT.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are three ways to delete a document in mongodb , they are&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.newCollection.deleteOne()&lt;/code&gt; - Delete at most a single document that match a specified filter even though multiple documents may match the specified filter.&lt;br&gt;
&lt;code&gt;db.newCollection.deleteMany()&lt;/code&gt; - Delete all documents that match a specified filter.&lt;br&gt;
&lt;code&gt;db.newCollection.remove()&lt;/code&gt; - Delete a single document or all documents that match a specified filter.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>database</category>
      <category>mongodb</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
