Introduction and usages of Bson in MongoDB

Source: Internet
Author: User
Tags json mongodb


This article mainly introduced the Bson introduction and the use example in the MongoDB, this article explained what is Bson, Bson in the MongoDB use, several Bson example and so on content, needs the friend may refer to under



One, what is Bson



Bson is a binary form of class JSON, called Binary JSON, which, like JSON, supports embedded document objects and array objects, but Bson has some data types that JSON does not have, such as date and bindata types.



Bson can be used as a storage form for network data exchange, a bit similar to Google's protocol Buffer, but Bson is a kind of schema-less storage form, its advantage is high flexibility, but its disadvantage is that the space utilization is not very ideal, Bson has three characteristics: lightweight, ergodicity, high efficiency,



{"Hello": "World"} This is a bson example where "Hello" is the key name, which is typically the CString type, and the byte representation is cstring::= (byte*) "/x00" where * represents 0 or more byte bytes,/ X00 denotes a terminator, followed by "World" as the value, and its type is generally string,double,array,binarydata type.



Second, the use of Bson in the MongoDB



MongoDB uses the Bson structure to store data and network data exchange. The concept of converting this format into a document is Bson because it is schema-free, so the corresponding document in MongoDB has this feature, and one of the documents can also be understood as a record in the relational database. Only the document here is more varied, such as the document can be nested.



One of the important reasons for MongoDB to Bson as its storage structure is its ergodic nature.



Iii. examples of several bson



3.1 Bson of a document says:



The code is as follows:



{



Title: "MongoDB",



Last_editor: "192.168.1.122",



Last_modified:new Data ("27/06/2011"),



Body: "MongoDB Introduction",



categories:["Database", "NoSQL", "Bson"],



Revieved:false



}



This is a simple bson structure in which each element is made up of key/value pairs.



3.2 An example of nesting



The code is as follows:



{



Name: "Lemo",



Age: "12",



address:{



City: "Suzhou",



Country: "The",



code:215000



}



scores:[



{' name ': ' Chinese version ', ' grade:3.0} ',



{' name ': ' Chinese ', ' grade:2.0} '



]



}



This is an example of a relatively complex point, which includes an array of address objects and fractional objects, where nested document objects and document object data are used to represent individual student information, and this nested document structure is more complex to use in relational databases.



4. Bson C + + code Analysis



The MongoDB source tree includes the Bson code base, and you can just include the bson.h header file, four of which are more important:



The code is as follows:



* Mongo::bsonobj, this is the representation of the Bson object.



* Mongo::bsonelement, this is the representation of the elements in the Bson object



* Mongo::bsonobjbuilder, this is the class that constructs the Bson object



* Mongo::bsonobjiterator, which is an iterator used to traverse each element of the Bson object



The following is the creation of a Bson object



The code is as follows:



Bsonobjbuilder b;



B.append ("name", "Lemo"),



B.append ("Age", 23);



Bsonobj p = b.obj ();



Or



The code is as follows:



Bsonobj p = Bsonobjbuilder (). Append ("name", "Lemo"). Append ("Age"). obj ();



Or use a flow method to



The code is as follows:



Bsonobjbuilder b;



b << "name" << "Lemo" << "age" << 23;



Bsonobj p = b.obj ();



Or create an object with macro



The code is as follows:



Bsonobj p = Bson ("name" << "Joe" << "age" << 33);



Here's a look at some of the code for these four classes:



Mongo::bsonobj is mainly used for storing Bson objects, the specific storage format is as follows



The code is as follows:



  {}* eoo



-------------------- ------------- ----------------- ---- ---



TotalSize: A total byte length that contains its own



Bsontype: Object type, there are boolean,string,date and other types, specific reference to bsontypes.h this file



FieldName: This represents the field name



Data: Here is to put the concrete, the data storage way according to different Bsontype



*: Indicates that multiple elements can be composed



Eoo: This is a terminator, which is generally/x00 to represent



In general, the creation of Bsonobj is done through Bsonobjbuilder, and unless you've got the byte stream, it can be generated directly bsonobj



Mongo::bsonelement It is primarily used to store a single element in an object, stored in the following format



The code is as follows:






This object is primarily the address of the specific element in the Bsonobj object, which does not actually store the value of the element.



Mongo::bsonobjbuilder It is primarily used to generate bsonobj, an object that integrates StringBuilder, which is primarily used to store actual byte points for replacing Std::stringstream, And this StringBuilder integrates Bufbuilder, which is a dynamically growing memory buffer, but the maximum capacity cannot exceed 64MB, which means a bsonobj maximum cannot exceed 64MB.



Mongo::bsonobjiterator It is primarily used to traverse each element of the Bsonobj object, providing a number of interfaces similar to the STL iterator, and it provides a foreach macro to provide more convenient operations, such as



The code is as follows:



if (foo) {



Bsonforeach (e, obj)



DoSomething (e);



}


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.