<?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: Pepper.</title>
    <description>The latest articles on Forem by Pepper. (@peppermints).</description>
    <link>https://forem.com/peppermints</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%2F772866%2Fa637ae5e-f0b1-4971-859e-32051d0f9587.png</url>
      <title>Forem: Pepper.</title>
      <link>https://forem.com/peppermints</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/peppermints"/>
    <language>en</language>
    <item>
      <title>Using MongoDB with Node.JS</title>
      <dc:creator>Pepper.</dc:creator>
      <pubDate>Sun, 12 Dec 2021 22:33:22 +0000</pubDate>
      <link>https://forem.com/peppermints/using-mongodb-with-nodejs-4n94</link>
      <guid>https://forem.com/peppermints/using-mongodb-with-nodejs-4n94</guid>
      <description>&lt;p&gt;First, create a new project.&lt;/p&gt;

&lt;p&gt;CD into the project folder and run &lt;code&gt;npm init&lt;/code&gt;. Follow those steps until you're done.&lt;/p&gt;

&lt;p&gt;Run: &lt;code&gt;npm i mongodb&lt;/code&gt;. This will install the official MongoDB driver for Node.&lt;/p&gt;

&lt;p&gt;Create an &lt;code&gt;index.js&lt;/code&gt;, or &lt;code&gt;main.js&lt;/code&gt;, depending on your main file when you ran &lt;code&gt;npm init&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Inside there: add this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {MongoClient} = require("mongodb");
const mongouri = 'mongodb://your_connection_string';
const client = new MongoClient(mongouri);

client.connect().then(console.log("Connected to MongoDB"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats, if you run &lt;code&gt;node .&lt;/code&gt;, you should see 'Connected to MongoDB'.&lt;/p&gt;

&lt;p&gt;Let's create a quick question database by using an asynchronous function. Add this above &lt;code&gt;client.connect().then(console.log("Connected to MongoDB"));&lt;/code&gt; and under the constants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function createListing(db, collection, data) {
    await client.db(db).collection(collection).insertOne(data);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, under &lt;code&gt;client.connect(...&lt;/code&gt;, put:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createListing('question', 'questions', {
    question: "What's 2+2?",
    answer: 4
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go ahead and run &lt;code&gt;node .&lt;/code&gt;. If you have access to your database, you should see that listing in the database.&lt;/p&gt;

&lt;p&gt;Let's read a listing and compare an answer by creating another asynchronous function. Under the 'createListing' function, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function readListing(db, collection, data) {
    const result = await client.db(db).collection(collection).findOne(data);
    if(result === null || result === undefined) {
        return false;
    }
    return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, let's remove the lines where we created our listing, and we will replace it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let guess = 4;
const res = await readListing('question', 'questions', {
    answer: guess
});
if(res === false) {
    console.log("Oops, you got it wrong.");
} else {
    console.log("Yay! You got it right!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now, we will run &lt;code&gt;node .&lt;/code&gt;, it should output: "Yay! You got it right!"&lt;/p&gt;

&lt;p&gt;Congratulations! You've just created and read data from a database!&lt;/p&gt;

&lt;p&gt;To the beginners: Keep learning. You never know what you can accomplish if you keep putting your all into it. This tutorial has just showed you how to use one of the BEST databases out there, very easily. So go and do what we all beginners &lt;em&gt;should&lt;/em&gt; do, keep learning, and keep attempting new things. Good luck!&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
