Skip to content

Instantly share code, notes, and snippets.

@krote
Created April 14, 2014 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krote/10628868 to your computer and use it in GitHub Desktop.
Save krote/10628868 to your computer and use it in GitHub Desktop.
###########################################
# MongoDB Sample
# Driver:https://github.com/mongodb/mongo-csharp-driver/releases
# MongoDB Bson Namespace : http://api.mongodb.org/csharp/current/
###########################################
Add-Type -Path "C:\mongodb-win32-x86_64-2008plus-2.4.6\CSharpDriver-1.9\MongoDB.Bson.dll"
Add-Type -Path "C:\mongodb-win32-x86_64-2008plus-2.4.6\CSharpDriver-1.9\MongoDB.Driver.dll"
$dbName = "test"
$connectionString = "mongodb://localhost/{0}?safe=true" -f $dbName
$db = [MongoDB.Driver.MongoDatabase]::Create($connectionString)
$collectionName = "servers"
$collection = $db[$collectionName]
$doc1 = New-Object MongoDB.Bson.BsonDocument
$doc1.Add("Name", "sv001")
$doc1.Add("OS", "WindowsServer2008")
$doc1.Add("setup-date","2014/04/11")
$collection.save($doc1)
$doc2 = New-Object MongoDB.Bson.BsonDocument
$doc2.Add("Name", "sv002")
$doc2.Add("OS", "WindowsServer2008")
$doc2.Add("setup-date". "2014/04/12")
$doc3 = New-Object MongoDB.Bson.BsonDocument
$doc3.Add("Name", "sv003")
$doc3.Add("OS", "WindowsServer2003")
$doc3.Add("setup-date". "2014/04/15")
$doc3.Add("type", "virtual")
$doc4 = New-Object MongoDB.Bson.BsonDocument
$doc4.Add("Name", "sv004")
$doc4.Add("OS", "WindowsServer2008")
$doc4.Add("setup-date". "2014/04/17")
$doc4.Add("type", "virtual")
$barray = New-Object MongoDB.Bson.BsonArray
$barray.Add($doc3)
$barray.Add($doc4)
$doc2.Add("Childlen", $barray)
$collection.save($doc2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment