![]() |
VOOZH | about |
The CData Cmdlets for MongoDB are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MongoDB.
Accessing and integrating live data from MongoDB has never been easier with CData. Customers rely on CData connectivity to:
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.
The Cmdlets are not only a PowerShell interface to MongoDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MongoDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MongoDB. To access MongoDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MongoDB.
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.
Install the module:
Install-Module MongoDBCmdlets
Connect:
$mongodb = Connect-MongoDB -Server "$Server" -Port "$Port" -Database "$Database" -User "$User" -Password "$Password"
Search for and retrieve data:
$name = "Morris Park Bake Shop" $restaurants = Select-MongoDB -Connection $mongodb -Table "restaurants" -Where "Name = `'$Name`'" $restaurants
You can also use the Invoke-MongoDB cmdlet to execute SQL commands:
$restaurants = Invoke-MongoDB -Connection $mongodb -Query 'SELECT * FROM restaurants WHERE Name = @Name' -Params @{'@Name'='Morris Park Bake Shop'}
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 -Connection $MongoDB -Columns @('borough','cuisine') -Values @('Myborough', 'Mycuisine') -Table restaurants -Id "My_id"
$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()
Add-MongoDB -Connection $MongoDB -Table restaurants -Columns @("borough", "cuisine") -Values @("Myborough", "Mycuisine")
$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()
Remove-MongoDB -Connection $MongoDB -Table "restaurants" -Id "My_id"
$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
Download a free trial of the MongoDB Data Provider to get started:
Download NowLearn more:
👁 MongoDB IconRapidly create and deploy powerful .NET applications that integrate with MongoDB document databases.