<?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>Forem: Kartik Budhiraja</title>
    <description>The latest articles on Forem by Kartik Budhiraja (@kartikbudhiraja).</description>
    <link>https://forem.com/kartikbudhiraja</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%2F162904%2F686c0add-4aa1-42b1-a439-9656db6a9694.jpeg</url>
      <title>Forem: Kartik Budhiraja</title>
      <link>https://forem.com/kartikbudhiraja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kartikbudhiraja"/>
    <language>en</language>
    <item>
      <title>Figuring it out no matter what ~ Leo Lima</title>
      <dc:creator>Kartik Budhiraja</dc:creator>
      <pubDate>Mon, 22 Feb 2021 13:29:18 +0000</pubDate>
      <link>https://forem.com/kartikbudhiraja/figuring-it-out-no-matter-what-leo-lima-59hf</link>
      <guid>https://forem.com/kartikbudhiraja/figuring-it-out-no-matter-what-leo-lima-59hf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZjgvrGWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/1024/1%2Alh5IKEOmPEErtaHNnZP5dw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZjgvrGWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/1024/1%2Alh5IKEOmPEErtaHNnZP5dw.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bringing you the next episode of Everyone Sucks, I (&lt;a href="https://medium.com/u/56d3cb6a43ed"&gt;Kartik Budhiraja&lt;/a&gt;) had the honour to have &lt;a href="https://medium.com/u/5ad35097ded0"&gt;Leonardo Lima&lt;/a&gt; as the guest for this episode. Leo is from Brazil and is currently working as a Senior Software Engineer at a crypto marketplace company.&lt;/p&gt;

&lt;p&gt;His story of being in development is really fascinating, as he studied journalism but pivoted into software development later. He talks about how many challenges he faced not being from a computer science background and how he was able to find what he loves and gets better at it.&lt;/p&gt;

&lt;p&gt;Some key takeaways:&lt;/p&gt;

&lt;h3&gt;
  
  
  Never complain
&lt;/h3&gt;

&lt;p&gt;Leo provides us all with an example of making the best of our resources without complaining. For his very first internship interview (which he got by sending a cold-email to the founder of the company), he was given a coding assignment. Fortune playing its usual role, Leo was moving at that time and ended up being without an internet connection at his place with an assignment to do in a programming language he did not have experience in ( If you’re a developer, you can imagine how hard can that be). But instead of complaining, Leo actually spent his days at the public library as they had internet and finished the assessment on time.&lt;/p&gt;

&lt;p&gt;Talk about finding a way out no matter what.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build and use the support network around you
&lt;/h3&gt;

&lt;p&gt;When asked about his way of handling the pressure of moving into tech and dealing with the imposter syndrome, he talks about the importance of having a support network around oneself. He mentions how he used to talk with his best friend and other people around him openly about the challenges he’s facing and the doubts he has.&lt;/p&gt;

&lt;p&gt;He puts it in a really impactful way by saying that “You do not have to fight alone. Help people around you and ask for help when you need it.”&lt;/p&gt;

&lt;h3&gt;
  
  
  Transform self-doubt to fuel
&lt;/h3&gt;

&lt;p&gt;At his first development job, Leo talks about how he always felt the pressure to prove himself. Not being from a computer science background, the self-doubt added additional pressure as one could imagine. Leo turned that self-doubt around and started using that as fuel for more focus. He talks about how he ended up taking his work with intense focus and putting in more hours just because he knew he had something to prove. It’s not to say that the pressure is good, it’s more to show that everyone feels that self-doubt, but the only thing we need to do with it eradicates it by focusing on what we can do i.e get after it and work with more focus.&lt;/p&gt;

&lt;h3&gt;
  
  
  It’s always a people business
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“No matter what your role is, you have to remember it’s always a people business” ~ Leo&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Leo tells how calling people as resources make him a bit uncomfortable as people are human, no matter what happens. He explains how important is it to remember this whether you’re a developer or in any other job. At the end of the day you are still working and communicating with people, so behave like one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Focus on what other person requires, not what you want to offer
&lt;/h3&gt;

