DEV Community

BillyGoat12
BillyGoat12

Posted on

Basic of MongoDB

Database management system

Data is facts about any object in consideration and we normally store data in a database because data in a database is organized which makes it easy to manage. A Database Management System is a collection of programs that enables users to access databases, manipulate data, and help in representation of data. Today we will be talking about databases and their management system.

Non-Structured Database

MongoDB is a non-structured database, this means that the data is not constricted to the shape of a table. To understand non-structured databases I will talk about structured databases first. Another name for structure databases is called relational database and when we are talking to this kind of database we use SQL. SQL is known as Structured Query Language. When talking to a non-Structured Database we use NoSQL. NoSQL means Not Only Structured Query Language.

Alt Text

Relational Database vs Non-Structured Database

We can think of relational databases as in structuring data in tables. A table has columns and this defines the structure of our data. So if we made a table with 3 columns then every single row has to follow that structure and if for some reason we want to add a new column for only a certain row it would not work unless we add a new column for the whole table. A field is what we refer to when we are talking about a value for a certain column. Now when we think about Non-Structure databases we do not have to worry about our data fitting perfectly inside a table. In a Non-Structure database we use a collection instead of a table and collection is a little different from a table. We can put a document in a collection and that document would be like a row in a table. The difference is that we can add a new document(row) with different data into our collection(table) because it's not restricted just to the documents that fit a specific format. Inside a document you will have key-value pairs and the key would be the field and the value would be that value. So basically a document is like an object.

Alt Text

Mongo

The way mongo works is pretty cool. For example, if we try to work with something that does not exist then mongo will create it for us. If we tried to work with a database that does not exist mongo will create it for us so like if we type use and a name it will create that database with that name for us so we can use it. Now that we are in the collection we can type db.a-collection-name.a-function and that will help us create that collection. So if we do db.user.insertOne ( { field: value } ) this would return
{ acknowledge: true, insertedId: ObjectID ( some number ) }.
The numbers in the objectId is like a primary key in the table but we did not have to set that up, mongo did it for us.

Alt Text

conclusion

Mongodb is a non-structure database and doesn't have to follow normal rules when creating data. Also mongodb makes it easier to change and create data because of its shortcuts and how its not restricted to a certain format.

Top comments (0)