JSON is a widely used lightweight data interchange format that supports today's most mainstream development languages. In recent years, the rise of Mongdb has adopted a JSON-like data format, enriched and enhanced over JSON, allowing MongoDB to handle and complain about larger data types. In this paper, the data types supported by MongoDB are given with 2 descriptions.
First, JSON characteristics
1, what's the matter JSON
JSON (JavaScript Object notation) is a lightweight data interchange format.
JSON uses a completely language-independent text format, but it also uses the same habits as C-language families (including C, C + +, C #, Java, JavaScript, Perl, Python, and so on).
These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parsing and generation (generally used to improve network transmission rate).
Can be detailed reference: http://www.json.org.cn/
2. JSON Data Writing format
Name/value pair Object
is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends.
Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma).
such as: ({"FirstName": "John"}), more formats see the data types supported by part Bson
Equivalent to this JavaScript statement: firstname= "John"
3. JSON supports only the following data types
Number (integer or floating-point numbers)
String (in double quotes)
Logical value (TRUE or FALSE)
Array (in square brackets)
Object (in curly braces)
Null
4. JSON is based on two types of structures:
A collection of name/value pairs (a collection of name/value pairs) that have different descriptions in different programming languages
such as: Objects (object), records (record), structure (struct), Dictionary (dictionary)
Hash table, a list of keys (keyed list), or associative array (associative array)
An ordered list of values. In most languages, it is implemented as arrays, vectors (vectors), lists (list), sequences (sequence)
Second, Bson characteristics
1, what is Bson
Bson () is a binary form of class JSON storage format, referred to as binary JSON
Like JSON, it supports embedded document objects and array objects, but Bson has some data types that JSON does not have, such as date and bindata types.
https://docs.mongodb.com/manual/reference/bson-types/
2, the specific Bson
Lightweight, ergodicity, high efficiency
3, MongoDB and Bson
MongoDB has made some additions to the JSON string, enabling it to support more data types and use it as a storage structure
MongoDB This format into the concept of a document, because Bson is schema-free, so the document corresponding to the MongoDB also has this feature
One of the important reasons for MongoDB to use Bson as its storage structure is its ergodicity
4. Demo MongoDB Supported data types
Null value
Db.mycol.insert ({x:null})
Writeresult ({"ninserted": 1})
Boolean type
Db.mycol.insert ({x:true})
Writeresult ({"ninserted": 1})
Decimal
Db.mycol.insert ({x:3.1515})
Writeresult ({"ninserted": 1})
Integer
Db.mycol.insert ({x:3})
Writeresult ({"ninserted": 1})
4 byte band matches integer
Db.mycol.insert ({x:numberint ("3")})
Writeresult ({"ninserted": 1})
8-byte signed integer
Db.mycol.insert ({x:numberlong ("3")})
Writeresult ({"ninserted": 1})
Character type
Db.mycol.insert ({x: "Robin"})
Writeresult ({"ninserted": 1})
Date type
Db.mycol.insert ({x:new Date ()})
Writeresult ({"ninserted": 1})
Regular expressions
Db.mycol.insert ({x:/u01/i})
Writeresult ({"ninserted": 1})
Array
Db.mycol.insert ({x:["a", "B", "C"]})
Writeresult ({"ninserted": 1})
Nested documents
Db.mycol.insert ({x:{y: "Nested"}})
Writeresult ({"ninserted": 1})
Object ID
Db.mycol.insert ({X:objectid ()})
Writeresult ({"ninserted": 1})
Code Snippets
Db.mycol.insert ({x:function () {/This is a test code/}})
Writeresult ({"ninserted": 1})
Find documents for collection MyCol
Db.mycol.find ({},{_id:0})
{"X": null}
{' X ': true}
{"X": 3.1515}
{"X": 3}//author:leshami
{"X": 3}//blog:http://blog.csdn.net/leshami
{"X": Numberlong (3)}
{"X": "Robin"}
{"X": Isodate ("2016-09-06t02:46:35.173z")}
{"X":/u01/i}
{"X": ["a", "B", "C"]}
{"X": {"y": "Nested"}}
{"X": ObjectId ("57ce2f34ce8685a6fd9df3ae")}
{' X ': function () {/This is a test code/}}
Undefined type
Db.mycol.insert ({name:undefined});
Writeresult ({"ninserted": 1})
Db.mycol.insert ({name: "123"});
Writeresult ({"ninserted": 1})
Printjson (Db.mycol.find (). ToArray ())
[
{
"_id": ObjectId ("57ce1cc8c60f1fe489e49c68"),
' Name ': undefined
},
{
"_id": ObjectId ("57ce1cdbc60f1fe489e49c69"),
' Name ': ' 123 '
}
]
Find documents of type undefined
Db.mycol.find ({name: "undefined"})
Db.mycol.find ({name:undefined})
Error:error: {
"Waitedms": Numberlong (0),
"OK": 0,
"ErrMsg": "Cannot compare to undefined",
"Code": 2
}
Db.mycol.find ({name:{$type: 6}})
{"_id": ObjectId ("57ce1cc8c60f1fe489e49c68"), "name": undefined}
Db.mycol.find ({name:{$type: "Undefined"}})
{"_id": ObjectId ("57ce1cc8c60f1fe489e49c68"), "name": undefined}
Comparison and sorting priority of MONGODB data types
1) Minkey (internal type)
2) Null
3) Numbers (INTs, longs, doubles)
4) Symbol, String
5) Object
6) Array
7) Bindata
8) ObjectId
9) Boolean
) Date
One) Timestamp
) Regular Expression
Maxkey (internal type)
5, about _id and object_id
Each document in MongoDB must have a "_id" key that is equivalent to a primary key in the RDBMS, except that the primary key is automatically generated by the MongoDB
The value of the "_id" key can be of any type and can be created without system creation, while user-defined rules generate
"_ID" is lightweight, globally unique, can be compared to the MySQL data in the Gtid, but also to solve the different machine replica set replication when the uniqueness of the problem
A 4-byte value representing the seconds since the Unix epoch,//timestamp
A 3-byte machine identifier,//Machine Unique identification code
A 2-byte process ID, and//processes ID
A 3-byte counter, starting with a random value. Random number
Db.mycol.findOne ()
{"_id": ObjectId ("57ce2d4cce8685a6fd9df3a3"), "X": null}
57CE2D4C//time stamp ==>1473129804 ==> 2016/9/6 10:43:24
ce8685//Machine Unique identification code
A6FD//Process ID
9DF3A3//random number