22.12.2023

How To Create Database and Collection in Mongodb?

Overview

That installation guide covered topic of manage DBMS MongoDB, in that case we will consider creation of database and collection in the system. All operations will execute on the machine with Ubuntu. If you have distribution of another system which support systemd, that guide will suitable for you.

MongoDB represent non-conventional DBMS, due to architecture and model system. They represent document-oriented and NoSQL management system. Instead of SQL in the DBMS represent own language of manage, which remind object-oriented language for programming. All object is entity with dedicated hierarchy DB -> Collections -> Document. Let's consider how to create two of the hierarchy step!

Installation

For avoid problem with library package and older utility we have to update system packages through apt manager:

apt update && apt upgrade -y

Screenshot №1 — Update

At the end of process you can see result of entering command, make sure that main component was upgraded! Run MongoDB shell or connect via any else way to the DBMS:

mongosh

Screenshot №2 — Mongosh

After login you can interact with DBMS, by using special command. If you have problem with log into the system, than try to check daemon for properly work:

systemctl status mongod

If you see dead process then try to start or restart daemon and check status again:

systemctl restart mongod

Screenshot №3 — Status

That have to help start system! At the first let's check existed database:

mongosh
show dbs

Screenshot №4 — List of DB

We can create own DB using implicit and explicit method, by the default we can type:

use gg

Screenshot №5 — Create DB

Which gg it's DATABASE_NAME, if you have already exist db with the same name, than system turn you to that db entity. Always when we start work with DB we have to type use command with name of DB. If you forget which database you use type:

db

And than name of the db will be displayed. In that db file we can create collection, main concept of them is grouping united various of files by determine parameters. We will use command before:

db.createCollection('ff')

Screenshot №6 — Create collection

By that command we create collection and simultaneously in the one command create document with value. The ff that name collection, you can enter any else. And by the command we can make sure that command will created:

show collections

In the field of the DB you can create multiply of the collections and use them. In another part of the manage MongoDB we consider deletion, rename and various operations of manipulate data in the system. All of that instructions dedicated to community edition version, which used in the different company.