MongoDB supports a wide range of data types as values in documents, we will discuss those below
Basic Data Types
Documents in MongoDB can be thought of as "JSON-like" in that they are conceptually similar to objects in JavaScript. JSON expressive capability is limited as there are null, Boolean, numeric, string, array, and object.
Although these data types allow for an impressive amount of expressiveness. There are a couple of additional types which are crucial for most applications.
NULL
Null can be used to represent both a null value and nonexistent field
{"x" : null}
BOOLEAN
This is used to represent values 'true' and 'false'
{"x" : true}
64 Bit floating point number :
all numbers are represented with this data type, thus this will be a floating point number
{"x" : 3.14}
as this will be
{"x" : 3}
String
any string characters can be represented with this data type
{"x" : "header"}
Symbol
This is not supported in the MongoDB shell, it will be converted to a string
object_id
object id is 12 bytes unique ID for documents.
{"x" ObjectId()}
DATE
Dates are stored as milliseconds since the epoch, timezone is not stored.
{"x" : new Date()}
Regular Expression
Documents can contain regular expressions, using java scripts regular expression
{"x" : /header/i}
Code
Documents can also contain JavaScipt code
{"x" : function () {/*......*/}}
Binary Data
Binary data is a string of arbitrary bytes, It can not be manipulated from the shell.
Maximum Value
BSON contains a special type representing the largest possible value. MongoDB does not have this type
Minimum Value
BSON contains a special type representing the smallest possible value. MongoDB does not have this type
Undefined
Undefined can be used in documents as well
{"x" : undefined}
Array
sets or lists of values can be represented as arrays
{"x" : ["a", "b", "c"]}
Embedded Document
Documents can contain entire documents
{"x" : {"y": "z"}}
If you like it, please share and comment below if any additional information is required.
0 comments:
Post a Comment