&lt;p&gt;When asked about his favourite failure, he talks about the importance of understanding other people’s needs. Through his own experience where he tried to help an intern by providing what he &lt;em&gt;thought&lt;/em&gt; would be the best for them. He explains how that backfired and reminded him that every person is different, just because you think that one thing would benefit that person, does not mean they would agree with you.&lt;/p&gt;

&lt;p&gt;If you’re in a management role, take a step to figure out what your team wants first, and then work on providing them. Do not put the cart in front of the horse.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bonus Tip&lt;/em&gt;: Coming from Journalism, writing is a major part of Leo’s life. He uses writing down the problem and its prospective solution to break it down into chunks and suggests everyone give it a shot.&lt;/p&gt;

&lt;p&gt;Please feel free to reach out to Leo if anyone needs any guidance. You can find Leo online on Twitter at @leocavia.&lt;/p&gt;

&lt;p&gt;We talked about a lot of other cool stuff, join us on youtube, Spotify, and every other place you get your podcast from. (Search for Everyone Sucks)&lt;/p&gt;

&lt;p&gt;Spotify: &lt;a href="https://open.spotify.com/episode/6sm92aEs2vO8ES3KfegM3E?si=oHd4uv6qROKk5MBxH6pcdg"&gt;https://open.spotify.com/episode/6sm92aEs2vO8ES3KfegM3E?si=oHd4uv6qROKk5MBxH6pcdg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xbdXiHxth3w"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




</description>
      <category>motivation</category>
      <category>frontend</category>
      <category>podcast</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>A journey of resilience with Jeffrey Chang</title>
      <dc:creator>Kartik Budhiraja</dc:creator>
      <pubDate>Thu, 10 Dec 2020 14:08:44 +0000</pubDate>
      <link>https://forem.com/kartikbudhiraja/a-journey-of-resilience-with-jeffrey-chang-3bbf</link>
      <guid>https://forem.com/kartikbudhiraja/a-journey-of-resilience-with-jeffrey-chang-3bbf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I8tuDw98--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AHzN7KwW8OcYEUvP6nOSkUg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I8tuDw98--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AHzN7KwW8OcYEUvP6nOSkUg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This weekend I got an opportunity to chat with @Jeffrey Chang about his journey.&lt;/p&gt;

&lt;p&gt;From working at a bakery to working at an auto shop to becoming a software developer at Telus Digital, he walks us through all of the ups and down’s he went through.&lt;/p&gt;

&lt;p&gt;To me personally, Jeffrey’s story stands out as a journey of resilience. A prime example of what determination and grinding it out can do in life.&lt;/p&gt;

&lt;p&gt;Some key takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Give yourself a shot and take a leap of faith (Jeffrey decided to invest $10,000 for the Bootcamp even when his financial situation was not that easy)&lt;/li&gt;
&lt;li&gt;Stick with it until it clicks, and trust me, it will click soon enough ~ Jeffrey&lt;/li&gt;
&lt;li&gt;Find the positive out of the negative (Jeffrey talked about his “failed” tech interview, which he turned into a positive as it provided him with the desire to learn)&lt;/li&gt;
&lt;li&gt;“Be a sponge and soak up all the knowledge you can” ~ Jeffrey Chang 😛&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some special advice for new grads/juniors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contribute to open source as much as you can. Ask yourself “What can I do to make this project better?”( Additional perk: Get your code reviewed by Senior dev’s around the world for free 😎 )&lt;/li&gt;
&lt;li&gt;Don’t spread yourself too thin, focus on one thing at a time. No more trying to learn docker and Vue.js at the same time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And finally my favourite quote from the conversation:&lt;/p&gt;

&lt;p&gt;“You’re not an idiot, you just do not understand it at the moment” ~ Jeffrey&lt;/p&gt;

