【发布时间】:2019-12-14 17:55:06
【问题描述】:
我需要运行一个从管道查询 MongoDB 集合的 powershell 脚本。我正在使用最新的 MongoDB 驱动程序 2.9.3。我写的脚本如下 -
$Assem = ("D:\PoweshellMongoDB\Drivers\MongoDB.Bson.dll", "D:\PoweshellMongoDB\Drivers\MongoDB.Driver.dll", "D:\PoweshellMongoDB\Drivers\MongoDB.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:\PoweshellMongoDB\PowerMongo.ps1:34 char:1
+ [MongoDBSample.MongoRepository]::Connect()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException
我不确定我缺少哪个参考。有更好的方法吗?还是我需要将驱动程序更新到旧版本?请指教。
【问题讨论】:
-
你看过这种方法吗:stackoverflow.com/questions/2664028/…?
标签: mongodb powershell .net-core mongodb-.net-driver