MongoDB-All In One

MongoDB-All In One

The basics you need to know when learning MongoDB...

When should I consider using MongoDB as a database solution?

MongoDB is a great choice in several situations. Here are some key scenarios when you should think about using MongoDB:

  1. Handling Large Amounts of Data: If your application deals with a lot of data that requires frequent reading and writing operations, MongoDB is a good option. It performs well with large volumes of data and can spread the workload across multiple servers using replication and sharding.

  2. Flexible Data Structure: If your application's data structure may change over time, MongoDB is suitable. It uses a document-based format, allowing you to store data in a JSON-like way without defining a fixed schema beforehand.

  3. High Availability: MongoDB offers a built-in replication feature, creating multiple copies of your data for increased availability and fault tolerance. This means your application remains accessible even if there are hardware failures or data centre issues.

  4. Real-Time Analytics & Reporting: MongoDB provides excellent support for real-time analytics and reporting. It has features like the aggregation pipeline and map-reduce, which help you extract valuable insights from your data and perform complex data manipulations easily.

  5. Geo-spatial Queries: If your application involves location-based data, MongoDB has built-in support for geospatial indexing and querying. This makes it convenient to work with location-based services, such as GPS tracking or location-based search features.

  6. Rapid Application Development: MongoDB is well-suited for startups and agile development teams. Its flexibility and ease of use enable quick iterations and frequent changes to the data structure. This way, developers can focus on implementing features without worrying about managing inflexible database structures.

In summary, use MongoDB when you have large datasets, need a flexible data model, want high availability, require real-time analytics, deal with location-based data, or prioritize rapid application development.

What is MongoDB Atlas?

MongoDB Atlas is a cloud-based database service provided by MongoDB that takes care of managing and maintaining MongoDB clusters. It offers several standout features and advantages for developers:

  1. Database as a Service (DBaaS): MongoDB Atlas handles essential database operations like backups, monitoring, scaling, and security. This allows developers to concentrate on their application development rather than database management.

  2. Global Cluster Support: Atlas lets you create distributed clusters that store and replicate data across multiple geographic locations. This improves performance, ensures high availability, and reduces data access delays (latency).

  3. Security: Atlas incorporates built-in security features like end-to-end encryption, role-based access control, and IP whitelisting. These measures ensure that your data remains safe and meets industry compliance standards.

  4. Performance Tools: MongoDB Atlas provides performance monitoring and optimization tools for your database. Features like the performance advisor and index suggestions help keep your database running at its best speed.

  5. Easy Scaling: You can easily scale your MongoDB cluster up or down, either vertically (by adding more powerful hardware) or horizontally (by distributing data across multiple servers). Atlas supports automated scaling for both storage and computing resources.

  6. Data Automation and Integration: Atlas seamlessly integrates with other services, such as business intelligence tools and serverless functions. Additionally, it facilitates straightforward data migration from existing on-premises or cloud-based deployments.

Understanding MongoDB Terminology.

Basic Concepts

  1. Data: Data refers to any information that can be stored and processed by a computer. It can include text, numbers, images, videos, and more.

  2. Database: A database is a structured collection of data. It's like a digital filing system where information is organized, stored, and easily retrievable.

  3. Collection: In a database, a collection is a group of related data records. It's similar to a folder that contains similar files. For example, a collection might store information about different users or products.

  4. Document: A document is a single data record within a collection. It's like a single file in a folder, and it contains information in a specific format, such as key-value pairs.

  5. Key-Value Pair: A key-value pair is a simple way of storing data, where each piece of information is represented by a "key" (a name or label) and its corresponding "value" (the actual data).

  6. Query: A query is a request for specific information from a database. It's like asking a question to retrieve data that matches certain criteria.

Advanced Concepts:

  1. Index: An index is a data structure that improves the speed of searching and retrieving data from a large collection. It acts like an organized reference to quickly find the data you need.

  2. Cursor: A cursor is a pointer to the result of a query. When you perform a query that returns multiple documents, the cursor allows you to navigate through the results and process them one by one.

  3. Aggregation: Aggregation is a way to perform advanced operations on data in a collection. It allows you to summarize, group, and process data to gain insights or create reports.

  4. Replica Set: A replica set is a group of MongoDB servers that maintain the same data. It ensures data redundancy and high availability, so if one server fails, another can take its place automatically.

  5. Sharding: Sharding is a technique used to horizontally scale a database by dividing the data into smaller chunks called shards. Each shard can be stored on a separate server, allowing the database to handle more data and distribute the workload effectively.

Understanding Data Models and Data Types

Firstly, you need to understand that MongoDB stores in BSON format, BSON is a binary-encoded format used to represent data and documents within the database. It is designed to be efficient in terms of both storage and processing, making it well-suited for MongoDB's data storage and retrieval operations.

