Quick Contact

    MongoDB Tutorial

    MongoDB Database Creation

    MongoDB Create Database

    If you are looking for a command to create a database, you must stop now. This is because MongoDB does not provide any command to create a database. In fact, we don’t create a database in MongoDB.

    Unlike SQL, where we need to create a database, tables and then insert the values manually, MongoDB creates the database automatically. You just need to save a value in the defined collection with the preferred name.

    You don’t even have to mention that you want to create a database. However, you can create collections manually. We will discuss that later.

    The “use” Command

    If there is no database, use the following command to define the name for your database and the database is created if it doesn’t exist already.

    Syntax

    usedatabase_name

    For example, Let’s say the database name is “ducat”.

    >use ducat

    Switched to db ducat

    Check Selected Database

    You can check that database you have selected already. Follow this command to do that.

    >db

    ducat

    List Database

    If you don’t know the databases that already exist in the system, you can list them and know if they do. Just use the following command for this.

    >show dbs

    local 0.52938GB

    test 0.49231GB

    If your database is not in the list, your database has not been created yet. To create a database you must save at least one document in it.

    Save a Document

    Run the following command to insert a document into the database.

    	>db.user.insert({name: "Praveen", age: 26})
    	WriteResult({ "nInserted" : 1 }) 
    	> show dbs
    	local          0.52938GB
    	dataflair      0.49231GB
    	test           0.49231GB
    	

    Now your database with the name “ducat” has been created.

    MongoDB has its own feature to create the database.

    Must Read About MongoDB Tutorial.

    Copyright 1999- Ducat Creative, All rights reserved.