- Introduction to MongoDB >
- BSON Types
BSON Types¶
On this page
BSON is a binary serialization format used to store documents and make remote procedure calls in MongoDB. The BSON specification is located at bsonspec.org.
BSON supports the following data types as values in documents. Each data
type has a corresponding number and string alias that can be used with the
$type
operator to query documents by BSON type.
Type | Number | Alias | Notes |
---|---|---|---|
Double | 1 | “double” | |
String | 2 | “string” | |
Object | 3 | “object” | |
Array | 4 | “array” | |
Binary data | 5 | “binData” | |
Undefined | 6 | “undefined” | Deprecated. |
ObjectId | 7 | “objectId” | |
Boolean | 8 | “bool” | |
Date | 9 | “date” | |
Null | 10 | “null” | |
Regular Expression | 11 | “regex” | |
DBPointer | 12 | “dbPointer” | Deprecated. |
JavaScript | 13 | “javascript” | |
Symbol | 14 | “symbol” | Deprecated. |
JavaScript (with scope) | 15 | “javascriptWithScope” | |
32-bit integer | 16 | “int” | |
Timestamp | 17 | “timestamp” | |
64-bit integer | 18 | “long” | |
Min key | -1 | “minKey” | |
Max key | 127 | “maxKey” |
To determine a field’s type, see Check Types in the mongo Shell.
If you convert BSON to JSON, see the Extended JSON reference.
Comparison/Sort Order¶
When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest:
- MinKey (internal type)
- Null
- Numbers (ints, longs, doubles)
- Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
- Regular Expression
- MaxKey (internal type)
MongoDB treats some types as equivalent for comparison purposes. For instance, numeric types undergo conversion before comparison.
Changed in version 3.0.0: Date objects sort before Timestamp objects. Previously Date and Timestamp objects sorted together.
The comparison treats a non-existent field as it would an empty BSON
Object. As such, a sort on the a
field in documents { }
and {
a: null }
would treat the documents as equivalent in sort order.
With arrays, a less-than comparison or an ascending sort compares the
smallest element of arrays, and a greater-than comparison or a
descending sort compares the largest element of the arrays. As such,
when comparing a field whose value is a single-element array (e.g. [
1 ]
) with non-array fields (e.g. 2
), the comparison is between
1
and 2
. A comparison of an empty array (e.g. [ ]
) treats
the empty array as less than null
or a missing field.
MongoDB sorts BinData
in the following order:
- First, the length or size of the data.
- Then, by the BSON one-byte subtype.
- Finally, by the data, performing a byte-by-byte comparison.
The following sections describe special considerations for particular BSON types.
ObjectId¶
ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId values consists of 12-bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation, specifically:
- a 4-byte value representing the seconds since the Unix epoch,
- a 3-byte machine identifier,
- a 2-byte process id, and
- a 3-byte counter, starting with a random value.
In MongoDB, each document stored in a collection requires a unique
_id field that acts as a primary key. If an inserted
document omits the _id
field, the MongoDB driver automatically
generates an ObjectId for the _id
field.
This also applies to documents inserted through update operations with upsert: true.
MongoDB clients should add an _id
field with a unique ObjectId.
Using ObjectIds for the _id
field provides the following additional
benefits:
in the
mongo
shell, you can access the creation time of theObjectId
, using theObjectId.getTimestamp()
method.sorting on an
_id
field that storesObjectId
values is roughly equivalent to sorting by creation time.Important
While ObjectId values should increase over time, they are not necessarily monotonic. This is because they:
- Only contain one second of temporal resolution, so ObjectId values created within the same second do not have a guaranteed ordering, and
- Are generated by clients, which may have differing system clocks.
See also
String¶
BSON strings are UTF-8. In general, drivers for each programming
language convert from 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.
[1] In addition, MongoDB
$regex
queries support UTF-8 in the regex string.
[1] | Given strings using UTF-8
character sets, using sort() on strings
will be reasonably correct. However, because internally
sort() uses the C++ strcmp api, the
sort order may handle some characters incorrectly. |
Timestamps¶
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
ordinal
for operations within a given second.
Within a single mongod
instance, timestamp values are
always unique.
In replication, the oplog has a ts
field. The values in
this field reflect the operation time, which uses a BSON timestamp
value.
Note
The BSON timestamp type is for internal MongoDB use. For most cases, in application development, you will want to use the BSON date type. See Date for more information.
If you insert a document containing an empty BSON timestamp in a top-level field, the MongoDB server will replace that empty timestamp with the current timestamp value. For example, if you create an insert a document with a timestamp value, as in the following operation:
Then, the db.test.find()
operation
will return a document that resembles the following:
If ts
were a field in an embedded document, the server would have left it as an
empty timestamp value.
Changed in version 2.6: Previously, the server would only replace empty timestamp values in the first
two fields, including _id
, of an inserted document. Now MongoDB will
replace any top-level field.
Date¶
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
The official BSON specification refers to the BSON Date type as the UTC datetime.
BSON Date type is signed. [2] Negative values represent dates before 1970.
Example
Construct a Date using the new Date()
constructor in the
mongo
shell:
Example
Construct a Date using the ISODate()
constructor in the
mongo
shell:
Example
Return the Date
value as string:
Example
Return the month portion of the Date value; months are zero-indexed,
so that January is month 0
:
[2] | Prior to version 2.0, Date values were
incorrectly interpreted as unsigned integers, which affected
sorts, range queries, and indexes on Date fields. Because
indexes are not recreated when upgrading, please re-index if you
created an index on Date values with an earlier version, and
dates before 1970 are relevant to your application. |