BSON is an extension of the JSON format, which is a widely used data interchange format in web development. JSON is human-readable and easy for developers to work with, but it is text-based, which can be less efficient in terms of storage and processing, especially for large datasets.

To address these efficiency concerns, MongoDB uses BSON, which is a binary representation of JSON-like data. In BSON, data types are encoded as binary values, which makes it more compact and faster to parse compared to the text-based JSON format. BSON can represent various data types, including strings, numbers, arrays, objects (documents), boolean values, dates, and binary data.

As a developer, you typically do not need to write data in BSON format directly. Instead, MongoDB's drivers and the database server handle the serialization and deserialization of data to and from BSON format for you.

The following is a brief summary of the different data types supported in MongoDB.

  • ObjectId:
    unique identifier for documents in a collection. It is the default value generated for the _id field, ensuring uniqueness within the collection.

  • String

    String is used to store text data

  • Boolean
    true/false value

  • Integer

    Integer is used to store an integer value. MongoDB supports two integer types: 32-bit (int) and 64-bit (long).

  • Double
    Used to store floating point numbersr.

  • Date

    Date is used to store the date and time in Unix time format

      {
         "createdAt": ISODate("2019-02-18T19:29:22.381Z"),
      }
    
  • Array
    Array is used to store a list of values in a single field. The values can be of different data types.
    you can update an array using $push, $addToSet, $pull, and $pop.

      //add only if unique
      db.collection.updateOne(
        { _id: ObjectId('123xyz') },
        { $addToSet: { hobbies: 'painting' } }
      );
    
      // Remove the first item (use $pop with -1)
      db.collection.updateOne({ _id: ObjectId('123xyz') }, { $pop: { hobbies: -1 } });
    
      // Remove the last item (use $pop with 1)
      db.collection.updateOne({ _id: ObjectId('123xyz') }, { $pop: { hobbies: 1 } });
    
      //add to array
      db.collection.updateOne(
        { _id: ObjectId('123xyz') },
        { $push: { hobbies: 'painting' } }
      );
    

    You can query an array using $in, $all, and $size

      //Finding documents with a specific item in an array:
      db.user.find({ hobbies: 'swimming' });
      //Finding documents with any of the specified items in an array:
      db.user.find({ hobbies: { $in: ['swimming', 'coding'] } });
      //Finding documents with all specified items in an array:
      db.user.find({ hobbies: { $all: ['reading', 'coding'] } });
      //Finding documents with a specific array size:
      db.user.find({ hobbies: { $size: 3 } });
    
  • Object

    Object is used to store embedded documents, meaning a document can contain another document.

    Updating a Document

      db.collection.updateOne(
        { name: 'Alice' },
        { $set: { 'address.city': 'Los Angeles' } }
      );
    
  • Null
    Null is used to store a null value, representing the absence of a value or the field, for example, when a field is optional in your documents.

  • Code
    Code is used to store JavaScript code.

      {
         "script": Code("function() { return 'Hello, World!'; }"),
      }
    
  • Regular Expression
    Stores regular expression.

      {
         "pattern": /^mongodb/i,
      }
    

SideNote: GridFS is a specification for storing and retrieving files that exced the BSON-document size limit of 16 MB.

Embedded Documents and Arrays

There are two ways to reference two separate collections.
one is by adding a ref. Id to the other collection, the second is by adding the data itself as a sub-object holding its information.

{
    _id: 1,
    name: 'John Doe',
    addresses: [
        {
            street: '123 Main St',
            city: 'New York',
            zipcode: '10001'
        },
        {
            street: '456 Broadway',
            city: 'Los Angeles',
            zipcode: '90001'
        }
    ]
}

In this example, the addresses field represents an array of embedded sub-documents that contain the address details for the user.

Now what's the advantage of Embedded Documents

  • You can benefit from improved read/write performance
    you do not have to find a reference, then find another reference, and then access the data you want.

  • You have a one-to-many relationship

Accessing what you want to find is pretty easy,by using the dot notation.

db.userCollection.find({addresses.street:"123 Main St"});

Collections and Methods

A collection can be thought of as a container or group, used to store documents of similar structure, like a table in relational databases. However, unlike tables, collections don’t enforce a strict schema, offering more flexibility in managing your data.

  • Flexible Schema: A collection can contain multiple documents with different structures or fields, allowing you to store unstructured or semi-structured data.

  • Dynamic: Collections can be created implicitly or explicitly, and documents can be added or removed easily without affecting others in the collection.

Creating a collection

db.createCollection('users');

