Introduction to data types of mongodb


MongoDBwithBSONone kindserializeStore data in binary format. The document before each serialization supports the data types in the following list, and each data type has a correspondingnumberandcharacter stringAlias. Can be used in$typeOperator for querying documents

data type

Type Number String Notes
Double 1 “double” —–
character string 2 “string” —–
object 3 “object” —–
array 4 “array” —–
binary data 5 “binData” —–
Undefined 6 “undefined” Expired
ObjectId 7 “objectId” —–
Boolean 8 “bool” —–
date 9 “date” —–
empty 10 “null” —–
regular expression 11 “regex” —–
DBPointer 12 “dbPointer” Expired
JavaScript (code) 13 “javascript” This data type is used to store JavaScript code in a document
Symbol 14 “symbol” Expired
JavaScript (with range) 15 “javascriptWithScope” —–
32-bit integer 16 “int” —–
time stamp 17 “timestamp” —–
64 bit integer 18 “long” —–
Decimal128 19 “decimal” New in version 3.4
Min key -1 “minKey” —–
Max key 127 “maxKey” —–

You can usetypeofperhapsinstanceofinstanceof/typeofDetermine the type of a field value

The following code examples are inMongo shellImplemented in

Here are some important data types


ObjectIdSimilar to the unique primary key, it can quickly survive and sort, including12 bytes, meaning:

  • The first four bytes indicate the creationunixTimestamp, Greenwich mean timeUTCTime, 8 hours later than Beijing time
  • The next three bytes are the machine identification code
  • The next two bytes consist of the process ID (PID)
  • The last three bytes are random numbers

Introduction to data types of mongodb
MongoDBDocuments stored in must have a_idKey. The value of this key can be of any type, and the default is oneObjectIdobject

becauseObjectIdThe created timestamp is saved in, so you don’t need to save the timestamp field for your document. You cangetTimestampFunction to get the creation time of the document:

> var newObject = ObjectId()
> newObject.getTimestamp()

ObjectIdConvert to string

> newObject.str

character string

Bson strings are UTF-8 encoded

time stamp

BSONThere is a special timestamp type forMongoDBInternal use, with ordinarydateType is irrelevant. The timestamp value is a 64 bit value. Of which:

  • The first 32 bits are a time_ T value (seconds from the UNIX era)
  • The last 32 bits are an incremental number of operations in a secondOrdinal number

staySingle mongod instanceIn, timestamp values are usually unique

In the replication set,oplogThere is onetsField. The value in this field represents the operation time using the bson timestamp

The bson timestamp type is mainly used for mongodbinsideuse. In most applications, you can use bson date type


Represents the current distanceUnixThe number of milliseconds in the new era (January 1, 1970). The date type is signed, and a negative number represents the date before 1970

>Var mydate1 = new date() // Greenwich mean time
> mydate1
> typeof mydate1
>Var mydate2 = isodate() // Greenwich mean time
> mydate2
> typeof mydate2

The time created in this way is a date type and can be usedJSMediumDateType method

Returns a string of time type

> var mydate1str = mydate1.toString()
> mydate1str
Sat Nov 25 2017 15:38:00 GMT+0800 (CST)
> typeof mydate1str


> Date()
Sat Nov 25 2017 15:43:28 GMT+0800 (CST)


This type is used to store Boolean values:true / false

Introduction to data types of mongodb

Scan attention, view more articles and improve programming ability