Automate MongoDB Integration Tasks from PowerShell
The CData ADO.NET Provider for MongoDB is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MongoDB.
About MongoDB Data Integration
Accessing and integrating live data from MongoDB has never been easier with CData. Customers rely on CData connectivity to:
- Access data from MongoDB 2.6 and above, ensuring broad usability across various MongoDB versions.
- Easily manage unstructured data thanks to flexible NoSQL (learn more here: Leading-Edge Drivers for NoSQL Integration).
- Leverage feature advantages over other NoSQL drivers and realize functional benefits when working with MongoDB data (learn more here: A Feature Comparison of Drivers for NoSQL).
MongoDB's flexibility means that it can be used as a transactional, operational, or analytical database. That means CData customers use our solutions to integrate their business data with MongoDB or integrate their MongoDB data with their data warehouse (or both). Customers also leverage our live connectivity options to analyze and report on MongoDB directly from their preferred tools, like Power BI and Tableau.
For more details on MongoDB use case and how CData enhances your MongoDB experience, check out our blog post: The Top 10 Real-World MongoDB Use Cases You Should Know in 2024.
Getting Started
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for MongoDB; this tutorial shows how to use the Provider to create, retrieve, update, and delete MongoDB data.
Once you have acquired the necessary connection properties, accessing MongoDB data in PowerShell can be enabled in three steps.
Set the Server, Database, User, and Password connection properties to connect to MongoDB. To access MongoDB collections as tables you can use automatic schema discovery or write your own schema definitions. Schemas are defined in .rsd files, which have a simple format. You can also execute free-form queries that are not tied to the schema.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for MongoDB\lib\System.Data.CData.MongoDB.dll") -
Connect to MongoDB:
$conn= New-Object System.Data.CData.MongoDB.MongoDBConnection("Server=MyServer;Port=27017;Database=test;User=test;Password=Password;") $conn.Open() -
Instantiate the MongoDBDataAdapter, execute an SQL query, and output the results:
$sql="SELECT borough, cuisine from restaurants" $da= New-Object System.Data.CData.MongoDB.MongoDBDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.borough $_.cuisine }
Update MongoDB Data
$cmd = New-Object System.Data.CData.MongoDB.MongoDBCommand("UPDATE restaurants SET Name='Morris Park Bake Shop' WHERE _id = @my_id", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MongoDB.MongoDBParameter("@my_id","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert MongoDB Data
$cmd = New-Object System.Data.CData.MongoDB.MongoDBCommand("INSERT INTO restaurants (Name) VALUES (@myName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MongoDB.MongoDBParameter("@myName","Morris Park Bake Shop")))
$cmd.ExecuteNonQuery()
Delete MongoDB Data
$cmd = New-Object System.Data.CData.MongoDB.MongoDBCommand("DELETE FROM restaurants WHERE _id=@my_id", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MongoDB.MongoDBParameter("@my_id","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject