<?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: opeyemi mathew adewumi</title>
    <description>The latest articles on DEV Community by opeyemi mathew adewumi (@adewunmi01).</description>
    <link>https://dev.to/adewunmi01</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%2F1408884%2F5d7f340a-6869-4957-a0cd-dd2ae5f76f3b.jpg</url>
      <title>DEV Community: opeyemi mathew adewumi</title>
      <link>https://dev.to/adewunmi01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adewunmi01"/>
    <language>en</language>
    <item>
      <title>Setting up MongoDB using Mongoose in Node.js</title>
      <dc:creator>opeyemi mathew adewumi</dc:creator>
      <pubDate>Mon, 20 May 2024 13:49:13 +0000</pubDate>
      <link>https://dev.to/adewunmi01/setting-up-mongodb-using-mongoose-in-nodejs-59fg</link>
      <guid>https://dev.to/adewunmi01/setting-up-mongodb-using-mongoose-in-nodejs-59fg</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;How to Set up MongoDB using Mongoose in Node.js&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;MongoDB is a popular NoSQL database that allows for flexible and efficient data storage. Mongoose is a popular ODM (Object Data Modeling) library for MongoDB that makes it easy to interact with the database from Node.js applications. In this article, we will go through the steps to set up MongoDB using Mongoose in Node.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Step 1: Install MongoDB&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;First, we need to have MongoDB installed on our machine. You can download the Community Server edition from the official MongoDB website if you don't have one yet. Follow the installation instructions for your operating system.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Step 2: Install Mongoose&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once you have successfully install MongoDB, you need to install Mongoose in your Node.js project. You can do this using npm by running the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Step 3: Establish a Connection&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Next, we have to create a new JavaScript file and require Mongoose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, use the &lt;code&gt;mongoose.connect()&lt;/code&gt; method to connect to your MongoDB database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongodb://localhost/mydatabase&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace 'mongodb://localhost/mydatabase' with the connection string to your MongoDB database.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Step 4: Define a Data Structure&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After connecting to database, define a schema for your data using the &lt;code&gt;mongoose.Schema&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Step 5: Create a Model&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once you have defined a schema, you can create a model using the &lt;code&gt;mongoose.model()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Step 6: Use the Model&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Finally, you can use the model to interact with your database. For example, you can create a new user document using the create() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;johndoe@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new user document in your database.&lt;br&gt;
By following these steps, you'll be up and running with MongoDB and Mongoose in Node.js, ready to build powerful and scalable applications.&lt;/p&gt;

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