,

Session 4.1: Introduction to MongoDB and Coding Exercise 10 in Full-Stack Development

Posted by


Welcome to Session 4.1 of our Full-Stack Development tutorial series! In this session, we will be diving into the world of MongoDB, a popular NoSQL database, and take a look at some basic operations we can perform with it. We will also be practicing what we have learned by coding a simple application that interacts with MongoDB.

Before we begin, make sure you have MongoDB installed on your machine. You can download MongoDB from the official website (https://www.mongodb.com/). Once you have MongoDB installed, make sure the MongoDB server is running.

Now, let’s start by understanding what MongoDB is and how it differs from traditional relational databases. MongoDB is a document-based database that stores data in JSON-like documents rather than rows and columns like a relational database. This makes it a great choice for storing unstructured or semi-structured data.

To interact with MongoDB, we will be using the official MongoDB Node.js driver. You can install this driver by running the following command in your project directory:

npm install mongodb

Now, let’s create a new file for our application, let’s call it app.js. In this file, we will establish a connection to our MongoDB database and perform some basic operations.

const { MongoClient } = require('mongodb');

const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);

async function main() {
  try {
    await client.connect();

    console.log('Connected to MongoDB');

    const database = client.db('myDatabase');
    const collection = database.collection('myCollection');

    // Insert a document
    const insertResult = await collection.insertOne({ name: 'John Doe' });
    console.log('Inserted document:', insertResult.ops[0]);

    // Find a document
    const findResult = await collection.findOne({ name: 'John Doe' });
    console.log('Found document:', findResult);

    // Update a document
    const updateResult = await collection.updateOne({ name: 'John Doe' }, { $set: { age: 30 } });
    console.log('Updated document:', updateResult.modifiedCount);

    // Delete a document
    const deleteResult = await collection.deleteOne({ name: 'John Doe' });
    console.log('Deleted document:', deleteResult.deletedCount);
  } catch (error) {
    console.error('Error:', error);
  } finally {
    await client.close();
  }
}

main();

In this code snippet, we establish a connection to our MongoDB database and perform basic CRUD operations – insert, find, update, and delete a document in a collection. Make sure to replace 'myDatabase' and 'myCollection' with your own database and collection names.

To run the application, execute the following command in your terminal:

node app.js

You should see the output of the various operations we performed on the MongoDB database.

Congratulations! You have now successfully connected to MongoDB and performed basic operations with it. In the next session, we will explore more advanced topics such as querying, indexing, and aggregation in MongoDB. Stay tuned for Session 4.2!

Thank you for following along with our Full-Stack Development tutorial series. If you have any questions or need further assistance, feel free to reach out. Happy coding!