bsonspec.orgBSON is a binary-encoded serialization of JSON-like documents. BSON is designed to be lightweight, traversable, and efficient. BSON, like JSON, supports the embedding of objects and arrays within other objects and arrays. See bsonspec.org for the spec and more information in general. BSON and MongoDBMongoDB uses BSON as the data storage and network transfer format for "documents". BSON at first seems BLOB-like, but there exists an important difference: the Mongo database understands BSON internals. This means that MongoDB can "reach inside " BSON objects, even nested ones. Among other things, this allows MongoDB to build indexes and match objects against query expressions on both top-level and nested BSON keys. See also: the BSON blog post. Language-Specific ExamplesWe often map from a language's "dictionary" type – which may be its native objects – to BSON. The mapping is particularly natural in dynamically typed languages: JavaScript: {"foo" : "bar"}
Perl: {"foo" => "bar"}
PHP: array("foo" => "bar")
Python: {"foo" : "bar"}
Ruby: {"foo" => "bar"}
Java: DBObject obj = new BasicDBObject("foo", "bar");
CSee http://github.com/mongodb/mongo-c-driver/blob/master/src/bson.h C++BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); cout << p.toString() << endl; cout << p["age"].number() << endl; See the BSON section of the C++ Tutorial for more information. JavaBasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info); coll.insert(doc); PHPThe PHP driver includes bson_encode and bson_decode functions. bson_encode takes any PHP type and serializes it, returning a string of bytes: $bson = bson_encode(null); $bson = bson_encode(true); $bson = bson_encode(4); $bson = bson_encode("hello, world"); $bson = bson_encode(array("foo" => "bar")); $bson = bson_encode(new MongoDate()); Mongo-specific objects (MongoId, MongoDate, MongoRegex, MongoCode) will be encoded in their respective BSON formats. For other objects, it will create a BSON representation with the key/value pairs you would get by running for ($object as $key => $value). bson_decode takes a string representing a BSON object and parses it into an associative array. Python>>> from pymongo.bson import BSON >>> bson_string = BSON.from_dict({"hello": "world"}) >>> bson_string '\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00' >>> bson_string.to_dict() {u'hello': u'world'} PyMongo also supports "ordered dictionaries" through the pymongo.son module. The BSON class can handle SON instances using the same methods you would use for regular dictionaries. Rubyirb(main):013:0> require 'rubygems'
irb(main):014:0> require 'mongo'
irb(main):015:0> document = {:title => 'Intro', :date => Time.now, :tags => ['MongoDB', 'databases', 'nosql']}
=> {:tags=>["MongoDB", "databases", "nosql"], :date=>Sat Dec 19 10:56:28 -0800 2009, :title=>"Intro"}
irb(main):016:0> sdoc = BSON.serialize(document)
=> #<ByteBuffer:0x101372880 @double_pack_order="E", @cursor=92, @int_pack_order="V", @buf="\\\000\000\000\004tags\0002\000\000\000\0020\000\b\000\000\000MongoDB\000\0021"
"\000\n\000\000\000databases\000\0022\000\006\000\000\000nosql\000\000\tdate\000"
"XEL\250%\001\000\000\002title\000\006\000\000\000Intro\000\000", @order=:little_endian>
irb(main):017:0> BSON.deserialize(sdoc)
=> {"tags"=>["MongoDB", "databases", "nosql"], "date"=>Sat Dec 19 18:56:28 UTC 2009, "title"=>"Intro"}
irb(main):018:0>
The BSON class also supports ordered hashes. Simply construct your documents using the OrderedHash class, also found in the MongoDB Ruby Driver. Examples with all of the supported BSON types can be found in the BSON unit tests. MongoDB Document TypesMongoDB uses BSON documents for three things:
|

IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.
These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group. blog comments powered by Disqus