<?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: Jacob Pelletier</title>
    <description>The latest articles on DEV Community by Jacob Pelletier (@jacobjpelletier).</description>
    <link>https://dev.to/jacobjpelletier</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%2F1018217%2Fe7d12b61-a385-4eaf-894d-08b695419e22.jpeg</url>
      <title>DEV Community: Jacob Pelletier</title>
      <link>https://dev.to/jacobjpelletier</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jacobjpelletier"/>
    <language>en</language>
    <item>
      <title>MongoDB Guide | Indexes</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Fri, 17 Feb 2023 04:53:20 +0000</pubDate>
      <link>https://dev.to/yankeedom/mongodb-guide-indexes-1j8k</link>
      <guid>https://dev.to/yankeedom/mongodb-guide-indexes-1j8k</guid>
      <description>&lt;h2&gt;
  
  
  MongoDB Guide | Indexes
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me on my journey in learning &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt;.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I highly recommend checking out &lt;a href="https://learn.mongodb.com/?_ga=2.83089430.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;Mongo University&lt;/a&gt;!&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydseret748igmf5szkya.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydseret748igmf5szkya.jpg" alt="Image description" width="800" height="522"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What will we be covering in this guide
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is an index.&lt;/li&gt;
&lt;li&gt;What is an index used for. &lt;/li&gt;
&lt;li&gt;What to consider when using indexes.&lt;/li&gt;
&lt;li&gt;How to use indexes.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  What are indexes?
&lt;/h2&gt;

&lt;p&gt;Indexes are special data structures that store a portion of the data in and ordered and easy to search way.&lt;/p&gt;

&lt;p&gt;Indexes point to document identities, which let you look up things faster. &lt;/p&gt;

&lt;p&gt;Indexes improve query performance, reduce disk I/O, and reduce the overall resources required. &lt;/p&gt;

&lt;p&gt;All collections have at least one index, the _id field. &lt;/p&gt;

&lt;p&gt;Without indexes, MongoDB reads all documents and sorts the results in memory.&lt;/p&gt;

&lt;p&gt;With indexes, MongoDB only fetches the documents identified by the index based on the query. &lt;/p&gt;

&lt;p&gt;Every query should use at least one index. &lt;/p&gt;

&lt;p&gt;When we insert new data, we need up update the index structure. Therefore, while indexes improve read performance, indexes also have a write performance cost. &lt;/p&gt;

&lt;p&gt;The most common index types are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Single field indexes, indexes on one field only.&lt;/li&gt;
&lt;li&gt;Compound, indexes from more than one field. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can also use multikey indexes, which are indexes that operate on an array field. &lt;/p&gt;
&lt;h2&gt;
  
  
  Create A Single Field Index
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/docs/manual/core/index-single/#create-an-index-on-an-embedded-field" rel="noopener noreferrer"&gt;Mongo Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Single field indexes are indexes on a single field.🤯&lt;br&gt;
They support queries and sorts on a single field. &lt;/p&gt;

&lt;p&gt;We can create a single field index with:&lt;br&gt;
&lt;code&gt;db.coll.createIndex({fieldname:1})&lt;/code&gt; where the 'createIndex' command creates the single field index, the field on which we create the new index will be passed to 'fieldname', and then the order of the sort is given (1 for ascending, -1 for descending). &lt;/p&gt;

&lt;p&gt;In the sample airbnb database, here is how we would create a single field index on the (embedded) field of &lt;code&gt;country&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlk06e5i7uhupp59oo4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlk06e5i7uhupp59oo4b.png" alt="Image description" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this same database, we can also create a single field index on the posting URL. This posting URL ought to be unique for every posting. Therefore we can enforce this uniqueness trait when we create a new single field index on host ID.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F151ukaalqyde3b27b1h9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F151ukaalqyde3b27b1h9.png" alt="Image description" width="800" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/docs/manual/core/index-unique/#:~:text=To%20create%20a%20unique%20index,unique%20option%20set%20to%20true%20." rel="noopener noreferrer"&gt;MongoDB Enforcing Index Uniqueness Docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we were to try to insert a new document with an existing posting URL, we would get a duplicate key error. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32e6i3f3b6dkl8sjawth.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32e6i3f3b6dkl8sjawth.png" alt="Image description" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out the indexes on our sample database with &lt;code&gt;db.listingsAndReviews.getIndexes()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Notice the 'listing_url' and 'address.country' indexes in the playground results. &lt;/p&gt;

&lt;p&gt;Our indexes from the Atlas Cloud dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1k63jwg032gltjo2nz3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1k63jwg032gltjo2nz3c.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hnzqhzc5bqpcwrjwgzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hnzqhzc5bqpcwrjwgzq.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check if any of these indexes are being used in a query, we can use the &lt;code&gt;explain()&lt;/code&gt; command. &lt;/p&gt;

&lt;p&gt;In this query, we find the listing in the United States.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwws7bi7bjujcaudnn61b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwws7bi7bjujcaudnn61b.png" alt="Image description" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The explain() command returns the query plan (seen above in the mongo playground results). &lt;/p&gt;

&lt;p&gt;The winningPlan subsection lists the stages that are executed when this query is run. 'IXSCAN' uses the index (&lt;code&gt;"indexName": "address.country_1"&lt;/code&gt;) to find the country. The 'Fetch' stage reads only the documents that the index has identified. &lt;/p&gt;

