Accessing data in MongoDB
MongoDB is an open source NoSQL database. In a MongoDB instance, an element of data is called a document, and documents are stored in collections. The document structure follows the JavaScript Object Notation (JSON) format. JSON is derived from the JavaScript scripting language for representing simple data structures and associative arrays called objects. A document is an associative array (also called a map, dictionary, hash table or hash).
The structure of a MongoDB instance is quite different from that of a relational database. In a relational database, data is organized in tables. Each table consists of records (rows), and each record consists of fields. Within each table, every record has the same fields in the same order. All the instances of a field taken together are called a column. In a MongoDB instance, collections are like tables, and documents are like records. The difference is that there are no fixed schemas, so any document in a collection can have different fields from the other documents. For example, Figure 6‑25 shows a table in a relational database with columns Last Name, First Name, and Date of Birth.
Figure 6‑25 Table in a relational database
The following code shows a MongoDB collection with similar data. The collection consists of a series of key-value pairs. The key is the name of the field and the value is the field’s content. The key and value are separated by a colon (:). Notice that the second document contains Address and City fields as well as Last Name, First Name, and Date of Birth.
{
"_id": ObjectId("4efa8d2b7d284dad101e4bc9"),
"Last Name": "DUMONT",
"First Name": "Jean",
"Date of Birth": "01-22-1963"
},
{
"_id": ObjectId("4efa8d2b7d284dad101e4bc7"),
"Last Name": "PELLERIN",
"First Name": "Franck",
"Date of Birth": "09-19-1983",
"Address": "1 chemin des Loges",
"City": "VERSAILLES"
}
A value can be a number; a string; true or false; binary data such as an image; an array of values (each of which can be of a different type); or a subordinate document. In the following code, the Address field contains a subordinate document with two fields, Street and City.
{
"_id": ObjectId("4efa8d2b7d284dad101e4bc7"),
"Last Name": "PELLERIN",
"First Name": "Franck",
"Date of Birth": "09-19-1983",
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567",
"verified": false
}
],
"Address": {
"Street": "1 chemin des Loges",
"City": "VERSAILLES"
},
"Months at Present Address": 37
}
MongoDB features full index support, including secondary and compound indexes. Indexes are specified per collection. There is a document-based query language that leverages these indexes. MongoDB also provides atomic update modifiers to keep code contention-free.
Clustered setups are supported, including easy replication for high availability, as well as auto-sharding for write-scaling and large data sets.
BIRT supports access to data in MongoDB. As with other types of data sources, for a report to use data from MongoDB, you must create the following BIRT objects:
*A data source that contains the information to connect to a MongoDB database.
*A data set that specifies the data to retrieve.