<?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: Aruya Temitayo</title>
    <description>The latest articles on DEV Community by Aruya Temitayo (@wstrguy).</description>
    <link>https://dev.to/wstrguy</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%2F1018534%2F3d8f4a16-8310-4a0a-85b4-f3f6fbd03306.png</url>
      <title>DEV Community: Aruya Temitayo</title>
      <link>https://dev.to/wstrguy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wstrguy"/>
    <language>en</language>
    <item>
      <title>Setting up MongoDB using Mongoose in Nodejs</title>
      <dc:creator>Aruya Temitayo</dc:creator>
      <pubDate>Sat, 18 Mar 2023 14:34:24 +0000</pubDate>
      <link>https://dev.to/wstrguy/setting-up-mongodb-using-mongoose-in-nodejs-2d0j</link>
      <guid>https://dev.to/wstrguy/setting-up-mongodb-using-mongoose-in-nodejs-2d0j</guid>
      <description>&lt;p&gt;Node.js has emerged as a popular server-side technology used for building scalable and efficient web applications. One of the critical components of any web application is a database. MongoDB is a popular NoSQL database that is known for its flexibility and scalability. In this article, we will look at how to set up MongoDB using Mongoose in Node.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Mongoose?
&lt;/h3&gt;

&lt;p&gt;Mongoose is a Node.js-based Object Data Modeling (ODM) library that provides a straightforward and powerful way to manage MongoDB databases. It provides an intuitive way to define data structures, perform CRUD operations, and query data using a simple API. Mongoose provides a schema-based solution to modeling data, which allows for enforcing data validation and consistency.&lt;/p&gt;

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

&lt;p&gt;The first step is to install MongoDB on your local machine. You can download MongoDB from their official website and follow the installation instructions provided.&lt;/p&gt;

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

&lt;p&gt;Once you have MongoDB installed, you can install Mongoose using the yarn package manager by running the following command in your terminal:&lt;/p&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwf3lmdcrknuzcl0hzv2.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwf3lmdcrknuzcl0hzv2.png" alt="Installing mongoose using yarn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Connecting to MongoDB using Mongoose
&lt;/h3&gt;

&lt;p&gt;To connect to your MongoDB database, you need to create a new Mongoose connection. You can do this by adding the following code to your db.js file:&lt;/p&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxepsa7s6gchi5kjd2v2.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxepsa7s6gchi5kjd2v2.png" alt="connecting database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Defining a schema and creating a model
&lt;/h3&gt;

&lt;p&gt;we can define a schema for our data and create a model to interact with the database. In this example, we will define a simple schema for a user object. Create a new file called "user.js" and add the following code:&lt;/p&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmc6fmtb6cb2xjdkqr5o.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmc6fmtb6cb2xjdkqr5o.png" alt="Schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Data Controller
&lt;/h3&gt;

&lt;p&gt;Can be used to manipulate the data in our MongoDB database. Here is an example of how to create a new user:&lt;/p&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmq0gjr37s0i96dxxkkot.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmq0gjr37s0i96dxxkkot.png" alt="Controller"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we listen for connection events using the &lt;strong&gt;mongoose.connect() method.&lt;/strong&gt; The 'connected' event is emitted when the connection is established successfully, and the 'error' event is emitted when there is an error connecting to MongoDB.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Article by Aruya Temitayo™️.&lt;/p&gt;
&lt;/blockquote&gt;

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