<?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: Dipayan Sanyal</title>
    <description>The latest articles on DEV Community by Dipayan Sanyal (@sanyaldips93).</description>
    <link>https://dev.to/sanyaldips93</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%2F410566%2Fc719a740-313a-4683-af38-18cf67ea68df.png</url>
      <title>DEV Community: Dipayan Sanyal</title>
      <link>https://dev.to/sanyaldips93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanyaldips93"/>
    <language>en</language>
    <item>
      <title>Read Operation - I / MongoDB</title>
      <dc:creator>Dipayan Sanyal</dc:creator>
      <pubDate>Mon, 23 Nov 2020 14:32:29 +0000</pubDate>
      <link>https://dev.to/sanyaldips93/read-operation-i-mongodb-9ib</link>
      <guid>https://dev.to/sanyaldips93/read-operation-i-mongodb-9ib</guid>
      <description>&lt;h2&gt;
  
  
  Query Operator -- Comparision
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({runtime: {$eq: 60}}) == db.&amp;lt;collection&amp;gt;.find({runtime: 60})&lt;/code&gt;&lt;br&gt;
The above is an equality operator. Without the comparison, it just works fine.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({runtime: {$ne: 60}})&lt;/code&gt;&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({runtime: {$gt: 60}})&lt;/code&gt;&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({runtime: {$lt: 60}})&lt;/code&gt;&lt;br&gt;
The above is a non equality operator. Respectively, greater than, lesser than. For other operators, have a look here.&lt;/p&gt;

