Expose MongoDB data as OData feed using MongOData provider

With growing popularity of MongoDB grows the variety of tools and technologies that expose data stored in MongoDB databases. Since I am using both MongoDB and OData in my projects, I decided to cross them and enable exposure MongoDB collections as OData feeds. So I started a project MongOData hosted at GitHub. Although I was using MongOData internally for some time for data retrieval, I was reluctant to announce it until I implement support for update. Update is now supported (version 0.4.0), so I feel it’s time to write a blog post presenting how MongOData works and how it was built.

MongOData highlights

  • MongOData is a MongoDB OData provider. It exposes each MongoDB collection as an OData resource set. Current release assumes that all data inside the collection have the same structure, so it builds metadata information based on the first row in each collection. I plan to support different metadata build strategies in future releases.
  • MongOData does not use OData open types, although open types seem to be a good fit for schemaless databases. Most of OData client tools don’t have support for open types and rely on types exposed via metadata, so I took an approach that is compatible with traditional OData consumers.
  • There are two ways to install MongOData: using MSI installer and using NuGet package. MSI installer is an easiest option if you only need to publish OData feed for one or all MongoDB databases. NuGet MongOData package should be used to add MongoDB WCF Data service to an existing Web project.
  • MongOData uses modified code from OData Provider Toolkit that helps building WCF Data Services custom providers. OData Provider Toolkit uses LINQ to Objects to execute queries, so it only supports in-memory queries and therefore requires all data to be read in memory upfront. Such implementation is of course is not suitable for large databases, so I revised the original code to support two strategies: in-memory and queryable providers. MongOData uses queryable provider and LINQ provider from the official MongoDB C# driver.

Installing MongOData using MSI installer

This is the easiest option to start using MongOData with MongoDB. Simply download the installer and run it on a machine with MongoDB and IIS installed. During the installation you can configure Web server virtual directory and MongoDB connection string:


Note that default connection string contains the wild card ‘*’. This syntax is not supported by MongoDB, it indicates that MongOData should expose all MongoDB databases available on the server. The specific database can then be selected by specifying its name in an URL, e.g.:



In the examples above we read data from “Northwind” and “zips” database.

In case you don’t want to open OData access to all configured MongoDB databases, you replace the wild card in the installation screen with the name of the specific database, e.g.: mongodb://localhost/Northwind?strict=true. Then only this specific database will be exposed as OData feed.

Installing MongOData using NuGet package

If you want to embed MongoDB OData feed into your Web service or application, you can use MongOData NuGet package. NuGet package adds custom WCF Data Service to your Web component, so you only need to specify the MongoDB connection string (MongOData NuGet package does not support wild cards, you will have to refer to a specific database). Here is what you need to do.

  1. Create a Web project using one of Visual Studio templates, for example, “ASP.NET MVC3 Web Application” (almost any generic ASP.NET Web application template will do).
  2. Open Package Manager Console window and write the following command:
    Install-Package MongOData

    NuGet package manager will install the latest MongOData component along with its dependency mongocsharpdriver, so you will see the package installation report similar to this:

    Attempting to resolve dependency 'mongocsharpdriver (≥ 1.4.1)'.
    Successfully installed 'mongocsharpdriver 1.4.2'.
    Successfully installed 'MongOData 0.4.0'.
    Successfully added 'mongocsharpdriver 1.4.2' to MongODataTest.
    Successfully added 'MongOData 0.4.0' to MongODataTest.

    If you check the project content, you will see that there are references to MongoDB driver and MongOData assembly files, and two new source files MongoDataService.cs and MongoDataService.svc. Here is the content of the newly added C# source file:

    public class MongoDataService : MongoQueryableDataService
        public MongoDataService()
            : base(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString)
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            config.DataServiceBehavior.AcceptCountRequests = true;
            config.DataServiceBehavior.AcceptProjectionRequests = true;
            config.UseVerboseErrors = true;

    Note that MongoDataService refers to a connection string named as “MongoDB”. If you are comfortable with this, you don’t need to do any modification to the source files, just update the connection string in web.config file so it points to the actual MongoDB database that you are going to expose as OData feed.

  3. Build the project and go the MongoDataService.svc page, you will see something like this:


You’re done! Now you can query your MongoDB OData feed using standard OData URI convention.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s