Search code examples
powershellmongodb-.net-driver

create index in mongodb using powershell


* EDITED * could someone please help me creating an index in mongodb using powershell?

I am following this create index using c# driver

this is my code:

$keys = IndexKeys.Ascending("Message Subject", "Job Result").Descending("Time Stamp")
$options = new-object MongoDB.Driver.IndexOptionsDocument
$options.SetUnique(true)
$notificationCollectionByDate.CreateIndex(keys, options)

I got the following errors

Method invocation failed because [MongoDB.Driver.IndexOptionsDocument] doesn't contain a method named 'SetUnique'.

The term 'IndexKeys.Ascending' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and tr y again.

Any idea what is wrong with my code?

thank you very much


Solution

  • It's just like the keys. Don't create a document if you are using the helpers.

    $options = [MongoDB.Driver.Builders.IndexOptions]::SetUnique(true)