🗂️Managing Your Data with MongoDB🗂️

·

1 min read

As we continue exploring back-end development, let's dive into MongoDB, a popular NoSQL database. MongoDB stores data in flexible, JSON-like documents, making it intuitive to learn and use.

In our house analogy, MongoDB is like the filing system in the office room, storing and organizing all important documents (data) that the house (our application) needs to function.

Let's look at how we can insert data into a MongoDB database:

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://localhost:27017';

const dbName = 'myproject';

const client = new MongoClient(url);


async function run() {

  try {

    await client.connect();

    console.log("Connected correctly to server");

    const db = client.db(dbName);

    const collection = db.collection('documents');



    let doc = {name: "John", age: 30};

    const result = await collection.insertOne(doc);

    console.log(`Document inserted with the _id: ${result.insertedId}`);

  } finally {

    await client.close();

  }

}


run().catch(console.dir);

In this example, we first connect to our MongoDB server, then we select our database and collection. We then insert a document into our collection with the insertOne method.

Being able to effectively store, retrieve, and manipulate data is a crucial part of any web application, and MongoDB is a powerful tool for this task.

In our next article, we'll look at combining all these skills to build a full-stack web application. Stay tuned! 🚀👩‍💻

Â