Introduction to MongoDB bson

Source: Internet
Author: User

 

Introduction to MongoDB bson

 

1. What is bson?

Bson is a type of JSON binary storage format, binary JSON for short. It is the same as JSON and supports embedded document objects and array objects, however, bson has some data types not available in JSON, such as date and bindata.

Bson can be used as a storage mode for network data exchange. This is a bit similar to Google's protocol buffer, but bson is a schema-less storage mode, which has the advantage of high flexibility, however, its disadvantage is that the space utilization is not ideal,

Bson has three features: lightweight, reproducible, and efficient.

{"Hello": "world"} This is a bson example. "Hello" is the key name, which is generally of the cstring type, and the byte value is cstring ::= (byte *) "/x00", where "*" indicates zero or multiple bytes, and "/x00" indicates the Terminator. "world" is the value. Its type is generally string, double, array, binarydata, and other types.

 

2. Use of bson in MongoDB

MongoDB uses the bson structure to store data and exchange network data. Convert this format into a document. Because bson is Schema-free, the document corresponding to MongoDB also has this feature, A document here can also be understood as a record in a relational database, but the document changes here are richer, such as the document can be nested.

One of the important reasons why MongoDB uses bson as its storage structure is its durability.

 

3. Several bson examples 3.1 bson representation of a document:

{<Br/> title: "MongoDB", <br/> last_editor: "192.168.1.122", <br/> last_modified: New Data ("27/06/2011 "), <br/> body: "MongoDB Introduction", <br/> categories: ["Database", "nosql", "bson"], <br/> revieved: false <br/>} 

This is a simple bson struct. Each element is composed of key/value pairs.

3.2 nested example

{<Br/> name: "Lemo", <br/> Age: "12", <br/> address: {<br/> City: "Suzhou ", <br/> country: "China", <br/> code: 215000 <br/>}< br/> scores: [<br/> {"name ": "English", "grade: 3.0 },< br/> {" name ":" Chinese "," grade: 2.0 }< br/>] <br/>} 

This is an example of a relatively complex point, including an array of address objects and score objects. nested document objects and document object data are used to represent the information of a single student, this nested document structure is complicated to use relational databases.

 

4. bson C ++ code analysis

The MongoDB source code tree contains the bson code library. You only need to include the bson. h header file. Four of them are important:

* Mongo: bsonobj, which is the representation of the bson object <br/> * Mongo: bsonelement, which is the representation of elements in the bson object <br/> * Mongo :: bsonobjbuilder, which is a class for building bson objects <br/> * Mongo: bsonobjiterator, which is an iterator used to traverse every element of a bson object 

 

Create a bson object.

Bsonobjbuilder B; <br/> B. append ("name", "Lemo"), <br/> B. append ("Age", 23); <br/> bsonobj P = B. OBJ (); 

 

Or

Bsonobj P = bsonobjbuilder (). append ("name", "Lemo"). append ("Age", 23). OBJ (); <br/> 

Or use the stream method.

Bsonobjbuilder B; <br/> B <"name" <"Lemo" <"Age" <23; <br/> bsonobj P = B. OBJ (); 

 

Or use a macro to create an object.

Bsonobj P = bson ("name" <"Joe" <"Age" <33); <br/> 

Here we will analyze the code of these four classes:

Mongo: bsonobj is mainly used to store bson objects. The specific storage format is as follows:

<Unsigned totalsize >{< byte bsontype> <cstring fieldname> <data >}* EOO <br/> --------------------------- --------------- ---- --- <br/> totalsize: a total length of bytes, contains itself <br/> bsontype: object type. Here there are Boolean, String, date, and Other types. For details, refer to bsontypes. h. File <br/> fieldname: field name <br/> data: the specific data is stored here. The data is stored in different bsontypes. <br/> *: it can contain multiple elements. <br/> EOO: This is an Terminator, usually expressed as/x00. 

Generally, bsonobj is created through bsonobjbuilder. Unless you have obtained its byte stream, you can directly generate bsonobj.

 

Mongo: bsonelement is mainly used to store a single element in an object. The storage format is as follows:

<Type> <fieldname> <value> 

This object mainly points to the address of a specific element in the bsonobj object, which does not actually store the value of the element.

Mongo: bsonobjbuilder it is mainly used to generate bsonobj. This object integrates stringbuilder, which is mainly used to store actual byte points and is used to replace STD: stringstream. This stringbuilder integrates bufbuilder, this is a buffer that can dynamically increase memory, but the maximum capacity cannot exceed 64 MB, that is, a bsonobj cannot exceed 64 MB.

 

Mongo: bsonobjiterator it is mainly used to traverse every element in the bsonobj object and provides interfaces similar to STL iterator. It also provides a foreach macro to provide more convenient operations, such

If (FOO) {<br/> bsonforeach (E, OBJ) <br/> dosomething (E); <br/>} 

 

 

5. Reference

* Http://www.mongodb.org/pages/viewpage.action? Pageid = 16646453

* Http://bsonspec.org/

* Http://www.mongodb.org/display/DOCS/BSON

 

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.