Defining, Creating and Dropping a MongoDB collection

MongoDB is an open-source, cross-platform document-based database.

Document: It is the basic unit of MongoDB and a single record inside a collection is known as a document.
It is a JSON object which has the data in the form of key-value pairs.



Collection: It is the grouping of documents. A collection belongs to the single database inside MongoDB.
Collection contains the data in the form of document just like the table of RDBMS which contains the data in the form of rows and columns.

    Creating a mongodb collection:
    As a collection belongs to the single database so first we will have to create a database for creating a collection:

  • STEP 1: First run the following command into your mongo shell or command prompt

    mongo

  • STEP 2: Check all the existing databases by using the following command:

    show dbs

  • STEP 3: Creating a database
    Here we are creating a database named as- geeksforgeeks

    use geeksforgeeks

    If the database geeksforgeeks is present it will switch to it, else it will create a new database name as geeksforgeeks and then switch to it as shown below.