&lt;p&gt;In case we have to query embedded documents, we have to use the dot notation within inverted commas, otherwise the dot notation will not be accepted and understood. Lets take a doc 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;{
    "rating" : {
        "average" : 3
        "channels" : ["CBS", "NBC", "Fox"]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query would be ...&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({"rating.average" : 3})&lt;/code&gt;&lt;br&gt;
We can use other operators as well with this type of querying.&lt;/p&gt;

&lt;p&gt;For arrays (if embedded, similar dot notation would be used..), the querying has a trick. If we want to match just one of the string in the array set,&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({"rating.channels": "CBS"})&lt;/code&gt;&lt;br&gt;
If we intend to match the entire array, then we have to set and look for the entire array..,&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({"rating.channels": ["CBS", "NBC", "Fox"]})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({"runtime": {$in: [30, 42]}})&lt;/code&gt;&lt;br&gt;
The $in operator takes in an array of values but does not look for an array. It matches for discrete values in the dataset and will only return those that matched. Just opposite to this is the $nin, which will return values but the ones specified in the array.&lt;/p&gt;

&lt;p&gt;=============================&lt;/p&gt;
&lt;h2&gt;
  
  
  Query Operator -- Logical
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;$or || $nor&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The $or operator works exactly like how logically it should. Its just the syntax we have to keep in mind.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.movies.find({$or: [{"rating.average": {$lt: 5}}, {"rating.average": {$gt: 9.3}}]})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Essentially we write the $or in the beginning of the query and use array to conjugately search for all the conditions. The $nor operator has a similar syntax and returns exactly what $or returns.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.movies.find({$nor: [{"rating.average": {$lt: 5}}, {"rating.average": {$gt: 9.3}}]})&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;$and&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The $and operator works similarly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.movies.find({$and: [{genres: "Horror"}, {genres: "Drama"}]})&lt;br&gt;
db.movies.find({$and: [{genres: "Horror"}, {"rating.average": {$gt: 8}}]})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Again, it conjugates the different conditions and returns a matching result.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;$not&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You may assume the $not operator works like $ne, and you are correct. Although we can write the query as follows,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.movies.find({runtime: {$not: {$eq: 60}}})&lt;/code&gt; is the same as &lt;code&gt;db.movies.find({runtime: {$ne : 60}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;=============================&lt;/p&gt;
&lt;h2&gt;
  
  
  Query Operator -- Elements
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;$exists&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets say in our collection we wanna find all persons who have a 'age' field..&lt;br&gt;
&lt;code&gt;db.users.find({age: {$exists : true}})&lt;/code&gt;&lt;br&gt;
&lt;code&gt;db.users.find({age: {$exists : true, $ne : null}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The strange way of passing two operators at the sametime is because mongodb implicitly uses and to concatenates values separated by comma. It uses "AND" implicitly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;$type&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All kind of types in mongodbo official webpage.&lt;br&gt;
However it works pretty straight forward.&lt;br&gt;
&lt;code&gt;db.users.find({phone: {$type : "double"}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;since the shell is written in JS, passing double or number would behave the same way in most cases.&lt;/p&gt;

&lt;p&gt;We can also use type a bit differently to check for multiple types.&lt;br&gt;
&lt;code&gt;db.users.find({phone: {$type : ["double", "string"]}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;=============================&lt;/p&gt;
&lt;h2&gt;
  
  
  Query Operator -- Arrays
&lt;/h2&gt;

&lt;p&gt;Lets say we have a collection as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "hobbies": [
            {
                "title" : "Sports",
                "freq" : 3
            },
            {
                "title" : "Yoga",
                "freq" : 4
            }
        ]
},
{
    "hobbies": [1,2,3]
},
{
    "hobbies": ['Cooking', 'Soccer']
},
{
    "hobbies": ['Soccer', 'Cooking']
},
{
    "hobbies": [
            {
                "title" : "Yoga",
                "freq" : 3
            },
            {
                "title" : "Cooking",
                "freq" : 5
            }
        ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to find a person with hobby as Sports, we have to write the following query : &lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({"hobbies.title" : "Sports"})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Although we treat the array of embedded docs as single embedded doc, but eventually the above query will look for title field in all of the embedded docs and match it to "Sports"..&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;$size&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now in order to find the result with the maximum number of hobbies, we could write a query that looks like this..&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({hobbies : {$size : 3}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will essentially return the document/s where the hobbies array has length: 3. By the way this is a strict quality and mongo db currently do not support for comparision.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;$all&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In order to find a document that contains multiple fields/values in an array, we can use the following query: &lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({hobbies: ['Cooking', 'Yoga']})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This would work, in the way that the query will be looking for the exact match. It would return the 3rd document from the above collection. But should we want documents that contain the above information in any order we must write the following query:&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({hobbies: {$all: ['Cooking', 'Yoga']}})&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;$elemMatch&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, when our array field is an array of documents, and we are looking for specific sets of field in that array of documents, then we use $elemMatch. In the above example if we use the following query it would not work as expected : &lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({$and: {"hobbies.title": "Yoga", "hobbies.freq": {$gt : 4}}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This would actually return the last document, even though there is no one doing Yoga at a freq of 4. This happens because mongodb is trying to find the document where the above condition exists, maybe not together, but definitely there. You see Cooking has a frequency of 5.&lt;/p&gt;

&lt;p&gt;But in order to make the query work perfectly fine, we use $elemMatch&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({hobbies : {$elemMatch: {"title": "Yoga", "freq": {$gt: 4}}}})&lt;/code&gt;&lt;br&gt;
The above will return nothing.&lt;/p&gt;

&lt;p&gt;But the below will return the last document..&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.find({hobbies : {$elemMatch: {"title": "Yoga", "freq": {$gte: 4}}}})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why? If you get the answer, write in the comments.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>database</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Javascript -- Array and useful methods</title>
      <dc:creator>Dipayan Sanyal</dc:creator>
      <pubDate>Sat, 21 Nov 2020 08:24:58 +0000</pubDate>
      <link>https://dev.to/sanyaldips93/javascript-array-and-useful-methods-4n8i</link>
      <guid>https://dev.to/sanyaldips93/javascript-array-and-useful-methods-4n8i</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;join() method joins an arrow using a delimiter.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1,2,3,4];
var a = arr.join(' '); // a = 1234;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;join() method acts like the toString() except we can specify the delimiter.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;pop() deletes the last element in the array *&lt;/em&gt;&lt;br&gt;
&lt;em&gt;// returns the value that was popped.&lt;/em&gt;&lt;br&gt;
*&lt;em&gt;push() pushes an element in the last grid of the array *&lt;/em&gt;&lt;br&gt;
&lt;em&gt;// returns the new length of the array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;shift() removes the first element in the array *&lt;/em&gt;&lt;br&gt;
&lt;em&gt;// returns the removed value.&lt;/em&gt;&lt;br&gt;
*&lt;em&gt;unshift() pushes an element in the first grid of the array *&lt;/em&gt;&lt;br&gt;
&lt;em&gt;// returns the new length of the array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;splice(position, number of elements, new elements);&lt;br&gt;
splice helps in deleting elements of the array and sending out the same array not a copy.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
//returns [ Banana,Orange,Lemon,Kiwiz,Apple,Mango ]

fruits.splice(2,2, "Pom", "Straw");
//returns [ Banana,Orange,Pom,Straw,Apple,Mango ]

fruits.splice(2,2);
//returns [ Banana, Orange, Apple, Mango ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The concat() method creates a new array by merging (concatenating) existing arrays:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias", "Linus"];
var myChildren = myGirls.concat(myBoys); 
// concatenates (joins) myGirls and myBoys
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The concat() method also takes string as arguments. We can also use two arrays in the argument to concat three arrays.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The slice() method slices out a piece of an array into a new array.&lt;br&gt;
&lt;em&gt;This example slices out a part of an array starting from array element 1 ("Orange"):&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1);
// returns [“Orange”, “Lemon”, “Apple”, “Mango”];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;It is important to understand that this method returns a new array!&lt;/em&gt;&lt;br&gt;
&lt;em&gt;The slice() method can take 2 arguments like slice(1,3)&lt;br&gt;
The method then selects elements from the start argument, and up to (but not including) the end argument.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var citrus = fruits.slice(1,3); // returns [“Orange”, “Lemon”]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The sort() method sorts an array alphabetically. The reverse() method reverses the element in an array.&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;However, by default sort() method sorts values as strings. So, in order to sort numbers we have to use compare function. Let’s use an example.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var points = [20, 30, 10 4, 70, 44];
points.sort(); 
// returns [ 10,20,30,4,44,70 ] since the problem is 2 comes after 1 and so on.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;So using compare functions we can solve this problem.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var points = [20, 30, 10 4, 70, 44];
points.sort(function(a,b) {return a-b}); // ascending order.
// here two elements are pitted against each other. If the result //is negative, a is sorted before b, and if result is positive //then b is sorted before a.
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return 0.5 - Math.random()});
// sorts randomly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Sorting object arrays.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var cars = [
    {type: ‘Volvo’, year:2016},
    {type: ‘BMW’, year:2018},
    {type: ‘Merc’, year: 2019}
];
cars.sort((a,b) =&amp;gt; a.year - b.year);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>node</category>
      <category>arrays</category>
      <category>methods</category>
    </item>
    <item>
      <title>Write Operation / MongoDB</title>
      <dc:creator>Dipayan Sanyal</dc:creator>
      <pubDate>Sat, 21 Nov 2020 08:13:08 +0000</pubDate>
      <link>https://dev.to/sanyaldips93/write-operation-mongodb-4m4a</link>
      <guid>https://dev.to/sanyaldips93/write-operation-mongodb-4m4a</guid>
      <description>&lt;p&gt;&lt;br&gt;
&lt;code&gt;db.&amp;lt;collection&amp;gt;.insert([{},{}], {ordered:false})&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;If we set ordered to false, then inserting a set of documents (some of with same id as previously inserted docs) will not completely bring the insertion functionality to a halt. It would rather look for other documents as well hoping to find new ids to insert.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.insert([{},{}], {w:1, j: undefined})&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;w&lt;/code&gt; simply means to write. And the number indicates to how many instances, in case we are using multiple instances on one server, we want this write to be acknowledged. &lt;code&gt;w : 1&lt;/code&gt; is the default and it says hey the mongodb server should have accepted that write. The storage engine is aware of it and will eventually write it to the disk. This basically acts like an acknowledgement; after the write is successful, the server sends out an acknowledgement.&lt;br&gt;
If &lt;code&gt;w: 0&lt;/code&gt; , then the write is superfast, but you dont receive any acknowledgement if the write was successful..&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;j&lt;/code&gt; stands for the journal.The journal is a separate to-do file used by the storage engine to write on before actually writing it to the disk. This ensures security if the server fails writing to the disk in case of a failure. Writing to the journal has less overhead because it simply stores the query.&lt;/p&gt;

&lt;p&gt;The above will only be done if we set &lt;code&gt;j : true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is also the option of &lt;code&gt;wtimeout&lt;/code&gt;, which indicates a timeout during the write operation.. Say you have a network failure in the event of a write, the &lt;code&gt;wtimeout&lt;/code&gt; gives the option to hold on to the query till the network reconnects. This also is a security feature, that ensures a smooth write operation.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.insert([{},{}], {w: 1, j: true, wtimeout: 100})&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

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