&lt;p&gt;Please feel free to reach out to Jeffrey if anyone needs any help. He provides his email in the conversation.&lt;/p&gt;

&lt;p&gt;We talked about a lot of other cool stuff, join us on youtube, Spotify, and every other place you get your podcast from. (Search for Everyone Sucks)&lt;/p&gt;

&lt;p&gt;Spotify: &lt;a href="https://open.spotify.com/episode/7tJlRnv4x9wKC0RbKCAYlg?si=4RHIGYp7Rnu450zoFtBkLg"&gt;https://open.spotify.com/episode/7tJlRnv4x9wKC0RbKCAYlg?si=4RHIGYp7Rnu450zoFtBkLg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apple Podcasts: &lt;a href="https://podcasts.apple.com/ca/podcast/everyone-sucks/id1477730495?i=1000501827490"&gt;https://podcasts.apple.com/ca/podcast/everyone-sucks/id1477730495?i=1000501827490&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/JiyFK5q4Ag0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




</description>
      <category>toronto</category>
      <category>motivation</category>
      <category>codingbootcampstuden</category>
      <category>podcast</category>
    </item>
    <item>
      <title>Create horizontal scroll list with ScrollSpy in React</title>
      <dc:creator>Kartik Budhiraja</dc:creator>
      <pubDate>Fri, 08 May 2020 14:01:48 +0000</pubDate>
      <link>https://forem.com/kartikbudhiraja/create-horizontal-scroll-list-in-react-1h62</link>
      <guid>https://forem.com/kartikbudhiraja/create-horizontal-scroll-list-in-react-1h62</guid>
      <description>&lt;p&gt;A horizontal scroll list can be implemented easily, thanks to the flexbox. The tricky and fun part is not only highlighting the active heading as well as bringing it into view as the user scrolls down the page.&lt;/p&gt;

&lt;p&gt;In this blog, we will discuss how to get this user experience.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Libraries used: &lt;a href="https://github.com/fisshy/react-scroll"&gt;react-scroll&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's assume we have a row of headings.&lt;/p&gt;

&lt;p&gt;These headings are embedded in Link from react-scroll and scroll down to respective part on click.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RuQCzNxS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AsqyF7KNMoBLs55-4XrSFUQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RuQCzNxS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AsqyF7KNMoBLs55-4XrSFUQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React Scroll has the following prop which would make it super easy to set an active category:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;onSetActive — invoke whenever link is being set to active&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The issue to solve inside this prop is to get the active category to be visible on the screen if the user scrolls down to it.&lt;/p&gt;

&lt;p&gt;To get an element into the view, we would use:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView"&gt;Element.scrollIntoView()&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To get access to the DOM element of the active category, we will use refs. It’s the recommended way of getting to access DOM nodes or React elements created in the render method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;We need to store the ref to the&lt;/em&gt; &lt;em&gt;&lt;li&gt; item, and then call&lt;/li&gt;&lt;/em&gt; &lt;em&gt;scrollIntoView() on the element when the user reaches that category.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we have multiple categories, we would need to store refs in an array. But as I experimented with it, I found out for some reason, refs get wiped out when stored in an array.&lt;/p&gt;

&lt;p&gt;With some research I found this comment:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9aWdA5lH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/801/1%2ACZOoOfypKQx5X6e4OJWaWw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9aWdA5lH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/801/1%2ACZOoOfypKQx5X6e4OJWaWw.png" alt=""&gt;&lt;/a&gt;Source → (&lt;a href="https://github.com/facebook/react/issues/1899"&gt;Array of references&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Storing each ref as string worked, but considering the comment was made in 2014, and react has evolved a lot since I was a bit skeptical.&lt;/p&gt;

