Mongodb Data Types - MongoDB Tutorial



Mongodb Data Types

  • MongoDB stores documents on disk in the BSON (Binary Structured Object Notation) serialization format.
  • BSON is a binary representation of JSON documents, though BSON data format provides more data types than JSON.
  • The mongo JavaScript shell and the MongoDB language drivers translate between BSON and the language-specific document representation.

MongoDB Data Types and Corresponding ID Number

Type Description Alias ID
Double Represents a float value “double” 1
String
  • BSON strings are UTF-8.
  • The language’s string format to UTF-8
    when serializing and deserializing BSON.
  • This makes it possible to store most international
    characters in BSON strings with ease.
  • In addition, MongoDB $regex queries support
    UTF-8 in the regex string.
“string” 2
Object
  • Objects represents an embedded document.
“object” 3
Array
  • Sets or lists of values can be represented
    as arrays.
“array” 4
Binary data
  • Binary data is a string of arbitrary bytes;
    it cannot be manipulated from the shell.
“binData” 5
Object id
  • Object ID is an identifier of the document and
    equivalent to a Primary key.
  • Object Id are:
    • Small
    • Likely unique
    • Fast to generate, and ordered.These values
      consist of 12-bytes
    • Where the first four bytes are a timestamp.
    • That reflect the ObjectId’s creation.
“objectId” 7
Boolean
  • A logical true or false.
  • Use to evaluate whether a condition is true or false
“bool” 8
Date
  • BSON Date is a 64-bit integer that represents the
    number of milliseconds.
“date” 9
Null
  • It represents both a null value and a nonexistent
    field.
“null” 10
Regular Expression
  • RegExp maps directly to a Javascript RegExp
“regex” 11
JavaScript “javascript” 13
Symbol
  • Not supported by the shell. If the shell gets
    a symbol from the database, it will
    convert it into a string.
“symbol” 14
JavaScript (with scope) “javascript
WithScope”
15
32-bit integer
  • Numbers without decimal points will be saved as
    32-bit integers.
“int” 16
Timestamp
  • BSON has a special timestamp type for internal
    MongoDB use and is not associated with the
    regular Date type.
  • Timestamp values are a 64 bit value where:
    • The first 32 bits are a time_t value
      (seconds since the Unix epoch).
    • The second 32 bits are an incrementing
      ordinalfor operations
      within a given second.
“int” 16
64-bit integer
  • Numbers without a decimal point will be saved
    and returned as 64-bit integers.
“long” 18
Min key
  • MinKey compare less than all other possible
    BSON element values, respectively, and exist
    primarily for internal use.
“minKey” 255
Max key
  • MaxKey compare greater than all other possible
    BSON element values, respectively, and exist
    primarily for internal use.
“maxKey” 127

Comparing values of different BSON types

When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest :


Order Data Types
1 MinKey (internal type)
2 Null
3 Numbers (ints, longs, doubles)
4 Symbol, String
5 Object
6 Array
7 BinData
8 ObjectId
9 Boolean
10 Date, Timestamp
11 Regular Expression

Related Searches to Mongodb Data Types - MongoDB Tutorial