Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\> Add-Type -Path "C:\mongodb\drivers\csharp\CSharpDriver-1.9.0\MongoDB.Bson.dll"
PS C:\> Add-Type -Path "C:\mongodb\drivers\csharp\CSharpDriver-1.9.0\MongoDB.Driver.dll"
PS C:\> $client = New-Object -TypeName "MongoDB.Driver.MongoClient" "mongodb://localhost"
PS C:\> $server = $client.GetServer()
PS C:\> $database = $server.GetDatabase("test")
PS C:\> $collection = $database.GetCollection("test")
PS C:\> $keys = [MongoDB.Driver.Builders.IndexKeys]::Ascending("a", "b", "c")
PS C:\> $options = [MongoDB.Driver.Builders.IndexOptions]::SetName("abcIndex")
PS C:\> $collection.CreateIndex($keys, $options)
DocumentsAffected : 0
HasLastErrorMessage : False
LastErrorMessage :
Upserted :
UpdatedExisting : False
Code :
Command :
CommandName :
Response : {ok=1, n=0}
ErrorMessage :
Ok : True
ServerInstance :
PS C:\>
You'll have to adjust the paths used in the Add-Type statements depending on where your driver DLLs are located.
Using the MongoDB shell, I ran this before those Powershell statements:
> db.test.drop()
true
> db.test.getIndexes()
[ ]
>
And using the MongoDB shell again afterwards, checked that the index was properly created:
> db.test.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.test"
},
{
"v" : 1,
"key" : {
"a" : 1,
"b" : 1,
"c" : 1
},
"name" : "abcIndex",
"ns" : "test.test"
}
]
>