Specifying the data to retrieve from MongoDB
Once the report connects to a MongoDB database, you create a data set and select the collection from which to retrieve data. A data set can retrieve data from one collection only.
After selecting a collection, you select the fields from which to retrieve data. BIRT maps each selected field to a data set column. Because MongoDB is a schema-less database in which each document can contain a different set of fields, you have the option of specifying the number of documents to scan to compile the list of fields. Scanning documents can be resource intensive. If all the documents contain the same fields, specify one (the default) as the number of documents to scan.
How to specify what data to retrieve from a MongoDB database
1 In Data Explorer, right-click Data Sets, then choose New Data Set.
2 In New Data Set, specify the following information:
1 In Data Source Selection, select the MongoDB data source to use. Data Set Type displays MongoDB Data Set.
2 In Data Set Name, type a name for the data set.
3 Choose Next.
3 In New MongoDB Data Set, in Query, do the following:
1 In MongoDB Collection, select the collection from which to retrieve data. Not required if you enter a Run Database Command expression.
2 In Command Operation, in Command type, optionally select a command type. Choose Expression and type an expression in Command Expression Builder. The default command type is FindQuery.
3 In Number of Documents to search for fields, type the number of documents for which to search for fields, then choose Find Fields. The fewer the number of documents to scan, the faster the response.
Available Fields displays the fields defined in the scanned documents. The letter D preceding a field name indicates that the field is a subordinate document. Square brackets ([]) following a field name indicate that the field is an array. If you do not see the fields you expect and want, increase the number of documents to scan, then choose Find Fields.
4 In Available Fields, select the field or fields whose data to retrieve. To move a single field from Available Fields to Selected Fields, select the field and choose >. You can select a field at any level, for example a subordinate document field, not just a leaf field (lowest level). To move all fields in a node from Available Fields to Selected Fields, select the node and choose >>.
Figure 6‑27 shows an example of a MongoDB query.
Figure 2-25 Example of a MongoDB queryFigure 2-25 Example of a MongoDB query
Figure 6‑27 Example of a MongoDB query
4 Choose Finish to save the data set. Edit Data Set displays the output columns, and provides options for editing the data set.
5 Choose Preview Results to view the data rows returned by the data set.
Mapping selected fields
Available Fields, shown in Figure 6‑2, displays fields and subordinate documents found by searching documents in a MongoDB collection. A subordinate document can, in turn, contain fields and other subordinate documents. For example, in Figure 6‑2, Available Fields shows the fields _id and name and the subordinate document nestedDocs[]. The subordinate document nestedDocs[] contains the field text and the subordinate document follower. In Selected Fields, fields in subordinate documents are identified using dot notation, for example nestedDocs[].text and nestedDocs[].follower.followed. When a subordinate document and its fields are both selected, only the selected fields are included in the subordinate document.
A field can be of type Array, and can contain multiple scalar values or subordinate documents. By default, scalar values in an array field are concatenated into a single String value in JSON expression format in a result set column, for example [1.0, 2.0, 3.0]. The field values in each subordinate document in an array field are also, by default, concatenated into a single String value in a result set column, for example
[{"followed":true,"ratings":[1.0,2.0,3.0]},{"followed":true,
"ratings":[1.0,2.0,3.0]}]
You can specify one or more array projection operators, such as $slice and $elemMatch, to project elements from an array in the result set. For example, you can use $slice to project elements from the zipcodes collection:
{ city: 1, loc: {$slice : 1}, state : true, _id : false }
Specify array projection operators in Selected Fields on the Property Binding page in the Data Set Editor. Make sure that the field names projected in the result set match the output column names in the BIRT data set. Alternatively, you can flatten subordinate documents by setting the Flatten nested collections runtime connection property to true.
Table 6‑1 shows the native data type to ODA data type mapping for MongoDB.
Table 6‑1 Native data type to ODA data type mapping for MongoDB
Native data type
Native data type value
ODA data type
Number
1
Double
String
2
String
Object
3
JavaObject
Array
4
JavaObject
Binary
5
Blob
Object id
7
String
Boolean
8
Boolean
Date
9
Date
Null
10
String
Regular expression
11
String
JavaScript
13
String
Symbol
14
String
JavaScript with scope
15
String
32-bit integer
16
Integer
Timestamp
17
Timestamp
64-bit integer
18
String
Min key
255
String
Max key
127
String
Writing expressions
When you create a MongoDB data set query, you can create Mongo JSON expressions for the following:
*Run Database commands
*Aggregate commands
*MapReduce commands
*Query expressions
*Sort expressions
You can type the expression or import it from a text file. You can then validate the syntax of the expression. You can override an expression at runtime by setting the appropriate property or properties on the Property Binding page in the Data Set Editor. Embedded input parameter markers are not supported.
Writing expressions for Run Database commands
Run Database Command executes a MongoDB command-driven query. The command result document is exposed as available fields that can be selected and bound to a data set’s columns. These commands are run on the connected database; any specified MongoDB collection is ignored.
Run Database Command supports read-only operation. The following MongoDB database commands are supported.
*buildInfo
*collStats
*connPoolStats
*count
*cursorInfo
*dataSize
*dbStats
*distinct
*eval with nolock: true *
*geoNear
*geoSearch
*getLastError
*getLog
*getPrevError
*group
*isMaster
*isdbgrid
*listCommands
*listDatabases
*listShards
*ping
*printShardingStatus
*replSetGetStatus
*serverStatus
* eval with nolock: true is used to evaluate a function written in JavaScript at the database server. eval’s default behavior of taking a global write lock is not allowed.
For more information about MongoDB database commands, see
http://docs.mongodb.org/manual/reference/command/
Writing expressions for Aggregate commands
When writing expressions for Aggregate commands, including array markers at the beginning and end of a pipeline expression is optional. A sort value must be 1 or -1.
For more information about Aggregate commands, see
http://docs.mongodb.org/manual/reference/aggregation/
For Aggregation Framework examples, see
http://docs.mongodb.org/manual/tutorial/aggregation-examples/
Writing query expressions
A query expression is optional, and is only applicable to a data set that has either a FindQuery or MapReduce (with output to a collection) command operation. You must specify a MongoDB JSON expression to describe a custom $query operation to filter on the specified collection, or an output collection returned by the MapReduce command. A query expression is ignored when a MapReduce command outputs to inline using the syntax out: { inline: 1 }. In this case, you must specify the query parameters directly in the MapReduce command expression.
For more information about query expressions and examples, see
http://docs.mongodb.org/manual/reference/operator/query/
Writing sort expressions
A sort expression is optional, and is only applicable to a data set that has either a FindQuery or MapReduce (with output to a collection) command operation. You must specify a MongoDB JSON expression to describe a custom sort operation on the result cursor. A sort value can be a positive value (>= 0 or true/false) for ascending order, or a negative value for descending order. Take note of the following:
*If Flatten nested collections is enabled, a sort expression is applied to the result set before rows are flattened.
*A sort expression is ignored when a MapReduce command outputs to inline using the syntax out: { inline: 1 }. In this case, you must specify the sort parameters directly in the MapReduce command expression.
*Make sure that an index exists on the sort attribute, or set a runtime limit on maxRows.
For more information about sort expressions and examples, see
http://docs.mongodb.org/manual/reference/method/cursor.sort
/#cursor.sort