【问题标题】:create index in mongodb using powershell使用powershell在mongodb中创建索引
【发布时间】:2014-04-06 21:52:15
【问题描述】:

* 已编辑 * 有人可以帮我在 mongodb 中使用 powershell 创建索引吗?

我正在关注这个create index using c# driver

这是我的代码:

$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] 不包含名为“SetUnique”的方法。

“IndexKeys.Ascending”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查拼写 的名称,或者如果包含路径,请验证该路径是 正确并再试一次。

知道我的代码有什么问题吗?

非常感谢

【问题讨论】:

    标签: powershell mongodb-.net-driver


    【解决方案1】:

    就像钥匙一样。如果您正在使用帮助程序,请不要创建文档。

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

    【讨论】:

    • 感谢 Craig,不,它不起作用我不知道为什么它也无法识别 IndexKeys 中的任何方法......
    • 嗯,这与您发布的问题不同。您可以使用完全限定名称 (MongoDB.Driver.Builders)。此外,您似乎错误地调用了静态方法(technet.microsoft.com/en-us/library/dd347632.aspx)。
    【解决方案2】:

    感谢克雷格你给了我答案的线索:

    这是解决方案:

    $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)
    

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 2020-10-08
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      相关资源
      最近更新 更多