Wednesday, 30 November 2016

MongoDB Basics II - Data Types

For MongoDB version 3.4 the data types are as follows

TypeNumberAliasNotes
Double1“double”stores floating point values
String2“string”stores UTF8 text
Object3“object”stores embedded documents
Array4“array”can store multiple values of multiple data types
Binary data5“binData”stores binary data
Undefined6“undefined”Deprecated. Works like Null
ObjectId7“objectId”stores the document's ID. 12 bytes, system generated
Boolean8“bool”true or false
Date9“date”64-bit integer representing milliseconds since 01.01.1970
Null10“null”
Regular Expression11“regex”
DBPointer12“dbPointer”Deprecated.
JavaScript13“javascript”stores JavaScript data without Scope
Symbol14“symbol”Deprecated.
JavaScript (with scope)15“javascriptWithScope”stores JavaScript data with Scope
32-bit integer16“int”
Timestamp17“timestamp”64-bit integer. 32 bits representing seconds since 01.01.1970 + 32 bits incrementing. Unique per Mongo instance
64-bit integer18“long”
Decimal12819“decimal”New in version 3.4
 Wikipedia:Decimal128_floating-point_format
Min key-1“minKey”always the lowest result in a sort
Max key127“maxKey”always highest result in a sort

Each data type has a number and string alias that can be used with the $type operator to query documents by BSON type. Additionally, there is a "number" alias that corresponds to all the numeric data-types.

db.addresses.find( { "areaCode" : { $type : "number" } } )
This will find all the Documents in the addresses Collection where areaCode is numeric
 
db.addresses.find( { "areaCode" : { $type : "string" } } )
This will find all the Documents in the addresses Collection where areaCode is text

Types can be implicitly defined:
{
 screenname: "Max",                    //  string
 age: 29,                              // integer
 visible: false,                       // boolean
 subscriptions: ["mongoDB","MariaDB"]  // array
 "phone" : {"cell" : "555-123-4567",
            "office" : "555-312-6789"},// Object (embedded document)

 email: null                           // null
}


or explicitly defined:
{
 "_id" : ObjectId("5b6b9f34a1f359177d123a5a"),
 field1: NumberLong(123456) ,
 field2: NumberInt(123456789)
}





No comments:

Post a Comment