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 Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
Alfred Rakgole 2
Alfred Rakgole 2

Awesome,

Can one use the report in web forms, MVC and Core?

I wish the wizard functionality can also implemented on
web report designer at some stage.



21 June 2021
Jacek Kosiński
Jacek Kosiński
XAF and Mongo !!!!
21 June 2021
Dmitry Tok (DevExpress Support)
Dmitry Tok (DevExpress Support)

@Alfred, yes, you can create a class library with multiple reports once, and reuse it across multiple apps thanks to the cross-platform nature of DevExpress Reports.

As for the data source wizard: do you expect that your users will create queries themselves? I'd love to hear more about your requirements. Please take a moment or two to describe them in a support center ticket. For now, register predefined data sources in Web Report Designer are as I advised earlier in this ticket: How to bind a report to MongoDB.

@Jacek: Thanks for your feedback. Can you please elaborate and define your usage scenario? Which modules require MongoDB support from your point of view? Can you describe why you want to use a NoSQL database in an XAF app instead of a relational database? Please reply here or create a new support center ticket so we can better understand your requirements. Oh, and do check out the following YouTube video at your convenience: Using MongoDB to persist XAF business objects.

24 June 2021
JULIO  DE TRINIDAD
JULIO DE TRINIDAD
XAF and Mongo !!!! +1 (Do you have it in mind for future near release?)
24 June 2021

Please login or register to post comments.