hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

What is Document Database

How does it sound having your database in “DOCUMENT”?

Yes!! Document databases are non-relational (or NoSQL) databases.

Instead of storing data in fixed rows and columns, document databases use flexible documents.

Document databases are the most popular alternative to tabular, relational databases.

Popular document-oriented databases are MongoDB, DynamoDB and CosmosDB.

What are documents

Ø A document is a record in a document database. A document typically stores information about one object and any of its related metadata.

Ø Documents store data in field-value pairs. The values can be a variety of types and structures, including strings, numbers, dates, arrays, or objects. Documents can be stored in formats like JSON, BSON, and XML.

What are Collections

  • A collection is a group of documents. Collections typically store documents that have similar contents.

  • Not all documents in a collection are required to have the same fields, because document databases have a flexible schema.

How do we do CRUD operations

Document databases typically have an API or query language that allows developers to execute the CRUD (create, read, update, and delete) operations.

  • Create: Documents can be created in the database. Each document has a unique identifier.

  • Read: Documents can be read from the database. The API or query language allows developers to query for documents using their unique identifiers or field values. Indexes can be added to the database in order to increase read performance.

  • Update: Existing documents can be updated — either in whole or in part.

  • Delete: Documents can be deleted from the database.

What are the key features of document databases?

Document databases have the following key features:

  • Document model: Data is stored in documents (unlike other databases that store data in structures like tables or graphs). Documents mapped to objects in most popular programming languages, which allows developers to rapidly develop their applications.

  • Flexible schema: Document databases have a flexible schema, meaning that not all documents in a collection need to have the same fields. Note that some document databases support schema validation, so the schema can be optionally locked down.

  • Distributed and resilient: Document databases are distributed, which allows for horizontal scaling (typically cheaper than vertical scaling) and data distribution. Document databases provide resiliency through replication.

  • Querying through an API or query language: Document databases have an API or query language that allows developers to execute the CRUD operations on the database. Developers can query documents based on unique identifiers or field values.

What makes document databases different from relational databases?

Three key factors differentiate document databases from relational databases: 1. The intuitiveness of the data model: Documents map the objects in code, so they are much more natural to work with. There is no need to decompose data across tables, run expensive joins, or integrate a separate Object Relational Mapping (ORM) layer. Data that is accessed together is stored together, so developers have less code to write and end users get higher performance. 2. The ubiquity of JSON documents: JSON has become an established standard for data interchange and storage. JSON documents are lightweight, language-independent, and human-readable. Documents are a superset of all other data models so developers can structure data in the way their applications need — rich objects, key-value pairs, tables, geospatial and time-series data, or the nodes and edges of a graph. 3. The flexibility of the schema: A document’s schema is dynamic and self-describing, so developers don’t need to first pre-define it in the database. Fields can vary from document to document. Developers can modify the structure at any time, avoiding disruptive schema migrations. Some document databases offer schema validation so you can optionally enforce rules governing document structures.

The difference between them can be explained with the help of the following diagram.


Let’s See How to Install and Work with MongoDB

1. Head over here and download the current version of MongoDB.

2. Make sure you are logged in as a user with Admin privileges.

3. Click on the installer to install MongoDB and follow the steps.

4. After the installation is complete you will have “Mongo DB Compass” query tool to play around with your database.

5. First screen will look like below in picture


1. Here in the new connection box, you can paste the URL you want to connect to the DB, either you can connect to any MongoDB on cloud or keep it to the localhost.

2. The default port for the localhost will be 27017.

Now, we will see how to play around with MongoDB compass.

1. First you can create your own DB by clicking on ‘+’ sign on left panel as shown in the below screenshot.


2. After you click the ‘+’ button you will be directed to create DB screen you can follow steps shown in below screen.


3. Once you click on create database, database with item collection will be created and now we will see how we can add data to this collection.

4. For adding data, you can choose your DB(Grocery) and the collection (Item), click on

button; select insert document option.

5. You can choose the tabular view in which it is easier to add the data.

6. id’ will be auto generated for every document entry in the DB.

7. Now if you try to add a record for id by clicking on it you can add the field as shown in picture below. You can choose the data types you want to assign to the fields.

8. You can choose the tabular view in which it is easier to add the data.


9. You can delete the document by clicking on the document and the delete button will pop-up to delete the document.

10. If you want to sort/ filter the data, you can query your database by using simple key value pair like in this ‘Grocery’ DB I want to find out items having no of packs 10. So, I will filter it using {NoOfPacks:10}, it will give results as shown in picture below.


What are the Advantages of MongoDB?

MongoDB records are stored as documents in compressed BSON files. The documents can be retrieved directly in JSON format, which has many benefits:

  • It is a natural form to store data.

  • It is human-readable.

  • Structured and unstructured information can be stored in the same document.

  • You can nest JSON to store complex data objects.

  • JSON has a flexible and dynamic schema, so adding fields or leaving a field out is not a problem.

  • Documents mapped to objects in most popular programming languages.

  • Most developers find it easy to work with JSON because it is a simple and powerful way to describe and store data.

  • Perhaps most importantly, the developer controls the database schema.

  • Developers adjust and reform the database schema as the application evolves without the help of a database administrator.

When Should You Use MongoDB?

  • Integrating large amounts of diverse data

  • Describing complex data structures that evolve

  • Delivering data in high-performance applications

  • Supporting hybrid and multi-cloud applications

  • Supporting agile development and collaboration

Conclusion

  • MongoDB is a general-purpose database that can provide many benefits to your application development processes.

  • It can help you build applications that are more future proof with its scaling capabilities and flexible schema.

  • It offers great developer experience with drivers for most major programming languages and a large community of users. It is also available on any of the major cloud providers.

  • Why not give it a try right now?

42 views0 comments

Recent Posts

See All