Managing a collection

  • Insert Documents: To insert a document into a collection, use the insertOne() or insertMany() methods.

      db.users.insertOne({ name: 'John Doe', age: 30, email: 'john@example.com' });
    
      db.users.insertMany([
        { name: 'Jane Doe', age: 28, email: 'jane@example.com' },
        { name: 'Mary Jane', age: 32, email: 'mary@example.com' },
      ]);
    
  • Find Documents: Use the find() method to query documents in a collection.

      db.users.find({ age: { $gt: 30 } });
    
  • Update Documents: Use the updateOne(), updateMany(), or replaceOne() methods to modify documents in a collection.

      db.users.updateOne({ name: 'John Doe' }, { $set: { age: 31 } });
    
      db.users.updateMany({ age: { $gt: 30 } }, { $inc: { age: 1 } });
    
  • Delete Documents: Use the deleteOne() or deleteMany() methods to remove documents from a collection.

      db.users.deleteOne({ name: 'John Doe' });
    
      db.users.deleteMany({ age: { $lt: 30 } });
    
  • Drop Collection: To delete the entire collection, use the drop() method.

      db.users.drop();
    

In MongoDB, collections are used to store documents. following are a few methods you'll need to learn to deal with mongoDB.

  • Insert
    To add data into these collections, MongoDB provides two primary insertion methods: insertOne() and insertMany(). In this section, we’ll explore the usage and syntax of these methods, along with their options and some basic examples.

The insertOne() method is used to insert a single document into a collection. This method returns an InsertOneResult object, that shows the outcome of the operation.

    db.inventory.insertOne({
      item: 'book',
      qty: 1,
    });

The insertMany() method is used to insert multiple documents into a collection at once. It returns an InsertManyResult object, displaying the status of the operation.

    db.inventory.insertMany([
      { item: 'pen', qty: 5 },
      { item: 'pencil', qty: 10 },
      { item: 'notebook', qty: 25 },
    ]);
  • Find
    find() method is an essential aspect of working with collections. It enables you to search for specific documents within a collection by providing query parameters. In this section, we’ll explore various find methods and how to filter, sort, and limit the search results.

      db.collection_name.find({"queryKey":"QueryValue"});
    

    MongoDB provides multiple logical operators for more advanced filtering, including $and, $or, and $not. To use logical operators, you pass an array of conditions.

    For example, to find users with an age of 25 and a first name of John:

      db.users.find({$and:[{age:25},{first_name:"John"}]})
    

    Projections
    Projections are used to control which fields are returned in the search results. by specifing a projection, you can choose to include or exclude a specific output. One is select, 0 is ignore

      db.users.find({ age: 25 }, { first_name: 1, age: 0 });
    

    Sort
    you can also sort the result based on a given field.

      db.users.find().sort({age:1})
    

    Limit and Skip
    To limit the function of the find method to X results
    to skip the first Y results

      db.users.find().limit(5) //show only 5
      db.users.find().skip(3) //skip first 3
    
  • Update
    update methods are used to modify the existing documents of a collection. They allow you to perform updates on specific fields or the entire document, depending on the query criteria provided. Here is a summary of the most commonly used update methods in MongoDB.

    updateOne():
    updates first document that it matches
    db.collection.updateOne(<filter>, <update>, <options>)

    updateMany():
    This method updates multiple documents that match the query criteria provided.

    replaceOne():
    This method replaces a document that matches the query criteria with a new document.

      db.collection.updateOne(
      //filter                //what to update with
      { name: 'John Doe' }, { $set: { age: 30 } }
      );
    
      //! tell what you want to do, 
      //then tell what you want to do to
      db.collection.updateMany(
      { status: 'new' }, { $inc: { views: 1 } }
      );
    
  • Delete
    You will often need to delete documents or even entire collections to manage and maintain your database effectively. MongoDB provides several methods to remove documents from a collection, allowing for flexibility in how you choose to manage your data. In this section, we will explore key delete methods in MongoDB and provide examples for each.

The deleteOne() method is used to delete a single document from a collection. It requires specifying a filter that selects the document(s) to be deleted. If multiple documents match the provided filter, only the first one (by natural order) will be deleted.

The deleteMany() method is used to remove multiple documents from a collection. Similar to deleteOne(), it requires specifying a filter to select the documents to be removed. The difference is that all documents matching the provided filter will be removed.

The remove() method can be used to delete documents in a more flexible way, as it takes both a filter and a justOne option. If justOne is set to true, only the first document (by natural order) that matches the filter will be removed. Otherwise, if justOne is set to false, all documents matching the filter will be deleted.
Syntax: db.collection.remove(FILTER, JUST_ONE)

In cases where you want to remove an entire collection, including the documents and the metadata, you can use the drop() method. This command does not require a filter, as it removes everything in the specified collection.

