MongoDB, an Introduction

Friday, October 21st, 2022

MongoDB is a NoSQL, document-based database management system. It was created in 2007 by MongoDB Inc. (formerly known as 10gen).

Contrasting MongoDb and SQL

SQL (Structured Query Language) and MongoDB are two different types of databases, each with its own strengths and weaknesses. Here are some key differences between the two:

Data Model:

  • SQL uses a relational model, where data is organized into tables with defined columns and relationships between them.
  • MongoDB uses a document model, where data is stored in semi-structured documents in a JSON-like format.

Schema:

  • SQL databases have a fixed schema, meaning that the structure of the data must be defined in advance and cannot change easily.
  • MongoDB has a flexible schema, where fields can be added or removed from documents without affecting the rest of the data in the collection.

Query Language:

  • SQL uses a declarative language, where you specify what data you want to retrieve without specifying how to retrieve it.
  • MongoDB uses a query language that is similar to JSON, where you specify the conditions to match documents in the collection.

Scaling:

  • SQL databases typically use a shared-nothing architecture, where each node in a cluster has its own separate copy of the data. This can make it more challenging to scale out the database.
  • MongoDB uses a shared-nothing architecture and automatic sharding, allowing it to scale horizontally by distributing data across multiple nodes.

While SQL is a mature and well-established technology, widely used for its ability to enforce data consistency and relationships, MongoDB is a newer technology, designed for fast and flexible storage of semi-structured data, and scalability. The choice between the two will depend on the specific requirements of the application and the nature of the data being stored.

MongoDB Structure, Databases and Collections

In MongoDB, a database is a top-level container for collections, which are similar to tables in SQL databases. Each collection can store multiple documents, and each document can have different fields. Indexes are used in MongoDB to improve query performance and can be created on specific fields within a document.

MongoDB, Sample Queries

  1. Find all documents in a collection:




db.collection.find({})
  1. Find a document with a specific field value:




db.collection.find({field: value})
  1. Find and limit the number of documents returned:




db.collection.find({}).limit(n)
  1. Sort documents by a specific field:




db.collection.find({}).sort({field: 1})
  1. Update a single document:




db.collection.updateOne({field: value}, {$set: {field: newValue}})
  1. Update multiple documents:




db.collection.updateMany({field: value}, {$set: {field: newValue}})
  1. Delete a single document:




db.collection.deleteOne({field: value})
  1. Delete multiple documents:




db.collection.deleteMany({field: value})
  1. Aggregate documents using the pipeline:




db.collection.aggregate([
   {$match: {field: value}},
   {$group: {_id: "$field", total: {$sum: "$value"}}}
])

MongoDB, Common Tools

  • MongoDB Compass: a GUI tool for visualizing and managing data in MongoDB databases
  • MongoDB Atlas: a cloud-based platform for deploying, managing, and scaling MongoDB databases
  • mongodump and mongorestore: command line tools for backing up and restoring MongoDB databases.

Overall, MongoDB is a popular choice for modern web and mobile applications due to its flexible data model, scalability, and performance.