&lt;p&gt;Note that sorting listings by market (i.e. city) after identifying documents by country adds another stage (SORT) to it's winning scan.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm0gb92bhrteke5wi16m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm0gb92bhrteke5wi16m.png" alt="Image description" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If a common query does not use an index, we should think about adding an index to make this query more efficient (otherwise the query will continue to scan the entire collection). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Because indexes can be a tricky topic, I will include more examples from the &lt;a href="https://learn.mongodb.com/learn/course/mongodb-indexes/lesson-2-creating-a-single-field-index-in-mongodb/learn?client=customer&amp;amp;page=2" rel="noopener noreferrer"&gt;MongoDB docs&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Single Field Index;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.createIndex({
  birthdate: 1
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a Unique Single Field Index
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.createIndex({
  email: 1
},
{
  unique:true
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;View the Indexes used in 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;db.customers.getIndexes()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Check if an index is being used on a query
a. The IXSCAN stage indicates the query is using an index and what index is being selected.
b. The COLLSCAN stage indicates a collection scan is perform, not using any indexes.
c. The FETCH stage indicates documents are being read from the collection.
d. The SORT stage indicates documents are being sorted in memory.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.explain().find({
  birthdate: {
    $gt:ISODate("1995-08-01")
    }
  })
db.customers.explain().find({
  birthdate: {
    $gt:ISODate("1995-08-01")
    }
  }).sort({
    email:1
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Creating Multikey Indexes.
&lt;/h2&gt;

&lt;p&gt;A multikey index is an index on an array field. While you can have multiple fields as part of an index, you can only include one array field per index. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flccmnc190rj4p80z957m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flccmnc190rj4p80z957m.png" alt="Image description" width="800" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using multikey to find listings with the internet amenity. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5s09s5ppoyn6zb27zc7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5s09s5ppoyn6zb27zc7.png" alt="Image description" width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that in the query above, MongoDB uses the multikey amenities index in the 'IXSCAN' stage. Then the documents can be fetched. The index values have each value (i.e. amenity) stored separately. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Because indexes can be a tricky topic, I will include more examples from the &lt;a href="https://learn.mongodb.com/learn/course/mongodb-indexes/lesson-3-creating-a-multikey-index-in-mongodb/learn?client=customer&amp;amp;page=2" rel="noopener noreferrer"&gt;MongoDB docs&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Single field Multikey Index
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.createIndex({
  accounts: 1
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;View the Indexes used in 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;db.customers.getIndexes()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Compound Indexes
&lt;/h2&gt;

&lt;p&gt;Compound indexes are indexes on multiple fields. This may include one multikey index if an array field is included. &lt;/p&gt;

&lt;p&gt;The order of the fields in a compound index matters. Preferably, fields that can be tested by equality first, followed by fields that can be used in sort, followed by fields that can be used for range. The sort order of the field values also matters.&lt;/p&gt;

&lt;p&gt;Equality tests are tests which examine fields based on a single value, such as &lt;code&gt;findOne({"host_is_superhost": true})&lt;/code&gt; or &lt;code&gt;find({"bedrooms": 1})&lt;/code&gt;. This will reduce the processing time for a query.&lt;/p&gt;

&lt;p&gt;Fields suitable for sort are those which can be sorted, for example &lt;code&gt;sort({"number_of_reviews":-1})&lt;/code&gt;. This determines the order of the results and eliminates the need for in-memory sorts. Sort order is important if query results are sorted by more than one field and they mix sort order, for example &lt;code&gt;sort({"number_of_reviews":-1, "price.numberDecimal":1})&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffkgld21nmf7myczx0ns5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffkgld21nmf7myczx0ns5.png" alt="Image description" width="800" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjasa6t5q04uvornifd67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjasa6t5q04uvornifd67.png" alt="Image description" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check this query with the explain() command, and you should see our chosen compound key fields under the 'IXSCAN' stage in 'indexName' (&lt;code&gt;"indexName": "bedrooms_1_number_of_reviews_-1_price.numberDecimal_1"&lt;/code&gt;) in the example above. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Because indexes can be a tricky topic, I will include more examples from the &lt;a href="https://learn.mongodb.com/learn/course/mongodb-indexes/lesson-4-working-with-compound-indexes-in-mongodb/learn?client=customer&amp;amp;page=2" rel="noopener noreferrer"&gt;MongoDB docs&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Compound Index
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.createIndex({
  active:1, 
  birthdate:-1,
  name:1
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Order of Fields in a Compound Index
Query:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.find({
  birthdate: {
    $gte:ISODate("1977-01-01")
    },
    active:true
    }).sort({
      birthdate:-1, 
      name:1
      })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.createIndex({
  active:1, 
  birthdate:-1,
  name:1
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;View the Indexes used in 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;db.customers.getIndexes()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Cover a query by the Index
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.explain().find({
  birthdate: {
    $gte:ISODate("1977-01-01")
    },
  active:true
  },
  {name:1,
    birthdate:1, 
    _id:0
  }).sort({
    birthdate:-1,
    name:1
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deleting Indexes
&lt;/h2&gt;

&lt;p&gt;Too many indexes can affect performance. But first you need to ensure that an index is not being used, because deleting that index will greatly impact the performance of that query. You can delete any index except for _id. You can hide an index instead of deleting it to test the effect of removing the index from the database. This can be done with the &lt;code&gt;db.collection.hideIndex(&amp;lt;index&amp;gt;)&lt;/code&gt; command. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv179x7sjvn685jqn5qsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv179x7sjvn685jqn5qsz.png" alt="Image description" width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptqh2l328w3rtwr5l82z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptqh2l328w3rtwr5l82z.png" alt="Image description" width="405" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="https://learn.mongodb.com/learn/course/mongodb-indexes/lesson-5-deleting-mongodb-indexes/learn?client=customer&amp;amp;page=2" rel="noopener noreferrer"&gt;docs&lt;/a&gt; for more information. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Complete Docs On Indexes
&lt;/h2&gt;

&lt;p&gt;can be found &lt;a href="https://learn.mongodb.com/learn/course/mongodb-indexes/conclusion/learn?client=customer&amp;amp;page=2" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Thank you. Until the next one!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React | Getting Started</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Thu, 16 Feb 2023 21:02:19 +0000</pubDate>
      <link>https://dev.to/yankeedom/react-getting-started-28no</link>
      <guid>https://dev.to/yankeedom/react-getting-started-28no</guid>
      <description>&lt;h2&gt;
  
  
  React Guide | Getting Started
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me on my journey in refreshing my React skills!&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;What React is good at:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;rendering UI&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What React is not so good at (and why you should check out my Next.js Guides):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;SSG &amp;amp; SSR&lt;/li&gt;
&lt;li&gt;Smart Bundling&lt;/li&gt;
&lt;li&gt;Global state management&lt;/li&gt;
&lt;li&gt;Data fetching to edge servers&lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0m4buhq10kgnci4jnpgn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0m4buhq10kgnci4jnpgn.jpg" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What will we be covering in this guide
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Brief React background&lt;/li&gt;
&lt;li&gt;Create-react-app and brief tour. &lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;React is declarative framework. &lt;/p&gt;

&lt;p&gt;Declarative (what) vs Imperative (how). &lt;/p&gt;

&lt;p&gt;It's Friday night and I want margaritas. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The imperative journey to retrieve margs:&lt;/strong&gt;&lt;br&gt;
I arrive at the bar. I approach the bar with a casual confidence. I try to make eye contact with a bartender. Once I make eye contact with a bartender, I do a small head nod. Once the bartender asks what I want, I tell them I want a margarita with top shelf mezcal, mixed with the house mix in a 2(mezcal)-3(mix) ratio, and placed on 3 cubes of ice. No salt or sugar on the rim. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The declarative journey to retrieve margs:&lt;/strong&gt;&lt;br&gt;
I arrive at the bar. I order a margarita from the bartender. &lt;/p&gt;

&lt;p&gt;React is a declarative API for the imperative implementation of code. &lt;/p&gt;

&lt;p&gt;SPA (Single Page Application Framework).&lt;/p&gt;

&lt;p&gt;React is a SPA, meaning that the app is routed through a single file. &lt;/p&gt;
&lt;h2&gt;
  
  
  NPM and NodeJS
&lt;/h2&gt;

&lt;p&gt;First, you will need to install NPM. There are a few ways to do this. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;curl "https://nodejs.org/dist/latest/node-${VERSION:-$(wget -qO- https://nodejs.org/dist/latest/ | sed -nE 's|.*&amp;gt;node-(.*)\.pkg&amp;lt;/a&amp;gt;.*|\1|p')}.pkg" &amp;gt; "$HOME/Downloads/node-latest.pkg" &amp;amp;&amp;amp; sudo installer -store -pkg "$HOME/Downloads/node-latest.pkg" -target "/"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;brew install node&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;download from nodejs.org, (&lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;https://nodejs.org/&lt;/a&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, install some helpful devtools:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React Developer Tools: shows react components and state  in the browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Verify that you have node and npm installed by entering the following.&lt;br&gt;
&lt;code&gt;node -v&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm -v&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;npm come with node, and is the package manager for node. &lt;/p&gt;
&lt;h2&gt;
  
  
  Create React App
&lt;/h2&gt;

&lt;p&gt;See the documentation at: &lt;a href="https://create-react-app.dev/docs/getting-started" rel="noopener noreferrer"&gt;https://create-react-app.dev/docs/getting-started&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Install react app commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app
cd my-app
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0h0ondutg3h4ti8qb1c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0h0ondutg3h4ti8qb1c.png" alt="Image description" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;localhost:3000&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc97zsgrhdmrs9kyxvqv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc97zsgrhdmrs9kyxvqv3.png" alt="Image description" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's take a look at the files in the 'my-app' folder. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foivcv3dve50shuhax86w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foivcv3dve50shuhax86w.png" alt="Image description" width="359" height="812"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note the dependencies in package.json. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;react&lt;/code&gt; is the react package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;react-dom&lt;/code&gt; allows react to interact with the browser and DOM. This is essential for web apps.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;react-scripts&lt;/code&gt; includes essential tools behind create-react-app, like webpack config options. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note the scripts in package.json.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;eject&lt;/code&gt; will eject you from create-react-app and allow you to dig deeper into config options.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt; starts dev server on port 3000.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;build&lt;/code&gt; builds static assets for deployment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The app flows through index.js. As you can see the App component is responsible for what we see on localhost:3000. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcvst7ttzjh49or3mzqvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcvst7ttzjh49or3mzqvz.png" alt="Image description" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that the html file for this create-react-app can be found in the public directory. notice the ID of root that index.js selects with &lt;code&gt;document.getElementById('root')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9iu7enx4u7hj6xi2b19v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9iu7enx4u7hj6xi2b19v.png" alt="Image description" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>MongoDB Guide | Aggregation</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Thu, 16 Feb 2023 05:07:23 +0000</pubDate>
      <link>https://dev.to/yankeedom/mongodb-guide-aggregation-22d7</link>
      <guid>https://dev.to/yankeedom/mongodb-guide-aggregation-22d7</guid>
      <description>&lt;h2&gt;
  
  
  MongoDB Guide | Aggregation
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me on my journey in learning &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt;.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I highly recommend checking out &lt;a href="https://learn.mongodb.com/?_ga=2.83089430.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;Mongo University&lt;/a&gt;!&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydseret748igmf5szkya.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydseret748igmf5szkya.jpg" alt="Image description" width="800" height="522"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What will we be covering in this guide
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What aggregation is. &lt;/li&gt;
&lt;li&gt;What the stages are. &lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  Introduction.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is aggregation?&lt;/strong&gt;&lt;br&gt;
Aggregation is an analysis and aggregation of data. An aggregation occurs in one or more stages that process documents. Documents can be filtered, sorted, grouped, and transformed in a pipeline. Outputs of one stage become inputs for another. Aggregations do not affect the original source data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.collection.aggregate([
  { $stage_name: {&amp;lt;expression&amp;gt;} },
  { $stage_name: {&amp;lt;expression&amp;gt;} }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each stage is a single operation on the data. Common operation stages are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;$match&lt;/strong&gt; - filters for data that match criteria. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;$group&lt;/strong&gt; - groups documents based on criteria.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;$sort&lt;/strong&gt; - puts documents in a specified order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each stage has its own syntax to carry out its operations. &lt;/p&gt;

&lt;p&gt;Field names prefixed with a dollar sign are called "field paths". It allows us to refer to the value in that field. &lt;/p&gt;

&lt;p&gt;For example, the code below grabs the values of the &lt;code&gt;first_name&lt;/code&gt; and &lt;code&gt;last_name&lt;/code&gt; value fields and sets the concatenation of these values to &lt;code&gt;defaultUsername&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;$set: {
  defaultUsername: {
     $concat: ["$first_name", "$last_name"]
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An aggregation is a collection and summary of data. A stage is a built-in methode that can be completed on data (does not alter it). An aggregation pipeline is a series of stages completed in data in some order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.collection.aggregate([
    {
        $stage1: {
            { expression1 }...
        },
        $stage2: {
            { expression1 }...
        }
    }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Stage Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{$match: {&amp;lt;query&amp;gt;}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{$expr: {&amp;lt;expression&amp;gt;}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;db.collection.aggregate([])&lt;/code&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Match and Group Stages
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;These are very common aggregations.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;$match: Filters documents matching some criteria
a. takes one argument.
b. works exactly like a find command.
c. {$match: {"state": "CT"}}.
d. place as early as possible so it can use indexes.
e. it reduces the amount of documents, and thus the amount of processing required.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*&lt;em&gt;match airbnb example *&lt;/em&gt;&lt;br&gt;
Find all airbnbs with 5 or more bedrooms&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ $match: { "beds": { $gte: 5 } } },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxcjcx4zzrczqvyo3jm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxcjcx4zzrczqvyo3jm3.png" alt="Image description" width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;$group: Creates a single document for each distinct value.
a. groups documents by a group key.
b. output is one document for each unique value of the group key.
c. requires a group key, specified by &lt;code&gt;_id&lt;/code&gt;, the field to group by.
d. it may also include one or more fields with an accumulator. 
e. the accumulator specifies how to aggregate the information for each of the groups.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;$group generic example:&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;$group: 
  {
    _id: &amp;lt;expression&amp;gt;, // group key
    &amp;lt;field&amp;gt;: { &amp;lt;accumulator&amp;gt; : &amp;lt;expression&amp;gt; }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;$group airbnb example&lt;/strong&gt;&lt;br&gt;
Find all airbnbs with 5 or more bedrooms and return the count of each record.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews.aggregate([

    { $match: { "beds": { $gte: 5 } } },

    { $group: { _id: "$beds", count: { $count: {} } } },

    { $sort: { count: -1 } },    

])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F87wa166bu8u3pzg2qqy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F87wa166bu8u3pzg2qqy6.png" alt="Image description" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Sort and Limit Stage
&lt;/h2&gt;

&lt;p&gt;I had already used sort earlier to make the results more readable. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;$sort: as demonstrated in the $group example, the $sort stage sort by some field, such as the count field above. &lt;br&gt;
a. &lt;code&gt;1&lt;/code&gt; represents ascending order, while &lt;code&gt;-1&lt;/code&gt; represents descending order. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;$limit: limits the number of documents that are passed to the next stage.&lt;br&gt;
a. only takes a positive integer&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews.aggregate([

    { $match: { "beds": { $gte: 5 } } },

    { $group: { _id: "$beds", count: { $count: {} } } },

    { $sort: { count: -1 } },    

    { $limit: 3 }

])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4m1viqxhvzyjivhsxv28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4m1viqxhvzyjivhsxv28.png" alt="Image description" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the order of the stages matters. If we were to limit before we sorted, then we would get different results. &lt;/p&gt;




&lt;h2&gt;
  
  
  Project, Count, and Set Stage
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;$project: determines resulting output shape.
a. specifies fields to return in aggregation.
b. similar to find operation.
c. should be used last if possible. 
d. chooses which fields to keep by either inclusion or exclusion.
e. set the field to 1 to include, &lt;code&gt;&amp;lt;field&amp;gt; : 1&lt;/code&gt;
f. set the field to 0 to include, &lt;code&gt;&amp;lt;field&amp;gt; : 0&lt;/code&gt;
g. new value specified for new fields and existing fields can be given a new value, &lt;code&gt;&amp;lt;field&amp;gt; : &amp;lt;new value&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notice the differences in the project stages below. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregation #1&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj742bjzv4jpkfowtgogd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj742bjzv4jpkfowtgogd.png" alt="Image description" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregation #2&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1jvgtqnnjof2w8terz8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1jvgtqnnjof2w8terz8.png" alt="Image description" width="800" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregation #3&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1vzw33io5wqmtrmvy90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn1vzw33io5wqmtrmvy90.png" alt="Image description" width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;$count: counts the number of documents in the pipeline. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, if we wanted to count how many bed options above 5 there are, we could perform the following aggregation.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28m2hlzgae8awo1m4eip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28m2hlzgae8awo1m4eip.png" alt="Image description" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;$set: adds or modifies fields in the pipeline. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this example, we rename beds to _id.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8a8g0ybk7l6w9atwnfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8a8g0ybk7l6w9atwnfg.png" alt="Image description" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same pipeline without the $set stage.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb986e357yo6byzmpob77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb986e357yo6byzmpob77.png" alt="Image description" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Out Stage
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;$out writes the documents that are returned by an aggregation pipeline into a collection. 
a. must the the last stage.
b. creates a new collection if it does not already exist.
c. if the collection exists, $out replaces the existing collection with new data. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Store counts of airbnbs with greater than 5 beds.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d75jast8nvgozz8owbq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d75jast8nvgozz8owbq.png" alt="Image description" width="781" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>announcement</category>
      <category>web3</category>
      <category>blockchain</category>
      <category>devto</category>
    </item>
    <item>
      <title>MongoDB Guide | Modifying Query Results</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Sat, 04 Feb 2023 15:48:36 +0000</pubDate>
      <link>https://dev.to/yankeedom/mongodb-guide-modifying-query-results-2ao9</link>
      <guid>https://dev.to/yankeedom/mongodb-guide-modifying-query-results-2ao9</guid>
      <description>&lt;h2&gt;
  
  
  MongoDB Guide | Modifying Query Results
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me on my journey in learning &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt;.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I highly recommend checking out &lt;a href="https://learn.mongodb.com/?_ga=2.83089430.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;Mongo University&lt;/a&gt;!&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydseret748igmf5szkya.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydseret748igmf5szkya.jpg" alt="Image description" width="800" height="522"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What will we be covering in this guide
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Return results of a query in a specified order&lt;/li&gt;
&lt;li&gt;Limit number of documents &lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqsll3rpnorx001u5h276.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqsll3rpnorx001u5h276.gif" alt="sorting" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Returning results of a query in a specified order and limiting the number of documents returned.
&lt;/h2&gt;

&lt;p&gt;To perform theses tasks, we will take advantage of cursors and cursor methods. &lt;/p&gt;

&lt;p&gt;A Cursor is a pointer to the result set of a query. There are two cursor methods, cursor.sort() and cursor.limit(). db.collection.find() method returns a cursor of documents that match that query. Cursor methods are chained to queries and perform actions on the result set. &lt;/p&gt;

&lt;p&gt;First, connect to our DB and take a look.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffsvyzeprjk2k0hq3gvt4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffsvyzeprjk2k0hq3gvt4.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Woah, thats a lot of results! &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LhizAKZU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media2.giphy.com/media/MQ4bWOjn8C9tT2dzIb/200.webp%3Fcid%3Decf05e470uy07cmae4ty5ppq9uhw6v2057wl3ilbvfoilyt9%26rid%3D200.webp%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LhizAKZU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media2.giphy.com/media/MQ4bWOjn8C9tT2dzIb/200.webp%3Fcid%3Decf05e470uy07cmae4ty5ppq9uhw6v2057wl3ilbvfoilyt9%26rid%3D200.webp%26ct%3Dg" alt="woah" width="240" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sort() specifies the order in which the query returns matching documents. You must apply &lt;br&gt;
sort() to the cursor before retrieving any documents from the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MongoDB does not store documents in a collection in a particular order. When sorting on a field which contains duplicate values, documents containing those values may be returned in any order.&lt;/p&gt;

&lt;p&gt;If consistent sort order is desired, include at least one field in your sort that contains unique values. The easiest way to guarantee this is to include the _id field in your sort query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sort results:&lt;/strong&gt;&lt;br&gt;
Let's sort by &lt;code&gt;property_type&lt;/code&gt; and return listing &lt;code&gt;name&lt;/code&gt; alphabetically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews
.find({property_type:"House"})
.sort({name:1})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A value of 1 means to sort in ascending order, while a value of -1 will mean descending order on that property. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5nrhxg6dulmrbikzg3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5nrhxg6dulmrbikzg3h.png" alt="Image description" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can add a projection to make the results easier to read.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews
.find({property_type:"House"},
{name:1})
.sort({name:1})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd8pl7ihraaup7jxotaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd8pl7ihraaup7jxotaw.png" alt="Image description" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In MongoDB, capitol letters are sorted first, then lowercase letters. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limit results:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoooj0xj14xbuhl7rphh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoooj0xj14xbuhl7rphh.png" alt="Image description" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you wanted return a document on two conditions:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F132y85uhw5xvvyc5k9sk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F132y85uhw5xvvyc5k9sk.png" alt="Image description" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's find a house, apartment or condo in the US, sort results by listing name in ascending order, and limit results to 5.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dcljulp739wa1ayz32p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dcljulp739wa1ayz32p.png" alt="Image description" width="661" height="220"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews
.find({property_type: 
  {$in: ["House", "Apartment", "Condo"]},
  "address.country_code": "US"})
.sort({name:1})
.limit(5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The use of dot notation while accessing nested information is required.&lt;/p&gt;

&lt;p&gt;Doing Great! Keep it up!&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wsnskwbx74197mmzgyh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wsnskwbx74197mmzgyh.gif" alt="doing great" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From MongoDB docs:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxloq59w4kn5arw3k7zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxloq59w4kn5arw3k7zj.png" alt="Image description" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0twa0almesnsae8rpm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0twa0almesnsae8rpm3.png" alt="Image description" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that these operations must occur in a specific order.  First db.collection.find(), then sort(), then limit(). &lt;/p&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZPGUEZm2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media4.giphy.com/media/89sIi77nGsdnq/200w.webp%3Fcid%3Decf05e47fy9o4rn3dmh3qaqlrvu0i6gcn0cqcjwy36jf1gqf%26rid%3D200w.webp%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZPGUEZm2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media4.giphy.com/media/89sIi77nGsdnq/200w.webp%3Fcid%3Decf05e47fy9o4rn3dmh3qaqlrvu0i6gcn0cqcjwy36jf1gqf%26rid%3D200w.webp%26ct%3Dg" alt="shopping" width="200" height="112"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Choosing what data to return from a query.
&lt;/h2&gt;

&lt;p&gt;Because you rarely want to return EVERYTHING.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;projection&lt;/strong&gt; (did you catch that we used it above?). Limiting which fields are returned may improve application performance. &lt;/p&gt;

&lt;p&gt;The projection from before.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq439uzk98uzaawjuybs6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq439uzk98uzaawjuybs6.png" alt="Image description" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is an example of a projection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd.collection.find(
  {&amp;lt;field1&amp;gt; : &amp;lt;value1&amp;gt;}, 
  {&amp;lt;field2&amp;gt; : &amp;lt;value2&amp;gt;, 
  &amp;lt;field3&amp;gt; : &amp;lt;value3&amp;gt;})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are finding documents that match &lt;code&gt;&amp;lt;field1&amp;gt; : &amp;lt;value1&amp;gt;&lt;/code&gt;, and specifying the projection on &lt;code&gt;{&amp;lt;field2&amp;gt; : &amp;lt;value2&amp;gt;, &amp;lt;field3&amp;gt; : &amp;lt;value3&amp;gt;}&lt;/code&gt;. Stop to appreciate the difference between finding a document based on two fields, and this projection. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AZUgYgmr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media2.giphy.com/media/3orieUe6ejxSFxYCXe/200.webp%3Fcid%3Decf05e47x5e04kwqi4fcya3oloirsqcmyfs2awc4vdoea3ut%26rid%3D200.webp%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AZUgYgmr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media2.giphy.com/media/3orieUe6ejxSFxYCXe/200.webp%3Fcid%3Decf05e47x5e04kwqi4fcya3oloirsqcmyfs2awc4vdoea3ut%26rid%3D200.webp%26ct%3Dg" alt="magnifying glass" width="265" height="200"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// finding on two fields
db.collection.find(
  {&amp;lt;field1&amp;gt; : &amp;lt;value1&amp;gt;, 
  &amp;lt;field2&amp;gt; : &amp;lt;value2&amp;gt;})

// finding on one field and projecting on another
cd.collection.find(
  {&amp;lt;field1&amp;gt; : &amp;lt;value1&amp;gt;},
  {&amp;lt;field2&amp;gt; : &amp;lt;value2&amp;gt;})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ID field (_id) is always returned by default. If you do not wish to return the ID field, you can include _id to the projection and setting the value to 0. By setting the value to 0, you are telling MongoDB to exclude this field from the results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd.collection.find(
  {&amp;lt;field1&amp;gt; : &amp;lt;value1&amp;gt;}, 
  {&amp;lt;field2&amp;gt; : &amp;lt;value2&amp;gt;, 
  &amp;lt;field3&amp;gt; : &amp;lt;value3&amp;gt;},
  _id : 0})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesirv274cgsg7o2bjdlf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesirv274cgsg7o2bjdlf.png" alt="Image description" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inclusion and exclusion statements cannot be combined!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can choose to include fields or include fields, but not both. &lt;/p&gt;

&lt;p&gt;The only exception to this rule is the _id field, which may be included with inclusion statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews
.find({"property_type": 
  {$in: ["House", "Apartment", "Condo"]},
  "address.country_code": "US"},
  {"name":1, "property_type":1, "address.country_code":1, "_id":0})
.sort({name:1})
.limit(5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;From MongoDB docs:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8lg63circezbr6x60che.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8lg63circezbr6x60che.png" alt="Image description" width="766" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjs5y3i8inoosfxhcya54.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjs5y3i8inoosfxhcya54.png" alt="Image description" width="636" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp2m1mkqmzpz6ut19qfd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp2m1mkqmzpz6ut19qfd.png" alt="Image description" width="762" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Can you guess what this query does?&lt;br&gt;
*&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;db.sales.find({ "customer.age": { $lt: 30 }, 
  "customer.satisfaction": { $gt: 3 }, 
}, 
{ "customer.satisfaction": 1, 
  "customer.age": 1, 
  "storeLocation": 1, 
  "saleDate": 1, 
  "_id": 0, }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;What about this code?&lt;br&gt;
*&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;db.sales.find({ storeLocation: { 
  $in: ["Seattle", "New York"] }, 
}, { 
  couponUsed: 0, 
  purchaseMethod: 0, 
  customer: 0, 
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LV78iOLK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media3.giphy.com/media/l0XtbC8EniiuwAEOQn/200w.webp%3Fcid%3Decf05e47h9rt8txktx33pcaalvv3p26il4tmuwou6xs0usow%26rid%3D200w.webp%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LV78iOLK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media3.giphy.com/media/l0XtbC8EniiuwAEOQn/200w.webp%3Fcid%3Decf05e47h9rt8txktx33pcaalvv3p26il4tmuwou6xs0usow%26rid%3D200w.webp%26ct%3Dg" alt="counting" width="200" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Counting documents in a collection
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What method allows you to count documents?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;db.collection.countDocuments(&amp;lt;query&amp;gt;, &amp;lt;options&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The total number of documents in our listingsAndReviews collection.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezixl536y4setkv43jqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezixl536y4setkv43jqo.png" alt="Image description" width="646" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The total number of listing with greater than 5 beds.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56r4nmi4iy3x3s5mjp0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56r4nmi4iy3x3s5mjp0g.png" alt="Image description" width="663" height="148"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.listingsAndReviews
.countDocuments({"beds":{ $gt: 5 }})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;From MongoDB docs:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmyjajphytuowd2h3aps.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmyjajphytuowd2h3aps.png" alt="Image description" width="777" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can you guess what this code does?&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;db.sales.countDocuments({ 
  items: { 
    $elemMatch: { 
      name: "laptop", 
      price: { $lt: 600 } 
    } 
   } 
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Resources From Mongo University
&lt;/h2&gt;

&lt;p&gt;Use the following resources to learn more about modifying query results in MongoDB:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lesson 01:&lt;/strong&gt; Sorting and Limiting Query Results in MongoDB&lt;br&gt;
MongoDB Docs: &lt;a href="https://www.mongodb.com/docs/manual/reference/method/cursor.sort/?_ga=2.14948278.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;cursor.sort(&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;MongoDB Docs: &lt;a href="https://www.mongodb.com/docs/manual/reference/method/cursor.limit/?_ga=2.14948278.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;cursor.limit()&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lesson 02:&lt;/strong&gt; &lt;a href="https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/?_ga=2.82452374.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;Returning Specific Data from a Query in MongoDB&lt;/a&gt;&lt;br&gt;
MongoDB Docs: &lt;a href="https://www.mongodb.com/docs/manual/reference/limits/?&amp;amp;_ga=2.82452374.1471118752.1675467284-1993933025.1675122350#mongodb-limit-Projection-Restrictions" rel="noopener noreferrer"&gt;Project Fields to Return from Query&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MongoDB Docs: Projection Restrictions&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lesson 03:&lt;/strong&gt; Counting Documents in a MongoDB Collection&lt;br&gt;
MongoDB Docs: &lt;a href="https://www.mongodb.com/docs/manual/reference/method/db.collection.countDocuments/?_ga=2.82452374.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;db.collection.countDocuments()&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0061oddurtgbq34ie5b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0061oddurtgbq34ie5b.gif" alt="bye" width="306" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See you in the next guide!&lt;br&gt;
Next guide will be posted soon!&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>cloud</category>
      <category>database</category>
      <category>beginners</category>
    </item>
    <item>
      <title>MongoDB Guide | Getting Started</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Sat, 04 Feb 2023 01:02:09 +0000</pubDate>
      <link>https://dev.to/yankeedom/mongodb-guide-getting-started-372g</link>
      <guid>https://dev.to/yankeedom/mongodb-guide-getting-started-372g</guid>
      <description>&lt;h2&gt;
  
  
  MongoDB Guide | Getting Started
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcp38knes0qhkg6i6hmct.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcp38knes0qhkg6i6hmct.jpg" alt="Image description" width="800" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me on my journey in learning &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt;.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I highly recommend checking out &lt;a href="https://learn.mongodb.com/?_ga=2.83089430.1471118752.1675467284-1993933025.1675122350" rel="noopener noreferrer"&gt;Mongo University&lt;/a&gt;!&lt;/p&gt;




&lt;h2&gt;
  
  
  What will we be covering in this guide
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Signing up for Atlas&lt;/li&gt;
&lt;li&gt;Setting up an Atlas DB&lt;/li&gt;
&lt;li&gt;Setting up connection options&lt;/li&gt;
&lt;li&gt;Uploading data to our DB&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yom59oznr7x8ccveuwb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yom59oznr7x8ccveuwb.gif" alt="click a mouse" width="360" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is MongoDB Atlas?
&lt;/h2&gt;

&lt;p&gt;I will be using MongoDB Atlas instead of Mongo locally during these guides. &lt;/p&gt;

&lt;p&gt;Developer data platform.&lt;/p&gt;

&lt;p&gt;Database as a service (DBaaS) at its core.&lt;/p&gt;

&lt;p&gt;Atlas manages database for you. Databases have built in replication, known as a replica set. A replica set is a group of servers that hold your data. They ensure data redundancy and high availability. &lt;/p&gt;

&lt;p&gt;There are two types of database deployments, serverless and dedicated.&lt;/p&gt;

&lt;p&gt;Serverless scales on demands and only charges for what you use. Good for variable traffic or sparse usage over time. Often a simpler choice if there are a lot of unknowns. &lt;/p&gt;

&lt;p&gt;Dedicated has larger more dedicated resources and more security features and auto-scaling. The preferred choice for production workloads. &lt;/p&gt;

&lt;p&gt;Atlas includes operational insights, backups with point-in-time restore, and online archive. &lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Sign up and sign into the &lt;a href="https://www.mongodb.com/atlas/database" rel="noopener noreferrer"&gt;Atlas platform&lt;/a&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cgj4uprdzzl674rzxoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cgj4uprdzzl674rzxoq.png" alt="Image description" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new project&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuyupwba9p3tgu0divjl4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuyupwba9p3tgu0divjl4.png" alt="Image description" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name your project&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcoxzorojuc1tcgptteei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcoxzorojuc1tcgptteei.png" alt="Image description" width="800" height="458"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6cldb0irxz4s6bjg4jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6cldb0irxz4s6bjg4jo.png" alt="Image description" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating a database&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9y3uuizhnr8zyigj3mrf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9y3uuizhnr8zyigj3mrf.png" alt="Image description" width="800" height="452"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxl1othm18ceu5j5wyld6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxl1othm18ceu5j5wyld6.png" alt="Image description" width="800" height="460"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5pk71tgwj2zamki93eq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5pk71tgwj2zamki93eq.png" alt="Image description" width="800" height="458"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zh0q6d8kfwtzss0q0p5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zh0q6d8kfwtzss0q0p5.png" alt="Image description" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0svyfavwhp3azdyb91a.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0svyfavwhp3azdyb91a.webp" alt="unlock" width="266" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a DB user&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbf5f761355nv65iab6dj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbf5f761355nv65iab6dj.png" alt="Image description" width="800" height="454"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkz78nwlrfkr9fj19ujzk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkz78nwlrfkr9fj19ujzk.png" alt="Image description" width="800" height="453"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkb7sesgbd6m6e3erbg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkb7sesgbd6m6e3erbg9.png" alt="Image description" width="800" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Behold our beautiful new DB!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvux1rlj5adko5fmf8zil.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvux1rlj5adko5fmf8zil.png" alt="Image description" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Load sample data to practice with in the next guide. This will take some time.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7m2bgjxijewqdlr714iv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7m2bgjxijewqdlr714iv.png" alt="Image description" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;See you in the next guide!&lt;/strong&gt;&lt;br&gt;
Next guide will be posted soon!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29tf3ju79gaxlh72ouga.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29tf3ju79gaxlh72ouga.gif" alt="see ya" width="480" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Next.js Guide | About pages/</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Fri, 03 Feb 2023 16:25:20 +0000</pubDate>
      <link>https://dev.to/yankeedom/nextjs-guide-getting-started-1541</link>
      <guid>https://dev.to/yankeedom/nextjs-guide-getting-started-1541</guid>
      <description>&lt;h2&gt;
  
  
  Next.js Guide | About pages/
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guide Prerequisites:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Good Javascript knowledge&lt;/li&gt;
&lt;li&gt;Good Typescript knowledge&lt;/li&gt;
&lt;li&gt;Basic React knowledge&lt;/li&gt;
&lt;li&gt;Ensure Node.js 14.6.0 or newer is installed on your machine.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2zi9dpa6n37ld2m2a3q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2zi9dpa6n37ld2m2a3q.jpg" alt="Image description" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Follow me on my journey in learning &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt;.&lt;/strong&gt; These guide break down documentation into little bite size pieces!&lt;/p&gt;

&lt;p&gt;I was not born with the knowledge of Next.js, so in addition to hours of suffering, here is where additional wisdom of Next.js will come from:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://nextjs.org/docs" rel="noopener noreferrer"&gt;Next.js docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/full-stack-development-with-next-js-typescript/" rel="noopener noreferrer"&gt;Udemy Course&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/the-next-js-handbook/" rel="noopener noreferrer"&gt;The Nest.js Handbook&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This guide will use typescript, MongoDB for a database, and tailwind for UI. I also use VS Code.  &lt;/p&gt;

&lt;p&gt;I will likely add more information in the future as I come across good examples in code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jacobjpelletier/nextjs-guide-getting-started-2mc2"&gt;Previous Post&lt;/a&gt;&lt;br&gt;
Next Post (coming soon!)&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotce59hfqj85yqm1zve9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotce59hfqj85yqm1zve9.gif" alt="Routing" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  About Next.js Pages
&lt;/h2&gt;

&lt;p&gt;What does the pages directory do?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important note from docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Next.js 13 introduces the app/ directory (beta). This new directory has support for layouts, nested routes, and uses Server Components by default. Inside app/, you can fetch data for your entire application inside layouts, including support for more granular nested layouts (with colocated data fetching).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have not chosen to create a guide on the app directory at this time, but maybe in the future!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does the pages directory do?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;From handbook:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any URL is mapped to the filesystem, to files put in the pages folder, and you don't need any configuration (you have customization options of course).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What is a page?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Next.js, a page is a React Component exported from a .js, .jsx, .ts, or .tsx file in the pages directory. Each page is associated with a route based on its file name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2lz6dl4rrj90xxtlb1hq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2lz6dl4rrj90xxtlb1hq.png" alt="Image description" width="659" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you create pages/about.js that exports a React component like below, it will be accessible at /about.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzs12gr6048kkx5o1bjv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzs12gr6048kkx5o1bjv.png" alt="Image description" width="800" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is pre-rendering?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By default, Next.js pre-renders every page. This means that Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript. Pre-rendering can result in better performance and SEO.&lt;/p&gt;

&lt;p&gt;Each generated HTML is associated with minimal JavaScript code necessary for that page. When a page is loaded by the browser, its JavaScript code runs and makes the page fully interactive. (This process is called hydration.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;SSG vs SSR&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;SSG, or Static Site Generation is the recommended form of pre-rendering in the Next.js framework. This means that the HTML is generated at &lt;strong&gt;build time&lt;/strong&gt; and will be reused on each request for the resource.&lt;/li&gt;
&lt;li&gt;SSR, or Server Side Rendering on the other hand, is a form of pre-rendering where the HTML is generated on &lt;strong&gt;each request&lt;/strong&gt; of the resource.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiev7mvokbdlcdyqbk5co.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiev7mvokbdlcdyqbk5co.gif" alt="SSR" width="500" height="375"&gt;&lt;/a&gt;&lt;br&gt;
"A google crawler inspecting SSR"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Importantly, Next.js lets you &lt;strong&gt;choose&lt;/strong&gt; which pre-rendering form you'd like to use for each page. You can create a "hybrid" Next.js app by using Static Generation for most pages and using Server-side Rendering for others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We recommend using Static Generation&lt;/strong&gt; over Server-side Rendering for performance reasons. Statically generated pages can be cached by CDN with no extra configuration to boost performance. However, in some cases, Server-side Rendering might be the only option.&lt;/p&gt;

&lt;p&gt;You can also use &lt;strong&gt;Client-side data fetching&lt;/strong&gt; along with Static Generation or Server-side Rendering. That means some parts of a page can be rendered entirely by client side JavaScript. To learn more, take a look at the &lt;a href="https://nextjs.org/docs/basic-features/data-fetching/client-side" rel="noopener noreferrer"&gt;Data Fetching&lt;/a&gt; documentation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;When should I use SSG?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Whenever possible because it is much faster than rendering a page with every request.&lt;/li&gt;
&lt;li&gt;Marketing pages&lt;/li&gt;
&lt;li&gt;Blogs and portfolios&lt;/li&gt;
&lt;li&gt;E-commerce product listing&lt;/li&gt;
&lt;li&gt;Help and documentation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhrssh83je9lvjp954bk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhrssh83je9lvjp954bk.png" alt="Image description" width="566" height="639"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create A Blank Slate To Work From
&lt;/h2&gt;

&lt;p&gt;Before our experiments to learn more about pages and routing, I deleted the files &lt;code&gt;home.module.css&lt;/code&gt;, &lt;code&gt;_documents.tsx&lt;/code&gt;, and I deleted all the contents of &lt;code&gt;globals.css&lt;/code&gt; and replace the contents of &lt;code&gt;index.tsx&lt;/code&gt; with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Home() {
  return &amp;lt;div&amp;gt;Welcome to Next.js!&amp;lt;/div&amp;gt;
}

export default HomePage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before edits:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbbcbqe2v3g95erzauuwb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbbcbqe2v3g95erzauuwb.png" alt="Image description" width="742" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After edits:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5b9794iaxkxk0g74xxcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5b9794iaxkxk0g74xxcf.png" alt="Image description" width="615" height="562"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;localhost:3000 result:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwzr2359hxf4rll1cub5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwzr2359hxf4rll1cub5.png" alt="Image description" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will cover &lt;a href="https://dev.toPages%20with%20Dynamic%20Routes"&gt;pages with dynamics routes&lt;/a&gt; into its own guide.&lt;/p&gt;
&lt;h2&gt;
  
  
  Static Generation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a page uses Static Generation, the page HTML is generated at build time. That means in production, the page HTML is generated when you run next build . This HTML will then be reused on each request. It can be cached by a CDN.&lt;br&gt;
In Next.js, you can statically generate pages with or without data. Let's take a look at each case.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Static Generation Without Data
&lt;/h2&gt;

&lt;p&gt;At this time, add a new about.tsx page as shown above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifcy6bkzme7y4jmraf8h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifcy6bkzme7y4jmraf8h.png" alt="Image description" width="659" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the following code to the about.tsx page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function About() {
  return &amp;lt;div&amp;gt;About&amp;lt;/div&amp;gt;
}

export default About
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it, a statically generated site with no data. Well the journey here was exciting. The result? Maybe not so much. Let's make it more interesting with data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Generation With Data
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some pages require fetching external data for pre-rendering. There are two scenarios, and one or both might apply. In each case, you can use these functions that Next.js provides:&lt;/p&gt;

&lt;p&gt;Your page content depends on external data: Use getStaticProps.&lt;br&gt;
Your page paths depend on external data: Use getStaticPaths (usually in addition to getStaticProps).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Scenario 1: Your page content depends on external data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TODO: Need to fetch `posts` (by calling some API endpoint)
//       before this page can be pre-rendered.
export default function Blog({ posts }) {
  return (
    &amp;lt;ul&amp;gt;
      {posts.map((post) =&amp;gt; (
        &amp;lt;li&amp;gt;{post.title}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To fetch this data on pre-render, Next.js allows you to export an async function called getStaticProps from the same file. This function gets called at build time and lets you pass fetched data to the page's props on pre-render.&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;export default function Blog({ posts }) {
  // Render posts...
}

// This function gets called at build time
export async function getStaticProps() {
  // Call an external API endpoint to get posts
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  // By returning { props: { posts } }, the Blog component
  // will receive `posts` as a prop at build time
  return {
    props: {
      posts,
    },
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scenario 2: Your page paths depend on external data&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/docs/basic-features/pages#scenario-2-your-page-paths-depend-on-external-data" rel="noopener noreferrer"&gt;Dynamic routes&lt;/a&gt; will be covered in a guide of their own in the future!&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Side Rendering
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;If a page uses Server-side Rendering, the page HTML is generated on each request.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To use Server-side Rendering for a page, you need to export an async function called getServerSideProps. This function will be called by the server on every request.&lt;/p&gt;

&lt;p&gt;For example, suppose that your page needs to pre-render frequently updated data (fetched from an external API). You can write getServerSideProps which fetches this data and passes it to Page like below:&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;export default function Page({ data }) {
  // Render data...
}

// This gets called on every request
export async function getServerSideProps() {
  // Fetch data from external API
  const res = await fetch(`https://.../data`)
  const data = await res.json()

  // Pass data to the page via props
  return { props: { data } }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Static Generation (Recommended)&lt;/strong&gt;: The HTML is generated at build time and will be reused on each request. To make a page use Static Generation, either export the page component, or export getStaticProps (and getStaticPaths if necessary). It's great for pages that can be pre-rendered ahead of a user's request. You can also use it with Client-side Rendering to bring in additional data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server-side Rendering&lt;/strong&gt;: The HTML is generated on each request. To make a page use Server-side Rendering, export getServerSideProps. Because Server-side Rendering results in slower performance than Static Generation, use this only if absolutely necessary.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://dev.to/jacobjpelletier/nextjs-guide-getting-started-2mc2"&gt;Previous Post&lt;/a&gt;&lt;br&gt;
Next Post (coming soon!)&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>crypto</category>
      <category>web3</category>
      <category>cryptocurrency</category>
    </item>
    <item>
      <title>Next.js Guide | Getting Started</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Fri, 03 Feb 2023 15:43:17 +0000</pubDate>
      <link>https://dev.to/yankeedom/nextjs-guide-getting-started-2mc2</link>
      <guid>https://dev.to/yankeedom/nextjs-guide-getting-started-2mc2</guid>
      <description>&lt;h2&gt;
  
  
  Next.js Guide | Getting Started
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;br&gt;
Contact me for suggestions, comments, or just to say hello at &lt;a href="mailto:jacob@yankeedo.io"&gt;jacob@yankeedo.io&lt;/a&gt;! 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guide Prerequisites:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Good Javascript knowledge&lt;/li&gt;
&lt;li&gt;Good Typescript knowledge&lt;/li&gt;
&lt;li&gt;Basic React knowledge&lt;/li&gt;
&lt;li&gt;Ensure Node.js 14.6.0 or newer is installed on your machine.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2zi9dpa6n37ld2m2a3q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2zi9dpa6n37ld2m2a3q.jpg" alt="Image description" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Follow me on my journey in learning &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt;.&lt;/strong&gt; These guide break down documentation into little bite size pieces!&lt;/p&gt;

&lt;p&gt;I was not born with the knowledge of Next.js, so in addition to hours of suffering, here is where additional wisdom of Next.js will come from:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://nextjs.org/docs" rel="noopener noreferrer"&gt;Next.js docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/full-stack-development-with-next-js-typescript/" rel="noopener noreferrer"&gt;Udemy Course&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/the-next-js-handbook/" rel="noopener noreferrer"&gt;The Nest.js Handbook&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This guide will use typescript, MongoDB for a database, and tailwind for UI. I also use VS Code.  &lt;/p&gt;

&lt;p&gt;I will likely add more information in the future as I come across good examples in code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jacobjpelletier/nextjs-guide-getting-started-1541"&gt;Next Post&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbt4597y1lrj9t2lcssx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbt4597y1lrj9t2lcssx.gif" alt="Getting Started" width="480" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We recommend creating a new Next.js app using create-next-app, which sets up everything automatically for you. (You don't need to create an empty directory. create-next-app will make one for you.)...&lt;/p&gt;

&lt;p&gt;If you want to start with a TypeScript project you can use the --typescript flag:&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;npx create-next-app@latest --typescript
# or
yarn create next-app --typescript
# or
pnpm create next-app --typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Run npm run dev or yarn dev or pnpm dev to start the development server on &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;br&gt;
Visit &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; to view your application&lt;br&gt;
Edit pages/index.js and see the updated result in your browser&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The installer will then start the install, and will ask you a series of configuration questions. See how I set up my project below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fto76esio2xb36u863dbz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fto76esio2xb36u863dbz.png" alt="Image description" width="744" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the installation is complete, navigate into the project folder and run &lt;code&gt;npm run dev&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7l5ijj41b132ku1twnug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7l5ijj41b132ku1twnug.png" alt="Image description" width="730" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can verify that the base project is working by visiting &lt;code&gt;localhost:3000&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0zhbiew32zaur8zu1tqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0zhbiew32zaur8zu1tqu.png" alt="Image description" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Checkout the default scripts in the &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;These scripts refer to the different stages of developing an application:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dev&lt;/strong&gt; - Runs next dev to start Next.js in development mode&lt;br&gt;
&lt;strong&gt;build&lt;/strong&gt; - Runs next build to build the application for production usage&lt;br&gt;
&lt;strong&gt;start&lt;/strong&gt; - Runs next start to start a Next.js production server&lt;br&gt;
&lt;strong&gt;lint&lt;/strong&gt; - Runs next lint to set up Next.js' built-in ESLint configuration&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiy616s1gyog5wj8ajdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiy616s1gyog5wj8ajdj.png" alt="Image description" width="781" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also note the pages and public folders at the root of the project directory. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;Next.js is built around the concept of pages.&lt;/u&gt; A page is a React Component exported from a .js, .jsx, .ts, or .tsx file in the pages directory. You can even add dynamic route parameters with the filename.&lt;/p&gt;

&lt;p&gt;Inside the pages directory, add the index.js file to get started. This is the page that is rendered when the user visits the root of your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pages&lt;/strong&gt; - Associated with a route based on their file name. For example, pages/about.js is mapped to /about&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;public&lt;/strong&gt; - Stores static assets such as images, fonts, etc. Files inside public directory can then be referenced by your code starting from the base URL (/).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Notice that I also choose to start with a &lt;a href="https://nextjs.org/docs/advanced-features/src-directory" rel="noopener noreferrer"&gt;/src directory&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pages can also be added under src/pages as an alternative to the root pages directory.&lt;/p&gt;

&lt;p&gt;The src directory is very common in many apps and Next.js supports it by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Important notes on /src directory:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From docs:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;src/pages will be ignored if pages is present in the root directory&lt;/p&gt;

&lt;p&gt;Config files like next.config.js and tsconfig.json, as well as environment variables, should be inside the root directory, moving them to src won't work. Same goes for the public directory&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;A guide on how to use environmental variable with Next.js and Typescript is coming up!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There you have it. You are ready to start your first Next.js project!&lt;/p&gt;

&lt;p&gt;Thank you for stopping by! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jacobjpelletier/nextjs-guide-getting-started-1541"&gt;Next Post&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>web3</category>
      <category>crypto</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Two Pointers - Sum of Three Values</title>
      <dc:creator>Jacob Pelletier</dc:creator>
      <pubDate>Fri, 03 Feb 2023 05:12:16 +0000</pubDate>
      <link>https://dev.to/yankeedom/two-pointers-palindromes-17m4</link>
      <guid>https://dev.to/yankeedom/two-pointers-palindromes-17m4</guid>
      <description>&lt;h2&gt;
  
  
  Two Pointers Pattern | Sum of Three Values | For Beginners
&lt;/h2&gt;

&lt;p&gt;by Jacob Pelletier&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flaq0izaak98rbi7vlpjx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flaq0izaak98rbi7vlpjx.jpeg" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me as I document my journey as I (quickly) work through &lt;a href="https://www.educative.io/courses/grokking-coding-interview-patterns-javascript" rel="noopener noreferrer"&gt;the 24 patterns&lt;/a&gt; of Leet Code Questions in Javascript as a nice refresher for myself. This resource, in addition to &lt;a href="https://leetcode.com/" rel="noopener noreferrer"&gt;Leet Code&lt;/a&gt;, is excellent preparation for any interview!&lt;/p&gt;

&lt;p&gt;I am no Ph.D. in C.S. (just with a lowly B.S.) and this isn't meant to be an authoritative or heavily theoretical guide. This is guide is meant to be fun, fast, and casual.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Each article in this series will seek to &lt;u&gt;plainly&lt;/u&gt; explain:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The pattern&lt;/li&gt;
&lt;li&gt;The problem&lt;/li&gt;
&lt;li&gt;The solution in JS&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If anyone at all ends up reading these things I will post solutions in TS in the future! 😜&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Additional problems common of the two pointer pattern, in addition to sum of three values, are:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Two Pointers&lt;/li&gt;
&lt;li&gt;Reverse Words in a String&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Explaining The Sum of Three Values Problem.
&lt;/h2&gt;

&lt;p&gt;The Sum of Three Values Problem is a classic computer science problem. It involves finding three numbers from an array of numbers such that the sum of these three numbers is equal to a given value. We consider solving this problem using the Two Pointer method as discussed in the course &lt;a href="https://www.educative.io/courses/grokking-coding-interview-patterns-javascript" rel="noopener noreferrer"&gt;the 24 patterns&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Given an array of integers, nums, and an integer value, target, determine if there are any three integers in nums whose sum equals the target. Return TRUE if three such integers are found in the array. Otherwise, return FALSE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yt94p31ylynkhrtanc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yt94p31ylynkhrtanc5.png" alt="Image description" width="763" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to solve:&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sort the array in ascending order.&lt;/li&gt;
&lt;li&gt;Loop through the entire array and set up two pointers (low and high) on every iteration.&lt;/li&gt;
&lt;li&gt;Low is set to the current loop index + 1, and high is set to the last index of the array.&lt;/li&gt;
&lt;li&gt;In an inner loop, look for values that, along with the current element, sum up to target.&lt;/li&gt;
&lt;li&gt;Calculate the sum of array elements pointed to by the current loop index, and the low and high pointers.&lt;/li&gt;
&lt;li&gt;If the sum is equal to target, return TRUE.&lt;/li&gt;
&lt;li&gt;Else if the sum is greater than target, move the high pointer backwards.&lt;/li&gt;
&lt;li&gt;Else if the sum is less than target, move the low pointer forwards.&lt;/li&gt;
&lt;li&gt;Terminate the inner loop when low reaches r crosses high. Reset low to the current loop element +1, and reset high to the last index.&lt;/li&gt;
&lt;li&gt;If, after processing the entire array, we don't find a triple that matches our requirement, we return FALSE.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Problem #1 With Solution
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Problem #1:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My Solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&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;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Space:&lt;/strong&gt; O(n)&lt;br&gt;
&lt;strong&gt;Time:&lt;/strong&gt; O(1)&lt;/p&gt;

&lt;p&gt;Thanks for tuning in, more algo content on the way as I move through it myself!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
