The following table is a few of the data types commonly used in MongoDB.
Data Type |
Description |
String |
String. Data types commonly used to store data. In MongoDB, the UTF-8 encoded string is legal. |
Integer |
Integer value. Used to store numeric values. Depending on the server you are using, it can be divided into 32-bit or 64-bit. |
Boolean |
Boolean value. Used to store Boolean values (True/false). |
Double |
Double-precision floating-point value. Used to store floating-point values. |
Min/max keys |
Compares a value to the lowest and highest value of a BSON (binary JSON) element. |
Array |
Used to store an array or list or multiple values as a single key. |
Timestamp |
Time stamp. Record when the document was modified or added. |
Object |
Used for inline documents. |
Null |
Used to create a null value. |
Symbol |
Symbol. The data type is basically the same as the string type, but unlike it, it is typically used in languages with special symbol types. |
Date |
Date time. Use the UNIX time format to store the current date or time. You can specify your own datetime: Create a Date object and pass in the month-date information. |
Object ID |
The object ID. The ID used to create the document. ( each document is available ) |
Binary Data |
Binary data. Used to store binary data. |
Code |
The code type. Used to store JavaScript code in a document. |
Regular expression |
ObjectId
ObjectId similar to a unique primary key, can be quickly generated and sorted, including the bytes, meaning:
- The first 4 bytes represent the creation of a Unix timestamp, UTC Time of GMT, 8 hours later than Beijing time
- The next 3 bytes are the machine identification code
- The immediate two bytes are composed of the process ID of the PID
- The last three bytes are random numbers
The document stored in MongoDB must have a _id key. The value of this key can be any type, default is a ObjectId object
Since the created timestamp is saved in ObjectId, you do not need to save the timestamp field for your document, and you can get the document creation time by using the Gettimestamp function:
var newObject = ObjectId ()> newobject.gettimestamp () isodate (" 2017-11-25t07:21:10z")
ObjectId Convert to String
> newobject.str5a1919e63df83ce79df8b38f
String
BSON strings are UTF-8 encoded.
Time stamp
BSON has a special timestamp type that is used for MongoDB internal use and is not related to normal date types. The timestamp value is a 64-bit value. which
- The first 32 bits are a time_t value (the number of seconds that differs from the UNIX ERA)
- The last 32 bits are an increment of the operation in a second
序数
In a single Mongod instance, the timestamp value is usually unique.
In the replication set, Oplog has a TS field. The value in this field uses the Bson timestamp to indicate the operation time.
The BSON timestamp type is used primarily for MongoDB internal use. In most cases of application development, you can use the BSON date type.
Date
Represents the current number of milliseconds from the new Era of Unix (January 1, 1970). The date type is signed and the negative number represents the date before 1970.
>varMydate1 =NewDate ()//GMT>Mydate1isodate ("2018-03-04t14:58:51.233z")>typeofmydate1Object>varMydate2 = Isodate ()//GMT>Mydate2isodate ("2018-03-04t15:00:45.479z")>typeofMydate2Object
This creates a date type, and you can use the method of the date type in JS.
Returns a string of a time type:
var mydate1str = mydate1.tostring ()>2018: :wuyi gmt+0000typeof mydate1strstring
Or
>2018: gmt+0000 (UTC )
Data types supported by MongoDB