Altova XMLSpy 2024 Enterprise Edition

BSON (Binary JSON) for MongoDB

Home Prev Top Next

The MongoDB application data platform stores data as JSON structures, but in a binary representation of the data. This representation is known as "Binary JSON" or BSON. The main benefits of using BSON for MongoDB are:

 

The binary format of BSON is faster to parse than the text of a JSON document.

Since JSON has limited datatyping, BSON has been provided with more datatypes (in particular, more numeric datatypes).

 

For more information about MongoDB and BSON, see this page at the MongoDB website.

 

Edit JSON schemas for BSON data

MongoDB provides the ability, during the addition of new DB data and the modification of DB data, for the DB data to be validated against a JSON schema document. However, because of the additional BSON datatypes, which are not part of the official JSON schema specifications but supplement them, JSON schemas for BSON are edited in XMLSpy via an editing layer for BSON that is overlaid on the JSON schema editor. This editing layer enables you to add and modify BSON-specific schema features—in addition to the standard JSON features.

 

Consequently, you can edit a JSON schema document of any version with or without the BSON editing layer. When the document is edited without the BSON editing layer, it is edited as a straightforward JSON schema document. With BSON support, the JSON schema document can additionally define BSON-specific features. To switch on BSON editing features in JSON Schema View for the active document, select the BSON Support check box at the top right of the main window (circled in green in the screenshot below). Note that BSON support (i) can be switched on regardless of the JSON schema version that has been selected, and (ii) applies to the current document only; it can be switched on/off for each document separately.

JSONBSONSupport

 

BSON types

After the BSON editing layer has been switched on, BSON datatypes become available for JSON objects, properties, and array items. Specifying that a component is of a BSON type consists of two steps:

 

1.Specify that the component is a BSON datatype (and not a JSON datatype) by selecting BSON as the base JSON type. Do this either in the component's datatype-selector combo box (by double-clicking the type value; screenshot below left) or the component's Details entry helper (screenshot below right).

Click to expand/collapse     JSONSVBSONTypeinDetailsEH

2.After the component's base type has been selected to be BSON, the BSON types become available for selection. Select the BSON type either in the component's datatype selection (left arrow in screenshot below) or in the Details entry helper (tight arrow in screenshot below).

Click to expand/collapse

 

Disabling BSON support

If you assign a BSON type to a JSON schema component (as described above) and then uncheck the BSON Support option for the document, a message box will appear. It informs you that there are BSON types in the document and asks whether you want to remove/convert the BSON types or keep them. If you choose to remove/convert, then those BSON types that can be converted to JSON types will be converted while the others will be removed. If you choose to keep the BSON types, then they will be retained—but colored orange in Schema View because BSON type support has been removed (see screenshot below).

Click to expand/collapse

 

© 2017-2023 Altova GmbH