Objective
I wanted to do a bulk records update and read in MongoDB using my client program which is written in C# console application (as my obvious love for that language
).
).Approach
I could have used the MongoDB APIs in the application and written functions to do read, update, etc. but at the time of writing that program, I had never used that API. I knew MongoDB native commands and C# and I wanted to reduce the learning curve at that time. As I started researching the smarter way of achieving my objective, I concluded the following. (I am not claiming that it is the smartest, but it is a way around to achieve with the syntax knowledge that I had at that time!
). For now, the approach that I decided,
). For now, the approach that I decided,- MongoDB activities run faster if you use the native MongoDB commands in its syntax. In fact, the single-line command to bulk upload a CSV file is so much easier to read, understand and manage! The catch is, you should know the syntax. I will show the required syntax here and you can take it further if interested in the technology.
- PowerShell script helps you run the above discussed MongoDB code from outside the MongoDB console. This helps me externalize the code and also allows my custom logic in preparing the script (the MongoDB one!), which may vary in every run.
- Since I had a couple of other tasks to perform, like Excel scraping, validating, data cleansing and finally loading everything to SharePoint etc, I chose the C# Console program to call the Powershell script and do the job.
- Overall, accessing the MongoDB is just a small part of my workflow and thus I was not very interested in learning the syntax of the MongoDB API in C#. I am well versed in MongoDB command syntax and preliminary PowerShell syntax and thought to apply the same.
I took an hour to finalize a sample MongoDB collection, read and run it from the PowerShell console, without opening the MongoDB console. It looked fine but here comes the first roadblock! The moment I saved the PowerScript file as "PS1" and tried executing it from command prompt - I got the message that executing PS1 script is blocked as security measures implemented by my company Infra team. So without PowerScript, I have no bridge between my C# program and MongoDB native commands.
Way Around!
Use the System.Management.Automation to run the PowerShell script in the memory itself when the C# program runs. Store the JS or JSON command/data in a file and pass the path as a parameter to the PowerShell script. So having finalized the options, here is the code.
- //-- libraries to be used (I am only including the non-default ones, here)
- using System.Management.Automation;
- using System.Collections.ObjectModel;
- using System.Collections.Generic;
- using System.Linq;
- //----------------------------
- //---Example function to upload data in a CSV (multiple rows) to MongoDB
- public void Upload2Mongo(string csvfilepath)
- {
- using (PowerShell PowerShellInstance = PowerShell.Create())
- {
- PowerShellInstance.AddScript("param($filepath) mongoimport --db dome --collection demands --type csv --mode=upsert --headerline --file $filepath --ignoreBlanks");
- PowerShellInstance.AddParameter("filepath", csvfilepath);
- Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
- }
- }
What we are doing here? The PowerShell object is from the Automation DLL. Line No 05: is adding a script line to the PowerScript instance. If you were writing a physical PS1 file, you would have written the line in that file. This is a MongoDB syntax. The parameters are self-explanatory. I have used a database named dome that contains a collection called demands. I am uploading a file of type CSV in an upsert mode. There is a header line in the file (1st row of CSV). Don't create an element if the value is blank.

Join the conversation! Your thoughts help the community grow.