Mongo Extended JSON

Mongo's REST interface supports storage and retrieval of JSON documents. Special representations are used for BSON types that do not have obvious JSON mappings, and multiple representations are allowed for some such types. The REST interface supports three different modes for document output { Strict, JS, TenGen }, which serve to control the representations used. Mongo can of course understand all of these representations in REST input.

  • Strict mode produces output conforming to the JSON spec http://www.json.org.
  • JS mode uses some Javascript types to represent certain BSON types.
  • TenGen mode uses some Javascript types and some 10gen specific types to represent certain BSON types.

The following BSON types are represented using special conventions:

Type Strict JS TenGen Explanation
data_binary
{ "$binary" : "<bindata>", "$type" : "<t>" }
{ "$binary" : "<bindata>", "$type" : "<t>" }
{ "$binary" : "<bindata>", "$type" : "<t>" }
<bindata> is the base64 representation of a binary string. <t> is the hexadecimal representation of a single byte indicating the data type.
data_date
{ "$date" : <date> }
Date( <date> )
Date( <date> )
<date> is the JSON representation of a 64 bit signed integer for milliseconds since epoch (unsigned before version 1.9.1).
data_timestamp
{ "$timestamp" : {"t": <t>, "i": <i>} }
Timestamp( <t>, <i> )
Timestamp( <t>, <i> )
<t> is the JSON representation of a 32 bit unsigned integer for milliseconds since epoch. <i> is a 32 bit unsigned integer for the increment.
data_regex
{ "$regex" : "<sRegex>", "$options" : "<sOptions>" }
/<jRegex>/<jOptions>
/<jRegex>/<jOptions>
<sRegex> is a string of valid JSON characters. <jRegex> is a string that may contain valid JSON characters and unescaped '"' characters, but may not contain unescaped '/' characters. <sOptions> is a string containing letters of the alphabet. <jOptions> is a string that may contain only the characters 'g', 'i', 'm' and 's' (added in v1.9). Because the JS and TenGen representations support a limited range of options, any nonconforming options will be dropped when converting to this representation.
data_oid
{ "$oid" : "<id>" }
{ "$oid" : "<id>" }
ObjectId( "<id>" )
<id> is a 24 character hexadecimal string. Note that these representations require a data_oid value to have an associated field name "_id".
data_ref
{ "$ref" : "<name>", "$id" : "<id>" }
{ "$ref" : "<name>", "$id" : "<id>" }
Dbref( "<name>", "<id>" )
<name> is a string of valid JSON characters. <id> is a 24 character hexadecimal string.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus