DEV Community

OKUKU_OKAL
OKUKU_OKAL

Posted on • Updated on

Introduction to MongoDB

NoSQL databases address specific challenges and requirements that traditional relational databases may struggle to handle effectively, particularly scaling strategy. MongoDB is a document-based NoSQL database. Documents refers to association arrays like JSON objects or Python dictionaries. In a document-oriented database, the concept of a "row" is replaced by a more flexible model, the "document."
The document-oriented approach allows for the representation of complicated hierarchical relationships with a single record through its support for embedded documents and arrays. Additionally, there are no predefined schemas because the categories and sizes of a document's keys and values are flexible. Without a rigid schema, it is simpler to add or remove fields as necessary. MongoDb documents of similar types are grouped into a collection. Every document has a special key, "_id", that is unique within a collection.
In this article, we'll examine MongoDB's core characteristics, compare it with RDBMS, go over its benefits, look at some of its use cases, dive into the fundamental CRUD operations, study MongoDB's indexes, and briefly discuss Aggregation Framework.

Why Use MongoDB?

  • You have the capacity to load any type of data into MongoDB, whether it's structured or not.
  • Working with MongoDB is easy because you can focus on the data you are writing and how you are going to read it, unlike traditional relational databases, that require you to create the schema first, then create table structures that will hold your data.
  • High availability is yet another benefit that MongoDB offers by storing multiple copies of your data. your data.

Advantages of MongoDB

MongoDB offers several advantages over traditional RDBMS:

1.Flexibility with the schema: This feature enables us to store unstructured data. For instance, merging data of varying shapes from different sources, in the process facilitating storage and analysis.
2.Code-first approach: There are no complex table definitions. You can start writing your first data as soon as you connect to your MongoDB database.
3.Evolving Schema: Querying and Analytics Capabilities: Mongo Query Language(MQL) has a wide range of operators for complex analysis use and Aggregation pipelines.
4.High Availability: MongoDB is a natively highly available system by means of redundancy (Typical MongoDB setups are three-node replica sets, where one of them is a primary member and the others are secondary members. Replication keeps a copy of your data on other data bearing nodes in the cluster. If one system fails, the other one takes over and you do not see any downtime.).

Use Cases For MongoDB

  1. IoT devices: there are billions of Iot devices around the globe that generate vast amounts of data. With scaling capabilities, MongoDB can easily store all of this data distributed globally.
  2. E-commerce: Products sold on e-commerce websites have different attributes. With the help of documents, sub-documents, and list properties in MongoDB, you can store the information together so it is optimized for reads.
  3. Real-time Analytics: MongoDB is a good fit when you want real-time analytics. Doing historical analysis is easy, but only a few can respond to changes happening minute by minute. And most of the time, this is due to complex Extract, Transform, and Load (or ETL) processes. With MongoDB, you can do most of the analysis where the data is stored.
  4. Gaming: MongoDB plays a large part in the gaming world, too. With more than ever multiplayer games being played globally, getting the data across is hugely important. With native scalability, also known as Sharding, MongoDB makes it easier to reach users around the world.
  5. Finance: Nowadays, we want our banking transactions to be as quick as possible, while we also expect the financial industry to keep our information secure. With MongoDB, you can perform thousands of operations on your database per second.
  6. Web Applications: MongoDB is well suited as a primary datastore for web applications. Numerous data models will be required; for managing users, sessions, app-specific data, uploads, and permissions.

MongoDB versus RDMS

  • MongoDB and relational databases are both capable of representing a rich data model.
  • Where relational databases use fixed-schema tables, MongoDB has schema-free documents.
  • Most relational databases support secondary indexes and aggregations.
  • Relational databases are preferred by data scientists or data analysts who write queries to explore data .On the contrary, MongoDB’s query language is aimed more at developers, who write a query once to embed it in their application.
  • MongoDB is faster as compared to RDBMS due to efficient indexing and storage techniques.

CRUD Operations

(How to Create, Read, Update and Delete documents in MongoDB)

MongoDB shell allows you to interact with MongoDB; it allows you to examine and manipulate data and administer the database server itself.

  • To start the MongoDB shell, run: mongo In order to execute a query on MongoDB, you must have knowledge of the database (or namespace) and collection from which you wish to retrieve documents. If no other database is specified when starting up the shell, it automatically selects a default database named "test."
  • To select a specific database in the MongoDB shell, you can use the use command followed by the database name: use training
  • Create a collection named 'f1drivers': db.createCollection("f1drivers")
  • Insert documents into our collection: First insert the leading driver in the championship standings after the 2023 Canadian GP:
db.f1drivers.insert(
{"Driver": "Max Verstappen",
 "Team": "RedBull",
"Points": 195})
Enter fullscreen mode Exit fullscreen mode

Insert the remaining drivers' information:

db.f1drivers.insertMany([

  {
    "Driver": "Sergio Perez",
    "Team": "RedBull",
    "Points": 126
  },
  {
    "Driver": "Fernando Alonso",
    "Team": "Aston Martin",
    "Points": 117
  },
  {
    "Driver": "Lewis Hamilton",
    "Team": "Mercedes",
    "Points": 102
  },
  {
    "Driver": "Carlos Sainz",
    "Team": "Ferrari",
    "Points": 68
  },
  {
    "Driver": "George Russell",
    "Team": "Mercedes",
    "Points": 65
  },
  {
    "Driver": "Charles Leclerc",
    "Team": "Ferrari",
    "Points": 54
  },
  {
    "Driver": "Lance Stroll",
    "Team": "Aston Martin",
    "Points": 37
  },
  {
    "Driver": "Esteban Ocon",
    "Team": "Alpine",
    "Points": 29
  },
  {
    "Driver": "Pierre Gasly",
    "Team": "Alpine",
    "Points": 15
  },
  {
    "Driver": "Lando Norris",
    "Team": "Mclaren",
    "Points": 12
  },
  {
    "Driver": "Alexander Albon",
    "Team": "Williams",
    "Points": 7
  },
  {
    "Driver": "Nico Hulkenberg",
    "Team": "Haas",
    "Points": 6
  },
  {
    "Driver": "Oscar Piastri",
    "Team": "Mclaren",
    "Points": 5
  },
  {
    "Driver": "Valtteri Bottas",
    "Team": "Alfa Romeo",
    "Points": 5
  },
  {
    "Driver": "Zhou Guanyu",
    "Team": "Alfa Romeo",
    "Points": 4
  },
  {
    "Driver": "Yuki Tsunoda",
    "Team": "AlphaTauri",
    "Points": 2
  },
  {
    "Driver": "Kevin Magnussen",
    "Team": "Haas",
    "Points": 2
  },
  {
    "Driver": "De Vries",
    "Team": "AlphaTauri",
    "Points": 0
  },
  {
    "Driver": "Logan Sargent",
    "Team": "Williams",
    "Points": 0
  }
]);

Enter fullscreen mode Exit fullscreen mode
  • List all documents in the collection:
db.f1drivers.find()
Enter fullscreen mode Exit fullscreen mode
  • List the drivers of the Mercedes team:
db.f1drivers.find({ Team: "Mercedes" })

Enter fullscreen mode Exit fullscreen mode
  • Find the third-ranked driver in the collection:
db.f1drivers.find().sort({ Points: -1 }).skip(2).limit(1)

Enter fullscreen mode Exit fullscreen mode
  • Update the team name from 'RedBull' to 'Red Bull Racing':
db.f1drivers.updateMany(
  { Team: "RedBull" },
  { $set: { Team: "Red Bull Racing" } }
)

Enter fullscreen mode Exit fullscreen mode
  • Delete drivers with zero points:
db.f1drivers.deleteMany({ Points: 0 })

Enter fullscreen mode Exit fullscreen mode

Indexes

Indexes are enormously important. They help quickly locate data without looking for it everywhere; they store the fields you are indexing as they also store the location of the document. MongoDB stores Indexes in a tree form.

  • To create an index we use the 'createIndex()' method:
db.students.createIndex({"class_id": 1})
Enter fullscreen mode Exit fullscreen mode
  • To delete an index we use the 'dropIndex()' method:
db.students.dropIndex("class_id_1")

Enter fullscreen mode Exit fullscreen mode

Make sure to provide the correct index name based on the index you want to drop. You can retrieve the list of existing indexes in the collection using the 'getIndexes()' method:

db.students.getIndexes()

Enter fullscreen mode Exit fullscreen mode

This command will display a list of indexes in the respective collection, including their names. From there, you can identify the index you wish to drop and use its name in the 'dropIndex()' method.
Please note that dropping an index permanently removes it from the collection, so ensure that you are selecting the correct index to drop.

Aggregation Framework

These are a series of operations that you apply on your data to get a desired outcome, particularly useful for tasks such as grouping, filtering, sorting, joining, and calculating aggregate statistics.
Common aggregation stages include:

  1. '$merge': Takes the outcome from a previous stage and stores it into a target collection.
  2. '$project': Changes the shape of documents or projects out certain fields.
  3. '$sort': Sorts your documents based on specific criteria.
  4. '$count': Calculates the count of documents in a collection, that match a specific criteria, and assigns the outcome to a specified field.

Conclusion

This article provides a comprehensive introduction to MongoDB. However what's covered is just 'the tip of the iceberg'. I'll recommend the following resources for more information on the topics covered:

  • "MongoDB: The Definitive Guide" by Kristina Chodorow and Shannon Bradshaw.
  • "MongoDB in Action" by Kyle Banker, Peter Bakkum, Shaun Verch, and Douglas Garrett.

The most effective way of mastering a given technology is by practical hands-on experience.

Top comments (0)