So I am looking to do 3 simple activities here:
- Start up a MongoDB instance in Azure’s Cosmos DB
- Populate data into that collection
- Use that data in a simple application
Setting up MongoDB
So this was rather simple and easy (thanks Microsoft!). You go into azure portal , click on resources, search for Azure Cosmos DB , then type in your (1) ID , and select(2) API (in my case I wanted mongoDB but there are 5 to choose from (SQL , Cassandra, Azure Table, Gremlin (aka graph) and of course MongoDB) . Finally you select a (3) subscription, (4) your resource group, and a (5)location, and if you wanted to see the power of multi master you could even enable that.
I used the Azure Data Explorer to create my database and my first collection, but I was wondering if I could use standard MongoDB software to connect to this instance, so I looked around for available and popular clients for mongoDb.
- No SQL https://nosqlbooster.com/downloads
- Compose https://www.compose.com/
- Studio 3T https://studio3t.com/
I downloaded NoSQLBooster after a 5 minute timebox review of software; seemed smaller, no cost, and I could be clearer on what level of compatibility there was as it was the less common of clients.
For those going the Studio 3T path, In my search I did find an article for using Studio 3T:
https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongochef
That being said other than getting my SSL settings wrong it wasn’t to difficult, below are my 3 tabs showing the configuration within the application.
and a successful connection.
Populate Data
I found some sample data to import from ozlerhakan :
https://github.com/ozlerhakan/mongodb-json-files/blob/master/datasets/companies.json
It was bigger and more complex and just what I needed to make this test a little more real, something I feel most articles skimp on.
I hit an issue where I had originally setup a unique key on my collection via the Azure Data Explorer and it was causing import problems even though the id field in this sample data did look unique . For a quick fix, I deleted the collection and recreated it.
I used NoSQLBooster’s import from Json and proceeded to load the file.
8 minutes later I had imported 18k rows of data.
Now keep in mind lots of little things in the application didn’t work right. For example, I tried to get a statistical info that this application said it could get ,but this version of mongo from azure didn’t support it. Tried to do an aggregate and it mentioned I would have to enable MongoDB Aggregation Pipeline . I enabled it, restarted and … same error. Great. Well I am not here to specifically test mongoDB aggregate but that is a problem I don’t love.
Using the Data
I start by creating a Console app (.net Standard).
I then added nuget packages for mongoDB.Driver, and mongoDB.Bson.
I wanted to map directly to a object and manipulate as a POCO so I used a Json to C# Class online converter, and that seemed to work well enough.
https://jsonutils.com/
Added those classes into my project and let resharper break them out into files, and I proceeded to create a simple connection class. My goal for this code was that it could connect , and it could retrieve data. That is my MVP for this effort. I grabbed some ideas on how to implement this quickly from an article:
https://docs.microsoft.com/en-us/azure/cosmos-db/create-mongodb-dotnet
But once again even with a few months of time on it , things had changed. I had to stumble in a couple situations when it came to connecting to Cosmo DB\MongoDB and I posted it on the MS site.
https://github.com/MicrosoftDocs/azure-docs/issues/8822
Probably the larger effort was that while the utility tried to identify some object data types it wasn’t perfect so I had to go through the object as it was consuming the data and correct data types. I also hit an odd issue where the “id” inside of the FundingRound object wasn’t being mapped to its id, even though it existed.
I couldn’t figure this one out so I used the decorator attribute of [BsonIgnoreExtraElements] on the FundingRound clas and my primary class. This basically says if I can’t find somewhere to map the data to, ignore it and move on. For more information on deseralization, this document in MongoDB is a good starting place.
https://mongodb-documentation.readthedocs.io/en/latest/ecosystem/tutorial/serialize-documents-with-the-csharp-driver.html
Microproject completed.
You can download it here. Just update the app.config with your connection values.
Peace and grace in your next coding effort .