2014-04-05 78 views
0

*編輯* 有人可以幫我創建一個使用PowerShell的mongodb索引嗎?使用powershell在mongodb中創建索引

我下面這個

,這是我的代碼:

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

我得到了以下錯誤

方法調用失敗,因爲[MongoDB.Driver.IndexOptionsDocument] 沒有按」 t包含一個名爲'SetUnique'的方法。

術語'IndexKeys.Ascending'不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。檢查名稱的拼寫 ,或者如果包含路徑,請驗證路徑是 正確並再次運行。

任何想法我的代碼有什麼問題?

非常感謝你

回答

1

它就像鑰匙。如果您使用助手,請勿創建文檔。

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

+0

感謝克雷格,沒有它不工作,我不知道爲什麼它不認可IndexKeys的任何方法... –

+0

好吧,這是一個不同於你發佈的問題。您可以使用完全限定名稱(MongoDB.Driver.Builders)。另外,您似乎錯誤地調用了靜態方法(http://technet.microsoft.com/zh-cn/library/dd347632.aspx)。 –

0

克雷格感謝你給我的線索,得到的答案:

這是解決方案:

$keys = [MongoDB.Driver.Builders.IndexKeys] 
$keys = [MongoDB.Driver.Builders.IndexKeys]::Ascending("Message Subject", "Job Result") 
$keys = [MongoDB.Driver.Builders.IndexKeys]::Descending("Time Stamp") 
$options = [MongoDB.Driver.Builders.IndexOptions]::SetUnique($true) 
$options = [MongoDB.Driver.Builders.IndexOptions]::SetDropDups($true) 
$notificationCollectionByDate.CreateIndex($keys, $options)