&lt;p&gt;And even though it worked, I got the following warning in console:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warning: A string ref, “1”, has been found within a strict mode tree. String refs are a source of potential bugs and should be avoided. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: &lt;a href="https://fb.me/react-strict-mode-string-ref"&gt;https://fb.me/react-strict-mode-string-ref&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also in the &lt;a href="https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs"&gt;documentation&lt;/a&gt; for refs, it is clearly stated:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;We advise against it because string refs have some issues, are considered legacy, and are likely to be removed in one of the future releases.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The array does not work, and storing string ref is not an elegant solution.&lt;/p&gt;

&lt;p&gt;So I thought of creating refs for each category heading using React.createRef() in the constructor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sY8ePy7V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A5UM54_Y5mL34MA7BT96a7Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sY8ePy7V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A5UM54_Y5mL34MA7BT96a7Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And it worked like a charm, we can access the element in the following way in the callback of setting active category:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o4G4YAlj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AUSXEqgWfo6M6ldA7q-0s_A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o4G4YAlj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AUSXEqgWfo6M6ldA7q-0s_A.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the refs can be added to the respective DOM element like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ozj9n1OJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/942/1%2AtT7BOUyV-xvk7tCyEM2Uag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ozj9n1OJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/942/1%2AtT7BOUyV-xvk7tCyEM2Uag.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And Voila!! 🤩&lt;/p&gt;

&lt;p&gt;🥳 There we have our horizontal scroll list, scrolling the active heading into view as the user scrolls down.&lt;/p&gt;

&lt;p&gt;This is the code sandbox for a working example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/laughing-wood-fgusk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Thank you for reading. If you have any questions or suggestions, please let me know in the comment box.&lt;/p&gt;

</description>
      <category>react</category>
      <category>programming</category>
      <category>reactscroll</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to connect Go with MongoDB using official mongodb-go-driver!</title>
      <dc:creator>Kartik Budhiraja</dc:creator>
      <pubDate>Sat, 23 Mar 2019 11:22:19 +0000</pubDate>
      <link>https://forem.com/kartikbudhiraja/how-to-connect-go-with-mongodb-using-official-mongodb-go-driver-5a59</link>
      <guid>https://forem.com/kartikbudhiraja/how-to-connect-go-with-mongodb-using-official-mongodb-go-driver-5a59</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F960%2F1%2AB4CJ2CIgFv3J2TDEu-8A8A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F960%2F1%2AB4CJ2CIgFv3J2TDEu-8A8A.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On December 13, 2018 MongoDB released its &lt;a href="https://github.com/mongodb/mongo-go-driver/" rel="noopener noreferrer"&gt;official Go Driver&lt;/a&gt; into beta, ready for the wider Go and MongoDB community to put it into test.🤩 MongoDB GO driver is pretty easy and quick to set up and is still &lt;a href="https://jira.mongodb.org/projects/GODRIVER/issues/GODRIVER-861?filter=allopenissues" rel="noopener noreferrer"&gt;WIP&lt;/a&gt; but is improving functionalities and features day by day. This tutorial is based on the official tutorial released by MongoDB, but I tried to make it as simple as possible along with a working reference.&lt;/p&gt;

&lt;p&gt;TLDR: The source code can be found &lt;a href="https://github.com/kartik-budhiraja/go-tut-mongodb-go-driver" rel="noopener noreferrer"&gt;here&lt;/a&gt; with CRUD operations&lt;/p&gt;

&lt;p&gt;In this tutorial, we will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the MongoDB Go Driver&lt;/li&gt;
&lt;li&gt;Connect the MongoDB using the Go Driver&lt;/li&gt;
&lt;li&gt;Use of BSON Objects in Go&lt;/li&gt;
&lt;li&gt;Perform CRUD Operations&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Installing the MongoDB Go Driver&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The MongoDB Go Driver is made up of several packages. If you are just using go get, you can install the driver using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get github.com/mongodb/mongo-go-driver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of this may look like a warning stating something like package github.com/mongodb/mongo-go-driver: no Go files in (...). This is an expected output.&lt;/p&gt;

&lt;p&gt;If you are using &lt;strong&gt;&lt;em&gt;govendor&lt;/em&gt;&lt;/strong&gt; package manager, you need to install the main mongo package as well as the bson and mongo/options package using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;govendor fetch github.com/mongodb/mongo-go-driver/mongo

govendor fetch go.mongodb.org/mongo-driver/bson

govendor fetch go.mongodb.org/mongo-driver/mongo/options
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of the required packages should get installed, now let's build up a connection to Mongo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Setting up the connection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the MongoDB Go Driver has been imported, we can connect to a MongoDB deployment using the Client.Connect(context).We need to set up a new client, we need to pass the URI of our mongo database while setting up the client. Then we just need to call client.Connect(context) with the context, which will set up our connection.&lt;/p&gt;

&lt;p&gt;The following code sets up a connection for us:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For keeping it simple, I would only be providing the code snippets of the functions being used, you may find the complete source code &lt;a href="https://github.com/kartik-budhiraja/go-tut-mongodb-go-driver" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Set up a context required by mongo.Connect
ctx, cancel := context.WithTimeout(context.Background(), 10\*time.Second)

//To close the connection at the end
defer cancel()

//We need to set up a client first
//It takes the URI of your database
client, error := mongo.NewClient(options.Client().ApplyURI("your\_database\_uri"))

if error != nil {
 log.Fatal(err)
}

//Call the connect function of client
error = client.Connect(ctx)

//Checking the connection
error = client.Ping(context.TODO(), nil)
fmt.Println("Database connected")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Using BSON Objects in Go&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JSON documents in MongoDB are stored in a binary representation called BSON (Binary-encoded JSON). Unlike other databases that store JSON data as simple strings and numbers, the BSON encoding extends the JSON representation to include additional types such as int, long, date, floating point, and decimal128. This makes it much easier for applications to reliably process, sort, and compare data. The Go Driver has two families of types for representing BSON data: The D types and the Raw types.&lt;/p&gt;

&lt;p&gt;The D family of types is used to concisely build BSON objects using native Go types. This can be particularly useful for constructing commands passed to MongoDB. The D family consists of four types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;D: A BSON document. This type should be used in situations where order matters, such as MongoDB commands.&lt;/li&gt;
&lt;li&gt;M: An unordered map. It is the same as D, except it does not preserve order.&lt;/li&gt;
&lt;li&gt;A: A BSON array.&lt;/li&gt;
&lt;li&gt;E: A single element inside a D.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of a filter document built using D types which may be used to find documents where the name field matches either Alice or Bob:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bson.D{{
 "name", 
 bson.D{{
 "$in", 
 bson.A{"Alice", "Bob"}
 }}
}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. CRUD Operations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For CRUD and other operations, we need to use the collection object, which we can create by referencing to our respective collection in our database like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BooksCollection := client.Database("test").Collection("books")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Insert&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For create, we can use collection.InsertOne() for individual entry or could use collection.InsertMany() which takes in the slice of objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/\*\*
\* Create - Adding a new book
\* res -\&amp;gt; the insert command returns the inserted id of the oject
\*/

res, err := BooksCollection.InsertOne(ctx, bson.M{"name": "The Go Language", "genre": "Coding", "authorId": "4"})

if err != nil {
log.Fatal(err)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In collection.InsertOne(), we can either pass a string object through bson.M{} or we could create an object of our respective type struct and pass that object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Read&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For finding documents, we need a filter document as well as a pointer to a value into which result can be decoded. To find a single document, use collection.FindOne(). This method returns a single result which can be decoded into a value. The filter object specifies what are we looking for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter := bson.D{{"name", "Book 1"}}

// create a value into which the result can be decoded
var result bookType

err = collection.FindOne(context.TODO(), filter).Decode(&amp;amp;result)
if err != nil {
 log.Fatal(err)
}

fmt.Printf("Found a single Book: %+v\n", result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find multiple documents, use collection.Find(). This method returns a Cursor. A Cursor provides a stream of documents through which we can iterate and decode one at a time. Once a Cursor has been exhausted, we should close the Cursor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cur, error := BooksCollection.Find(ctx, bson.D{{}})

var allbooks []\*bookType

//Loops over the cursor stream and appends to result array
for cur.Next(context.TODO()) {
var booksResultHolder bookType

err := cur.Decode(&amp;amp;bookResultHolder)

if err != nil {
log.Fatal(err)
}

allbooks = append(allbooks, &amp;amp;booksResultHolder)
}

//dont forget to close the cursor
defer cur.Close(context.TODO())

// Loop over the result array and perform whatever required
for \_, element := range allbooks {
book := \*element
fmt.Println(book)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;If you get empty results back, check your fields of type structures, you must export fields of structs, else they are ignored by the mongodb-go-driver package. (Each field should start with Capital letter, specifying go that it could be exported)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;type users struct {&lt;br&gt;&lt;br&gt;
User string &lt;code&gt;bson:"user" json:"user"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Data string &lt;code&gt;bson:"data" json:"data"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;By default when a struct value is transformed/stored/retrieved from MongoDB, the field name is used. If you want to use different names, you may use &lt;a href="https://stackoverflow.com/questions/10858787/what-are-the-uses-for-tags-in-go/30889373#30889373" rel="noopener noreferrer"&gt;&lt;em&gt;tags&lt;/em&gt;&lt;/a&gt; to tell what names should the fields map to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your documents have automatically generate id’s, at the moment you won’t be able to get those id’s through mongodb-go-server , this is because the id’s are strings wrapped around in custom objects, and at the moment, the driver does not support them. More detailed info can be found &lt;a href="https://stackoverflow.com/questions/52024532/using-mongodb-go-driver-for-decoding-documents-into-structs-with-custom-type-fie" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The collection.UpdateOne() method allows you to update a single document. It requires a filter document to match documents in the database and an update document to describe the update operation. These can be built just how we made the filter object while reading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/\*\*
\* Update
\* Collection has functions like UpdateOne and UpdateMany
\* Returns the Matched and Modified Count
\*/

filter := bson.D{{"name", "Book 1"}}

// Need to specify the mongodb output operator too
newName := bson.D{
{"$set", bson.D{
{"name", "Updated Name of Book 1"},
}},
}

res, err := BooksCollection.UpdateOne(ctx, filter, newName)

if err != nil {
log.Fatal(err)
}

updatedObject := \*res

fmt.Printf("The matched count is : %d, the modified count is : %d", updatedObject.MatchedCount, updatedObject.ModifiedCount)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Delete&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we can delete documents using collection.DeleteOne() or collection.DeleteMany(). Here we can pass nil as the filter argument, which will match all documents in the collection or any other specific argument. We could also use &lt;a href="https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Drop" rel="noopener noreferrer"&gt;collection.Drop()&lt;/a&gt; to delete an entire collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter = bson.D{{"name", "Updated Name of Book 2"}}

deleteResult, error := BooksCollection.DeleteOne(ctx, filter)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Next Steps
&lt;/h3&gt;

&lt;p&gt;The source code for this tutorial can be found &lt;a href="https://github.com/kartik-budhiraja/go-tut-mongodb-go-driver" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Documentation for the MongoDB Go Driver is available on &lt;a href="https://godoc.org/github.com/mongodb/mongo-go-driver" rel="noopener noreferrer"&gt;GoDoc&lt;/a&gt;. You may be particularly interested in the documentation about using &lt;a href="https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Aggregate" rel="noopener noreferrer"&gt;aggregations&lt;/a&gt; or &lt;a href="https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Session" rel="noopener noreferrer"&gt;transactions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope this tutorial made things a bit simpler for you, Happy coding everyone!👨🏽‍💻🍻&lt;/p&gt;




</description>
      <category>programming</category>
      <category>go</category>
      <category>mongodb</category>
      <category>golangtutorial</category>
    </item>
  </channel>
</rss>
