Create Database, Collections and Documents in MongoDB | Part 2

Priyanka Patwal
Last Updated: May 13, 2022

Introduction

MongoDB is document-oriented, no sequel(No SQL) database. MongoDB replaces the concept of rows of conventional relational data models with something known as documents. 

 

MongoDB offers developers the flexibility to work with evolving data models. Since it is document-based, it allows embedded documents, arrays and represents complex hierarchical relationships using a single record.

 

It is also schema-free, which means that the keys defined in the document are not fixed. As a result, massive data migration can be ruled out. 

 

You must be wondering, how do we start using MongoDB? Keep reading, and we'll explain everything in this article. Let's start by understanding the meaning of some of the critical terms of MongoDB.

 

Key Terms of MongoDB

In SQL databases, the data is stored in tables, where the column denotes the attribute and the row denotes a particular record. These tables reside inside the databases. The SQL databases have a relational property where different tables are related to each other.  

 

But as we already know, MongoDB is a NoSQL database. Let’s see how data is stored in MongoDB.

Database

A database is a physical storage location for data. On the file system, each database has its collection of files. Multiple databases are commonly found on a single MongoDB server.

Collection

The term "collection" refers to a group of MongoDB documents. It's the same thing as an RDBMS table. Within a single database, there is a collection. Collections do not enforce a schema. Different fields can be found in different documents within a collection. In most cases, all of the documents in a collection serve the same or comparable purposes.

Document

A document consists of a collection of key-value pairs. The schema of documents is dynamic. Documents in the same collection don't have to have the same set of fields or structure, and standard fields in a collection's documents can contain different types of data.

 

Let’s get started with creating parts!

Creating a Database in MongoDB

Use the use <db> statement in mongosh to select a database to use, like in the following example:

use myDB

If a database is non-existent, MongoDB creates the database when you first store data for that database. 

Creating a collection in MongoDB

If a collection is non-existent, MongoDB creates the collection when you first store data for that collection.

db.myCollection1.insertOne( { x: 1 } )
db.myCollection2.createIndex( { y: 1 } )

 

If their corresponding collections do not already exist, the insertOne() and createIndex() methods create them.

 

The DB.createCollection() method in MongoDB allows you to explicitly create a new collection with various settings, such as a maximum size or documentation validation rules. You do not need to explicitly create the collection if you do not specify these settings because MongoDB creates new collections when you first store data for the collections.

Creating a document in MongoDB

You can create documents in a collection using the MongoDB CRUD Operators 

  • Use the insertOne() method to insert one document.
  • Use the insertMany() method to insert more than one document.

Creating one document

To create one document, use the following syntax :

db.collection.insertOne(
  <document>,
    {
      writeConcern: <document>
    } 
)

 

 

For example:

use("test");

db.sales.insertOne(
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z")}
);

Creating many documents

To create many documents, use the following syntax:

DB.collection.insertMany(
  [ <document 1> , <document 2>, ... ],
   {
        writeConcern: <document>,
    ordered: <boolean>
   })

 

 

For example:

use("test");

db.sales.insertMany([
  { "_id" : 2, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z") },
  { "_id" : 3, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : new Date("2014-03-01T09:00:00Z") },
  { "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : new Date("2014-03-15T09:00:00Z") },
]);

Frequently asked questions

1. How to create a database in MongoDB?

Ans: Use the use <db> statement in mongosh to select a database to use

 

2. What is a collection in MongoDB?

Ans: The term "collection" refers to a group of MongoDB documents

 

3. What is a document in MongoDB?

Ans: A document consists of a collection of key-value pairs. The schema of documents is dynamic.

 

4. How to create a collection?

Ans: db.myCollection1.insertOne( { x: 1 } )

    db.myCollection2.createIndex( { y: 1 } )

Key Takeaways:

MongoDB is a document database with the scalability and flexibility that we want in today’s world. This article explains how to create documents, databases and collections in MongoDB.

 

This article is a part of three blog series on MongoDB. The first part of this blog introduces MongoDB, its functional features, and features that make it the most popular non-relational database. And the third blog contains CRUD operations in MongoDB. 

 

For a complete understanding of MongoDB, we suggest you read all three articles.

 

Happy Learning!

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think