从 Powershell 脚本查询 Mongo 集合



我需要运行一个从管道查询MongoDB集合的powershell脚本。我正在使用最新的MongoDB驱动程序2.9.3。我把剧本写成如下——

$Assem = ("D:PoweshellMongoDBDriversMongoDB.Bson.dll", "D:PoweshellMongoDBDriversMongoDB.Driver.dll", "D:PoweshellMongoDBDriversMongoDB.Driver.Core.dll")
$Source = @"
using MongoDB.Bson;
using MongoDB.Driver;
using System;
namespace MongoDBSample
{
public static class MongoRepository
{
public static void Connect()
{
try
{
var mongoClient = new MongoClient("mongodb://localhost:27017");
var database = mongoClient.GetDatabase("ILP4");
var collection = database.GetCollection<BsonDocument>("Issues");
var count = collection.CountDocumentsAsync(new BsonDocument()).ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
"@
Add-Type  -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  
[MongoDBSample.MongoRepository]::Connect()

但是当我调试脚本时,我得到以下错误 -

Exception calling "Connect" with "0" argument(s): "Could not load file or assembly 'MongoDB.Driver, Version=2.9.3.0, Culture=neutral, PublicKeyToken=null' or 
one of its dependencies. The system cannot find the file specified."
At D:PoweshellMongoDBPowerMongo.ps1:34 char:1
+ [MongoDBSample.MongoRepository]::Connect()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException

我不确定我缺少哪个参考。有没有更好的方法?还是我需要将驱动程序更新到旧版本?请指教。

我找到了解决方案。我创建了一个.NET Framework类库,它使用MongoDB驱动程序2.3.1连接到MongoDB。在我的管道中,我将安装程序创建为 - 从源代码管理中签出库代码,构建并发布 dll,从我的内联 powershell 脚本中,我正在引用这个 dll 并执行它。

最新更新