Introduction and use of bson in MongoDB

Source: Internet
Author: User

Introduction and use of bson in MongoDB

This article mainly introduces the introduction and use of bson in MongoDB. This article describes what bson is, how bson is used in MongoDB, and several BSON examples. For more information, see

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.

Ii. 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.

Iii. Several BSON examples

3.1 BSON of a Document indicates:

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 struct. Each element is composed of key/value pairs.

3.2 nested example

The Code is as follows:

{

Name: "lemo ",

Age: "12 ",

Address :{

City: "suzhou ",

Country: "china ",

Code: 215000

}

Scores :[

{& Quot; name & quot;: & quot; english & quot;, & quot; grade: 3.0 & quot },

{"Name": "chinese", "grade: 2.0}

]

}

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:

The Code is as follows:

* Mongo: BSONObj, which indicates the BSON object.

* Mongo: BSONElement. This is the expression of elements in the BSON object.

* Mongo: BSONObjBuilder, which is a class for building BSON objects.

* Mongo: BSONObjIterator, an iterator used to traverse every element of the BSON object

Create 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", 23). obj ();

Or use the stream method.

The Code is as follows:

BSONObjBuilder B;

B <"name" <"lemo" <"age" <23;

BSONObj p = B. obj ();

Or use a macro to create an object.

The Code is as follows:

BSONObj p = BSON ("name" <"Joe" <"age" <33 );

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:

The Code is as follows:

   { } * EOO

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

TotalSize: the total length of a byte, including its own

BSONType: object type. Here there are Boolean, String, Date, and Other types. For details, refer to the bsontypes. h file.

FieldName: field name

Data: specific Data is stored here. The Data storage methods are based on different bsontypes.

*: It can contain multiple elements.

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:

The Code is as follows:

  

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

The Code is as follows:

If (foo ){

BSONForEach (e, obj)

DoSomething (e );

}

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.