Learning MongoDB

0% completed

Previous
Next
DataTypes in MongoDB

In MongoDB, data is stored in BSON format, which supports a variety of data types. Each data type plays a crucial role in how data is stored, queried, and manipulated.

This lesson will provide an in-depth explanation of each BSON data type used in MongoDB, along with examples to illustrate their usage.

BSON Data Types in MongoDB

Here is a detailed explanation of each BSON data type with examples:

In each example, we used the insertOne() method to insert the data into the current database. It takes the JSON object as a parameter. We will learn it in the next chapter.

  1. String

    • Stores UTF-8 encoded text. Strings are the most commonly used data type to represent text data.
    • Example:
      db.myCollection.insertOne({ name: "John Doe" })
  2. Integer

    • Represents 32-bit or 64-bit signed integers. Used for storing numerical data without decimal points.
    • Example:
      db.myCollection.insertOne({ age: 30 })
  3. Double

    • Stores 64-bit IEEE 754 floating-point numbers. Suitable for storing numbers with decimal points.
    • Example:
      db.myCollection.insertOne({ price: 19.99 })
  4. Boolean

    • Represents a Boolean value, either true or false.
    • Example:
      db.myCollection.insertOne({ isStudent: false })
  5. Null

    • Represents a null value. Used to explicitly set a field to null.
    • Example:
      db.myCollection.insertOne({ middleName: null })
  6. Array

    • An ordered list of values. Arrays can contain multiple types of data, including other arrays and objects.
    • Example:
      db.myCollection.insertOne({ tags: ["mongodb", "database", "NoSQL"] })
  7. Object

    • A collection of key-value pairs, where keys are strings and values can be any BSON data type. Used for embedding documents within other documents.
    • Example:
      db.myCollection.insertOne({ address: { street: "123 Main St", city: "Anytown", zipcode: "12345" } })
  8. Binary Data

    • A byte array. Useful for storing data like images or files in a binary format.
    • Example:
      db.myCollection.insertOne({ fileData: new BinData(0, "binarydatahere") })
  9. ObjectId

    • A unique identifier for documents. MongoDB automatically generates an _id field of type ObjectId if none is provided.
    • Example:
      db.myCollection.insertOne({ _id: ObjectId("507f1f77bcf86cd799439011") })
  10. Date

    • Stores the date and time in milliseconds since the Unix epoch (January 1, 1970). Used for storing dates and times.
    • Example:
      db.myCollection.insertOne({ createdAt: new Date() })
  11. Regular Expression

    • Represents a regex pattern. Useful for pattern matching within strings.
    • Example:
      db.myCollection.insertOne({ pattern: /abc/i })
  12. JavaScript

    • Stores JavaScript code as a string. Can be executed in the context of the document.
    • Example:
      db.myCollection.insertOne({ code: function() { return "Hello, MongoDB!"; } })
  13. JavaScript with Scope

    • Stores JavaScript code along with a scope object. The scope provides variables that the code can access.
    • Example:
      db.myCollection.insertOne({ codeWithScope: { $code: "function() { return x; }", $scope: { x: 1 } } })
  14. Timestamp

    • A special internal timestamp used by MongoDB for internal operations. Not typically used directly in applications.
    • Example:
      db.myCollection.insertOne({ timestamp: Timestamp() })
  15. Decimal128

    • A 128-bit decimal-based floating-point number. Useful for high-precision calculations, such as financial data.
    • Example:
      db.myCollection.insertOne({ bigNumber: NumberDecimal("12345.6789") })
  16. Min Key and Max Key

    • Special types used for internal comparisons. MinKey is the lowest possible value and MaxKey is the highest possible value.
    • Example:
      db.myCollection.insertOne({ minKeyField: MinKey(), maxKeyField: MaxKey() })

Understanding the various BSON data types in MongoDB is essential for effectively storing and manipulating data. Each data type has specific characteristics and use cases that can significantly impact the performance and functionality of your database. By leveraging the appropriate data types, you can ensure that your MongoDB applications are both efficient and capable of handling a wide range of data structures.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next