Reporting - New MongoDB Data Source (v21.1)

Reporting Team Blog
21 June 2021

As you may already know, you can now bind DevExpress Reports (v21.1) to document collections within a MongoDB instance. In this post, we’ll summarize how you can do it.

MongoDB Data in Reports

MongoDB is a NoSQL, document-based, distributed database that stores data within JSON-like documents (BSON format). The new MongoDbDataSource component deserializes objects from this BSON format to native .NET types. Like other built-in data sources, it also persists information about these types within a report layout. We extended our data source wizard so you can quickly set up a MongoDbDataSource and add it to your reports.

Data Source Wizard - MongoDB Data Source

Data Source Wizard Capabilities

The first wizard page allows you to specify parameters required to connect to a MongoDB instance. Two options are available: you can either paste a connection string or specify connection fields - such as hostname and port.

Data Source Wizard - MongoDB Data Source - Connection Parameters

You can also specify authentication credentials when necessary. The first version of this data source supports Password and SCRAM-SHA-256 authentication types. If you require use of the other authentication types, feel free to comment below and tell us what you currently use.

Note that our MongoDB data source uses MongoDB.Driver to obtain MongoDB data at runtime. If the driver is not installed, the Visual Studio Data Source Wizard prompts you to install it:

Data Source Wizard - MongoDB Data Source - Install NuGet Package

Once you’ve set up connection parameters, you need to configure queries for the instance’s database collections:

Data Source Wizard - MongoDB Data Source - Specify Queries

At present, the Configure queries wizard page allows you to select the name of a MongoDB database and its collection. You can also specify an alias for a collection and a filter condition for the collection’s items. Please let us know in the comment section below or via the DevExpress Support Center if you require additional capabilities within our data source wizard.

Runtime MongoDB Data Access

Of course, you can set up a MongoDBDataSource object at runtime. The steps are the same as those outlined in the Data Source Wizard section above: you need to specify connection parameters and configure queries.

An instance of the MongoDBConnectionParameters class stores MongoDB connection parameters:

var connectionParameters = new MongoDBConnectionParameters(
    hostName: "localhost",
    isSRV: false,
    port: 27017
);

You can also create a MongoDBCustomConnectionParameters object if you wish to use a MongoDB connection string:

var connectionString = new MongoDBCustomConnectionParameters() {
    ConnectionString = "mongodb://localhost:27017"
};

To configure queries, create and set up a MongoDBQuery class instance:

var queryProducts = new MongoDBQuery() {
    DatabaseName = "Northwind",
    CollectionName = "Products",
    FilterString = "[UnitPrice] <= 40"
};

The final step is to create a MongoDBDataSource object and assign the parameters and queries to corresponding data source properties:

var mongoDBDataSource = new MongoDBDataSource() {
    ConnectionParameters = connectionParameters,
    Queries = { queryProducts }
};

You can then use the MongoDB data source in a report and reference data fields:

var report = new XtraReport() {
    DataSource = mongoDBDataSource,
    DataMember = "Products",
    // ...
};

WinForms GridControl Support

Our MongoDB data source is not limited to DevExpress Reports. It is also available for use by our WinForms Data Grid control. You can bind this control to a MongoDB instance in code:

mongoDBDataSource.Fill();
dataGrid.DataSource = mongoDBDataSource;

Note that we call the Fill method explicitly to load data from a MongoDB instance to the data source. We don’t call this method when binding a report to the data source since report-based applications call this method implicitly.

You can also use the Data Source Wizard to bind the DevExpress WinForms Grid control to MongoDB:

WinForms Data Source Wizard - MongoDB Data Source

Refer to the following help topic for more information: WinForms Controls: Bind to MongoDB Data.

Your Feedback Counts

What do you think about our new MongoDB data source? Does it address your needs? Feel free to share your feedback in the comments section below.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.