Mongodb Guide (translated) (12)-developer zone-data types and conventions (1)

Source: Internet
Author: User
Tags mongo shell

MongoDB (BSON) Data Type

In addition to the basic JSON types: string, integer, boolean, double, null, array, and object, mongo also uses special data types. These types include date, object id, binary data, regular expression, and code. Each driver implements these types in a specific language. view your driver documentation for detailed information.

Check the data type from shell

In shell, float and integer types are considered as standard javascript numbers, so they cannot be separated.

> // v1.8+ shell
> x
{
"_id" : ObjectId("4dcd3ebc9278000000005158"),
"d" : ISODate("2011-05-13T14:22:46.777Z"),
"b" : BinData(0,""),
"c" : "aa",
"n" : 3,
"e" : [ ],
"n2" : NumberLong(33)
}
> x.d instanceof Date
true
> x.b instanceof BinData
true
> typeof x
object
> typeof x.b
object
> typeof x.n
number
> typeof x.n
number
> typeof x.n2
object
> x.n2 instanceof NumberLong
true
> typeof x.c
string

Timestamp Data Type

In mongodb, BSON contains timestamp data types with special semantics.

The timestamp is stored as 64-bit and is unique in the same mongod. The first 32 bits are time_t values (from UTC time to the current number of seconds ). The next 32 bits are the incremental sequence of operations within the same second.

Mongodb uses the timestamp data type as "OpTimes" for the field "ts" in the oplog of the replication group ".

When it is null, the timestamp has special semantics. If it is null and the timestamp is one of the first two fields of the object, the timestamp is automatically converted to a unique value. (It must be one of the first two top-level fields for performance reasons; the entire document does not scan the timestamp .)

The following is an example of mongo shell (v1.7.5 or later ).

> // not one of the first 2 fields
> db.foo.insert( { x : 1, y : new Timestamp() } )
> db.foo.find()
{ "_id" : ObjectId("4d1d4ce78b1a04eeb294c098"), "x" : 1, "y" : { "t" : 0, "i" : 0 } }
> // in first 2 fields, auto fill of value works
> db.foo.drop()
> db.foo.insert( { y : new Timestamp(), x : 3 } )
> // the shell displays timestamps as { t : ..., i : ... } where t is the time
> // component and i is the ordinal component
> db.foo.find()
{ "_id" : ObjectId("4d1d4cfd8b1a04eeb294c099"), "y" : { "t" : 1293765885000, "i" : 1 }, "x" : 3 }
> db.foo.drop()
> for( var i = 0; i < 10; i++ ) db.foo.insert({y:new Timestamp(), x : i})
> db.foo.find()
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c09a"), "y" : { "t" : 1293765911000, "i" : 1 }, "x" : 0 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c09b"), "y" : { "t" : 1293765911000, "i" : 2 }, "x" : 1 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c09c"), "y" : { "t" : 1293765911000, "i" : 3 }, "x" : 2 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c09d"), "y" : { "t" : 1293765911000, "i" : 4 }, "x" : 3 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c09e"), "y" : { "t" : 1293765911000, "i" : 5 }, "x" : 4 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c09f"), "y" : { "t" : 1293765911000, "i" : 6 }, "x" : 5 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c0a0"), "y" : { "t" : 1293765911000, "i" : 7 }, "x" : 6 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c0a1"), "y" : { "t" : 1293765911000, "i" : 8 }, "x" : 7 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c0a2"), "y" : { "t" : 1293765911000, "i" : 9 }, "x" : 8 }
{ "_id" : ObjectId("4d1d4d178b1a04eeb294c0a3"), "y" : { "t" : 1293765911000, "i" : 10 }, "x" : 9 }
>

UTC DateTime Data Type

According to the BSON Date/Time data type, it is called UTC DateTime. (There is another timestamp data type, but it is a special internal type of mongodb and you should not use it ). UTC DateTime stores the number of milliseconds since the Unix epoch (Jan 1, 1970) as a 64-bit integer data. This integer is signed, so it is stored as a negative number before 1970.

However, mongodb versions earlier than 1.9.1 incorrectly resolves the DateTime number to an unsigned integer, which affects the index of the sort, range query, and DateTime fields. The index will not be rebuilt during the upgrade. Therefore, if you use a program earlier than version 1.9.1 To create an index on a field of the DateTime type, and then upgrade it to a program later than or equal to version 1.9.1, some Indexes sort dates by unsigned integers (those earlier than date 1970 are ranked after or equal to date 1970), which affects sorting and range query. To solve this problem, you must discard and recreate your index.

Internationalized string

Mongodb supports strings in UTF-8 format stored in objects and queries. (Particularly, BSON strings are UTF-8 .)

Generally, the drivers of each language convert the strings of the language into UTF-8 during serialization and deserialization of BSON. For example, the java driver converts a java unicode string to a UTF-8 during serialization.

This means that in most cases you can efficiently store most international characters to mongodb strings. Some reminders:

  • Mongodb Regular Expression query supports the use of UTF-8 in regular expression strings.
  • Currently, sort () on a string uses strcmp: the sorting order may be reasonable, but it is not completely correct in the international data. Future mongodb versions may support full UTF-8 ordering.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.