Sling Academy
Home/MongoDB/MongoDB Integer & Double data types: A practical guide (with examples)

MongoDB Integer & Double data types: A practical guide (with examples)

Last updated: February 03, 2024

Introduction

MongoDB is a powerful and flexible NoSQL document database that has gained widespread popularity for its ability to handle large volumes of data, its schema-less structure, and its impressive flexibility. One of the keys to utilizing MongoDB effectively is understanding the various data types it supports, especially those relating to numbers such as Integer and Double.

This practical guide will delve into MongoDB’s handling of Integer and Double data types, providing examples to illustrate how these types are used, manipulated, and queried within a MongoDB environment. Understanding these data types is essential for accurately storing and processing numerical data in your MongoDB collections.

Getting Started with MongoDB and its Data Types

Before diving into the specifics of Integer and Double data types, let’s take a moment to understand MongoDB’s approach to data storage. MongoDB stores data in JSON-like documents that allow for varied and nested structures. Among the basic data types MongoDB supports are:

  • String
  • Integer
  • Double
  • Boolean
  • Array
  • Object
  • Date
  • Null

For this guide, the focus will be on Integer and Double data types – two types integral for numerical computations and data representation.

Understanding Integer and Double Data Types

In MongoDB, the Integer and Double types represent numeric values, but they cater to different ranges and precisions. Integer, typically a 32-bit or 64-bit number depending on the environment, is best suited for whole numbers. In contrast, Double is a floating-point number, making it ideal for precision calculations that include decimals.

Example 1: Inserting an Integer

db.numbers.insertOne({"name": "one", "value": 1})

This example inserts an Integer type into the database. The value is straightforwardly a whole number.

Example 2: Inserting a Double

db.numbers.insertOne({"name": "pi", "value": 3.14159})

This time, the value is a Double, which can handle decimal points unlike the Integer type.

Querying Integer and Double DataTypes

Querying based on these numeric types can be done precisely, enabling you to retrieve documents that match specific numeric criteria.

Example 3: Querying for an Integer Value

db.numbers.find({"value": {$type: "int"}})

This query retrieves documents where the ‘value’ field is specifically an Integer.

Example 4: Querying for a Double Value

db.numbers.find({"value": {$type: "double"}})

Similarly, this query fetches documents where the ‘value’ is a Double type, demonstrating the ability to distinguish between numeric types.

Advanced Manipulation and Aggregation

MongoDB provides powerful aggregation operations that can perform complex calculations across your documents. For numeric data types like Integer and Double, these capabilities are particularly powerful.

Example 5: Summation of Integer Values

db.numbers.aggregate([
    {
        $match: {
            value: {
                $type: "int"
            }
        }
    },
    {
        $group: {
            _id: null,
            total: {
                $sum: "$value"
            }
        }
    }
])

This aggregation pipeline matches documents with an Integer ‘value’, then sums them up, showcasing the simplicity and efficiency with which MongoDB processes numerical data.

Example 6: Calculating the Average of Double Values

db.numbers.aggregate([
    {
        $match: {
            value: {
                $type: "double"
            }
        }
    },
    {
        $group: {
            _id: null,
            avgValue: {
                $avg: "$value"
            }
        }
    }
])

This example demonstrates calculating the average of Double values, utilizing MongoDB’s aggregation capabilities to perform precise floating-point arithmetic.

Conclusion

Understanding and effectively utilizing Integer and Double data types within MongoDB can significantly enhance your database’s flexibility and performance. Through the practical examples provided, we’ve seen how MongoDB handles these numeric types, from basic insertion and querying to more advanced aggregation capabilities. Mastery of these concepts will empower you to build more nuanced and powerful data models in your MongoDB applications.

Next Article: Working with String data type in MongoDB (with examples)

Previous Article: MongoDB Int32 and Long data types: A practical guide (with examples)

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)