Introduction to MongoDB Databases

In MongoDB, a database is a container for collections, and collections are containers for documents. Databases can be created and managed using the MongoDB shell. Let's walk through the process of creating your first MongoDB database.


Using the MongoDB Shell

We will use the MongoDB shell to create a database. Follow these steps:


  1. Open your command line terminal or PowerShell.
  2. Start the MongoDB server if it's not already running (you can follow the steps from the previous guide).
  3. Open the MongoDB shell by typing the following command:

mongo

  • Now, let's create a new database. MongoDB will create the database when you insert data into it. To create a database, simply insert a document into a collection in the database you want to create. For example, let's create a new database called "myFirstDB" and insert a document into a collection named "myCollection":

  • use myFirstDB
    db.myCollection.insertOne({ name: "John", age: 30 })

    Now, the "myFirstDB" database is created, and it contains the "myCollection" collection with one document.


    Viewing Your New Database

    You can list all the databases in your MongoDB server by running the following command in the MongoDB shell:


    show dbs

    Your "myFirstDB" should appear in the list of databases.


    Conclusion

    Congratulations! You've created your first MongoDB database. MongoDB is a versatile database system that can store and manage your data efficiently. You can continue to work with your database, insert more data, and explore the many features MongoDB has to offer.