Tuesday, January 12, 2016

Codes for Data Type as per BSON Specifications

As we know MongoDb is document oriented Database so what actually means is Mongodb fundamentally record type is kind of nested dictionary of key value associations. We Map the documents that come out of Mongo Db to Objects in the Programming language that can represent these type of key value associations.

In Javascript, the type of Object that represents these kinds of key value associations is called a Javascript Object. MongoDB uses a binary representation for the data inside the document.
The specification for Binary representation can be find out at  http://bsonspec.org/

The Binary format BSON stands for Binary JSON and it is a serialized format that designed to represent a super set of what can be described in JSON syntax

Now for example you have a collection as people which has a column as name and the user has stored some integer values also in that name field. you want to query the people document to find the name but don't want to display the result having name field with value as integer

db.people.find({name:{$type:2}})
This will fetch you the name from people collection  which are of String type. here the value 2 is mapped for String datatype.

Similar for other datatype has been mapped to some integer value. Here is an list of all Datatype which are mapped to a particular Integer type as per BSON specifications