The document, which is comprised of field/value pairs, is at the heart of the MongoDB data structure. Most interactions with MongoDB occur at the document level.
A field can contain a single value, multiple fields, or multiple elements.
A value made up of multiple fields is referred to as an embedded document and is assigned the Object data type (see field cars
in the screenshot). When rendered in the JSON format, an embedded document is enclosed in curly braces and adheres to the same structure as the outer (main) document.
A value made up of multiple elements is referred to as an array and is assigned the Array data type (see field Profession
in the screenshot). When rendered in the JSON format, an array is enclosed in square brackets, with each element separated by a comma. An element can be a scalar value (like in the field Profession
) or an embedded document (like in the field cars
).
MongoDB Data Types
A field can be one of these MongoDB data types:
Type | Alias |
---|---|
Double | “double” |
String | “string” |
Object | “object” |
Array | “array” |
Binary data | “binData” |
Undefined | “undefined” |
ObjectId | “objectId” |
Boolean | “bool” |
Date | “date” |
Null | “null” |
Regular Expression | “regex” |
DBPointer | “dbPointer” |
JavaScript | “javascript” |
Symbol | “symbol” |
JavaScript (with scope) | “javascriptWithScope” |
32-bit integer | “int” |
Timestamp | “timestamp” |
64-bit integer | “long” |
Decimal128 | “decimal” |
Min key | “minKey” |
Max key | “maxKey” |
Terms you might not know:
data type: an attribute that tells what kind of data that value can have. Common data types include: integers, strings, floating point values (source)
embedded document: related data attached in a single structure or document (source)
scalar value: a variable that holds one value at a time. It is a single component that assumes a range of number or string values (source)