Syntax: db.collection.drop()

Query Operators

There are several types of query operators, such as the below.

Comparison Operators

Comparison operators allow you to compare the value of a field with specified values. Some common comparison operators are:

  • $eq: Matches values that are equal to the specified value.

  • $gt: Matches values that are greater than the specified value.

  • $gte: Matches values that are greater than or equal to the specified value.

  • $lt: Matches values that are less than the specified value.

  • $lte: Matches values that are less than or equal to the specified value.

  • $ne: Matches values that are not equal to the specified value.

  • $in: Matches values that are in the specified array.

  • $nin: Matches values that are not in the specified array.

Logic Operators

  • $and: Matches documents where all the specified conditions are true.

  • $or: Matches documents where at least one of the specified conditions is true.

  • $not: Matches documents where the specified condition is not true.

  • $nor: Matches documents where none of the specified conditions are true.

Element Operators

  • $exists: Matches documents that have the specified field.

  • $type: Matches documents where the specified field is of the specified BSON type.

Element Operators

Evaluation operators perform operations on specific fields and values, such as regular expression searches or checking the size of arrays. Some examples include:

  • $expr: Allows the use of aggregation expressions within query language.

  • $jsonSchema: Matches documents that fulfill the specified JSON Schema.

  • $mod: Matches documents where the specified field has a value divisible by a divisor and equal to a remainder.

  • $regex: Matches documents where the specified field contains a string that matches the provided regular expression pattern.

  • $text: Performs text search on the content of indexed fields in the documents.

  • $where: Matches documents that satisfy a JavaScript expression.

Evaluation Operators

Evaluation operators perform operations on specific fields and values, such as regular expression searches or checking the size of arrays. Some examples include:

  • $expr: Allows the use of aggregation expressions within query language.

  • $jsonSchema: Matches documents that fulfill the specified JSON Schema.

  • $mod: Matches documents where the specified field has a value divisible by a divisor and equal to a remainder.

  • $regex: Matches documents where the specified field contains a string that matches the provided regular expression pattern.

  • $text: Performs text search on the content of indexed fields in the documents.

  • $where: Matches documents that satisfy a JavaScript expression.

Array Operators

  • $all: Matches documents where an array field contains all specified values.

  • $elemMatch: Matches documents where an array field contains at least one element that matches the specified conditions.

  • $size: Matches documents where an array field contains a specified number of elements.

Bitwise Operators

  • $bitsAllClear: Matches documents where all bits of the specified field are clear (0) in the specified bitmask.

  • $bitsAllSet: Matches documents where all bits of the specified field are set (1) in the specified bitmask.

  • $bitsAnyClear: Matches documents where any bits of the specified field are clear (0) in the specified bitmask.

  • $bitsAnySet: Matches documents where any bits of the specified field are set (1) in the specified bitmask.

//$project
db.users.aggregate([
  {
    $project: {
      posts: 0,
      name:1
    },
  },
]);

//you can just use the second parameter in the find
db.books.find({}, { title: 1, author: 1, _id: 0 });

//return only the first 3 elements of the tags
db.collection.find({}, { tags: { $slice: [0,3] } });

//eq
                    //field:{operator:value}
db.collection.find({ field: { $eq: value } });
//or with aggregator
db.products.aggregate([
  {
    $addFields: {
      discounted: {
        //operator:["field":"value"]
        $eq: ['$price', 50], //return a true or false value
      },
    },
  },
]);

the $in operator allows you to specify an array of values and filter documents based on whether their field value exists within that array.

db.articles.find({ tags: { $in: ['NoSQL', 'javascript'] } });
//$in is OR [NoSQL or Javascript]
//$all is AND
// *AND*
db.orders.find({ $and: [{ price: { $gt: 1 } }, { quantity: { $lt: 10 } }] });
//OR
db.products.find({
  $or: [{ category: 'Fruits' }, { price: { $lte: 15 } }],
});

Logical opertors are always in an array, as you have to compare them.

Developer Tools

Developer tools aim to help you manage, interact, and visualize your data to make development tasks quicker and easier.

MongoDB Shell, also known as mongo, is a command-line interface (CLI) that allows you to interact with a MongoDB instance. You can use the mongo shell to perform CRUD operations, administrative tasks, and manage your databases.

MongoDB Compass is a graphical user interface (GUI) that simplifies the process of managing your MongoDB data. With Compass, you can visually explore and interact with your data, modify and sort documents, create indexes, and validate data schemas for better data governance.

MongoDB Atlas is a fully-managed cloud-based database platform offering the best of MongoDB. Its intuitive interface provides an effortless deployment experience, automated backups, self-healing recovery, and many other features that make it an ideal choice for database management.