MongoDB and DevExtreme News - Data Servers Made Easy

Oliver's Blog
17 August 2017

Several months ago, I announced the library devextreme-query-mongodb, which can be used to query MongoDB with parameters in the format supported by our DevExtreme UI widgets. You can find the original announcement here, and the library has its home on github here.

Recently I have released version 2 of this library. I chose to use major version 2 for the release because of some structural changes I made. Existing projects that were using v1 should work with v2 directly, as soon as you take one extra requirement into account. As the project wiki shows:

In v2, the library is published with babel-compiled files (in the dist) folder, which are used by default. This provides broader compatibility, but it introduces a requirement for babel-polyfill. To satisfy this, you should add a dependency to babel-polyfill to your project (npm install –save babel-polyfill) and initialize the polyfill before you load devextreme-query-mongodb:

const query = require('devextreme-query-mongodb');

The options API

To make it easier to create data services on the basis of devextreme-query-mongodb, I have included an API in v2 that validates query parameters. You can find a page of documentation in the wiki, but the gist of it is that you can retrieve a valid loadOptions object like this:

const getOptions = require('devextreme-query-mongodb/options').getOptions;


// request object in req
const loadOptions = getOptions(req.query);

A sample data server

The page DemoDataServer in the wiki has the full source code for a data server. I decided not to publish the server as a github project, because the source needs a few changes depending on your own scenarios and a github project wouldn’t have worked out of the box (or grown more complicated again).

Here are the basic steps implemented in the sample.

1 — Connect to MongoDB. Of course you need to use your own MongoDB connection URI here.

mongodb.MongoClient.connect(mongoUri, function(err, database) {

2 — Set up an Express web server. Modify the URL part and collection name as necessary.

const app = express();
app.get('/countries', function(req, res) {
  getData(database.collection('countries'), req, res);

3 — Load data, taking advantage of the new options API. Adapt the schema for your collection.

function getQueryOptions(req) {
  return getOptions(req.query, {
    areaKM2: 'int',
    population: 'int'

async function getData(coll, req, res) {
  try {
    const options = getQueryOptions(req);

    const results = await query(
  } catch (err) {
    handleError(res, err, 'Failed to retrieve data');

4 — Run the web server

const httpServer = http.createServer(app);
httpServer.listen(process.env.HTTPPORT || 8080, function() {
  const port = httpServer.address().port;
  console.log('HTTP server running on port', port);

Try it!

Please give the library a spin if you can use it in your projects, and report any issues or questions directly on github!

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.