MongoDBDataSource Class
Contains properties and methods that allow you to bind an application or component to a MongoDB instance.
Namespace: DevExpress.DataAccess.MongoDB
Assembly: DevExpress.DataAccess.v24.2.dll
Declaration
[ToolboxBitmap(typeof(MongoDBDataSource), "MongoDBDataSource.bmp")]
public class MongoDBDataSource :
MongoDBDataSourceBase,
IListSource,
IListAdapter2,
IListAdapter,
ISupportFillAsync,
IListAdapterAsync2,
IListAdapterAsync,
IDynamicLookupSettingsDataProvider
Remarks
Note
The MongoDB.Driver package should be installed in your project to supply MongoDB data at runtime.
You should create and set up a MongoDBDataSource object to bind an application or component to a MongoDB instance. The sections below explain in detail how you can do it.
Specify Connection Parameters
You can use one of the ways below to specify connection parameters to a MongoDB instance.
Use a Connection String
Create a MongoDBCustomConnectionParameters object and assign a connection string to the object’s ConnectionString property.
Configure Connection Fields Individually
- Create a MongoDBConnectionParameters object, assign the hostname and port of the MongoDB instance to the object’s Hostname and Port properties.
- (Optional) Enable the object’s IsSRVRecord property if the hostname is an SRV record.
- (Optional) Use the object’s AuthenticationInfo property to specify authentication credentials.
Load a Connection String From a Project’s Configuration File
Refer to the ConnectionName property description for details.
Implement a Custom Connection Service
Refer to the LoadConnection(String) method description for more information.
Create Data Queries
An instance of the MongoDBQuery class is a query to a database collection of the MongoDB instance. You can create multiple queries to database collections. To create one query, initialize a MongoDBQuery object and assign the name of a database and the name of a database collection to the query’s DatabaseName and CollectionName properties. To filter items of the database collection, assign a filter condition to the query’s FilterString property.
The names of all queries to database collections should be unique. A string stored in a query’s CollectionName property is the default name for this query. To create several queries to the same database collection, use the Alias property to specify different names for these queries.
Create a MongoDB Data Source
Create a MongoDBDataSource
object and assign the created connection parameters to the object’s ConnectionParameters property. Add the data queries to the object’s Queries collection. If you load a connection string from a project’s configuration file or implement a custom connection service, assign the name of the connection string to the object’s ConnectionName property.
Load Data From the MongoDB Instance to the MongoDB Data Source
Call the Fill() method for the MongoDB data source
object to execute the queries. You can also call the FillAsync() method to execute queries asynchronously. These queries load data from the MongoDB instance to the data source. You can execute individual queries and specify parameters that you want to reference in the queries’ FilterString property. Refer to the descriptions of the method’s overloads for more details:
Use the MongoDB Data Source in Your Application or Component
Reporting
Assign the created MongoDBDataSource
object to a report’s DataSource property to use data loaded from the MongoDB instance in this report. Refer to the following topics for more details on how to use the MongoDB data source in reporting applications:
Dashboard
Refer to the topic: Bind a Dashboard to MongoDB.
WinForms
Refer to this article: WinForms Controls: Bind to MongoDB Data.
Example
The example below demonstrates how to use the MongoDBDataSource
class to bind an application/component to a MongoDB instance. The example uses the MongoDBCustomConnectionParameters class to specify a connection string to the MongoDB instance and the MongoDBQuery class to specify data queries to the Categories and Products collections of the Northwind database.
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
// ...
// Create a MongoDBCustomConnectionParameters object and assign
// a connection string to a MongoDB instance to the object's
// ConnectionString property.
var connectionString = new MongoDBCustomConnectionParameters() {
ConnectionString = "mongodb://localhost:27017"
};
// Specify queries to database collections.
var queryCategories = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Categories",
};
var queryProducts = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Products",
};
// Create a MongoDBDataSource object. Assign the created connection
// string to the object's ConnectionParameters property. Add the
// queries to the object's Queries collection.
var mongoDBDataSource = new MongoDBDataSource() {
ConnectionParameters = connectionString,
Queries = { queryCategories, queryProducts }
};
// Call the Fill method of the MongoDBDataSource object to execute the
// queries and load data from the MongoDB instance.
mongoDBDataSource.Fill();
// Use the created object as a data source in your application or component.
//...
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.MongoDB
' ...
' Create a MongoDBCustomConnectionParameters object and assign
' a connection string to a MongoDB instance to the object's
' ConnectionString property.
Dim connectionString = New MongoDBCustomConnectionParameters() With {
.ConnectionString = "mongodb://localhost:27017"
}
' Specify queries to database collections.
Dim queryCategories = New MongoDBQuery() With {
.DatabaseName = "Northwind",
.CollectionName = "Categories"
}
Dim queryProducts = New MongoDBQuery() With {
.DatabaseName = "Northwind",
.CollectionName = "Products"
}
' Create a MongoDBDataSource object. Assign the created connection
' string to the object's ConnectionParameters property. Add the
' queries to the object's Queries collection.
Dim mongoDBDataSource = New MongoDBDataSource() With {
.ConnectionParameters = connectionString
}
mongoDBDataSource.Queries.Add(queryCategories)
mongoDBDataSource.Queries.Add(queryProducts)
' Call the Fill method of the MongoDBDataSource object to execute the
' queries and load data from the MongoDB instance.
mongoDBDataSource.Fill()
' Use the created object as a data source in your application or component.
'...