In this article, we will see how to create database in MongoDB. Actually, you do not need to create database manually in MongoDB because it will create database automatically when you save the value into defined collection at very first time. It is little wired if you are a beginner MongoDB developer.



In MySQL data is stored in rows and tables where as in MongoDB data is stored in JSON collection. In MongoDB you do not need to create schema, when you save a data into collection it will create schema based on your data.
First of all start mongo shell and connect to your MongoDB instance. To start mongo shell navigate to below simple steps:

  • 1. Open command line or terminal.
  • 2. Go to your : cd (Eg. cd /user/mongodb)
  • 3. Type ./bin/mongo to start mongo : ./bin/mongo. … (Eg. ../bin/mongo)

Create database in MongoDB

You can also use MongoDB client such as Robomongo and MongoBooster . MongoBooster is my favorite MongoDB client.

Create Database in MongoDB

MongoDB use DATABASE_NAME is used to create a database. Run below command in command line to create MongoDB database.

Syntax:

The command will create a new database if it doesn’t exist, otherwise it will return the existing database.

Example:

If you want to create a database with name “db_shoppy”, then command syntax would be as follow.

MongoDB – Check the currently selected database

To check which database is currently you have selected, a db command would be used.

Example:

Output:

MongoDB – Insert Document

The command insert() is used to insert data into database. If the collection does not exists in the database, then MongoDB will create a collection and then insert a document into it.

Syntax:

Example:

Here, product is collection name and we have inserted two fields into it.

MongoDB – Show all databases

To show all created databases use “show dbs” command. Run this command in mongo shell to get list of all the created databases.

Example:

Output:

Output will display list of database with database size.

MongoDB – Find Document

To find a document in MongoDB, you need to use find() command.

Syntax:

Example:

pretty() will display the result in formatted way.

MongoDB – Drop Database

To drop the selected database, run dropDatabase() command into MongoDB shell.

Syntax:

Example:

To drop the database first of all select the database using use command that you want to drop and run db.dropDatabase() to drop selected database.

These are the most common commands that each MongoDB developer need to know. Let me know other commands that you usually use while working with MongoDB.

 

Read More:

Create a RESTful Web Service API with Slim

How to Integrate PayUMoney Payment gateway in PHP

How To Integrate Stripe Payment Gateway Using PHP and JavaScript

Create RESTful API Using Node.JS, Express and MySQL

sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">