Loading

Mongo DB

How to Create Database & Collection in MongoDB?. The Complete Mongo DB Developer Course 2023 [Videos].

In MongoDB, the first basic step is to have a database and collection in place.

In MongoDB, the first basic step is to have a database and collection in place. The database is used to store all of the collections, and the collection in turn is used to store all of the documents. The documents in turn will contain the relevant Field Name and Field values.

The snapshot below shows a basic example of how a document would look like.

MongoDB Create & Insert Database

The Field Names of the document are “Employeeid” and “EmployeeName” and the Field values are “1” and “Smith respectively. A bunch of documents would then make up a collection in MongoDB.

In this Video, you will learn –

Creating a database using “use” command

Creating a database in MongoDB is as simple as issuing the “using” command. The following example shows how this can be done.

MongoDB Create & Insert Database

Code Explanation:

  • The “use” command is used to create a database in MongoDB. If the database does not exist a new one will be created.

If the command is executed successfully, the following Output will be shown:

Output:

MongoDB Create & Insert Database

MongoDB will automatically switch to the database once created.

Creating a Collection/Table using insert()

The easiest way to create a collection is to insert a record (which is nothing but a document consisting of Field names and Values) into a collection. If the collection does not exist a new one will be created.

The following example shows how this can be done.

db.Employee.insert
(
	{
		"Employeeid" : 1,
		"EmployeeName" : "Martin"
	}
)

Code Explanation:

  • As seen above, by using the “insert” command the collection will be created.

Adding documents using insert() command

MongoDB provides the insert () command to insert documents into a collection. The following example shows how this can be done.

Step 1) Write the “insert” command

Step 2) Within the “insert” command, add the required Field Name and Field Value for the document which needs to be created.

MongoDB Create & Insert Database

Code Explanation:

  1. The first part of the command is the “insert statement” which is the statement used to insert a document into the collection.
  2. The second part of the statement is to add the Field name and the Field value, in other words, what is the document in the collection going to contain.

If the command is executed successfully, the following Output will be shown

Output:

MongoDB Create & Insert Database

The output shows that the operation performed was an insert operation and that one record was inserted into the collection.

See All

Comments (192 Comments)

Submit Your Comment

See All Posts

Related Posts

Mongo DB / Youtube

What is MongoDB and why use it?

MongoDB is an open source NoSQL database management program. NoSQL is used as an alternative to traditional relational databases. NoSQL databases are quite useful for working with large sets of distributed data. MongoDB is a tool that can manage document-oriented information, store or retrieve information.
27-dec-2020 /4 /192

Mongo DB / Youtube

What is the difference between a document and a collection in MongoDB?

Databases, collections, documents are important parts of MongoDB without them you are not able to store data on the MongoDB server. A Database contains a collection, and a collection contains documents and the documents contain data, they are related to each other.
27-jan-2021 /4 /192

Mongo DB / Youtube

What is MongoDB and why use it?

MongoDB is an open source NoSQL database management program. NoSQL is used as an alternative to traditional relational databases. NoSQL databases are quite useful for working with large sets of distributed data. MongoDB is a tool that can manage document-oriented information, store or retrieve information.
24-May-2021 /4 /192