以下の解決策を見つけてください:これは、PowerShellスクリプト内のc#と最新のmongoドライバー(2.2.3)を使用する柔軟なコードです-必要に応じてc#コードで遊ぶことができます:-)
$mongoDbDriverPath = "C:\work\mongo"
$dbName = "deser"
$collectionName = "Foo"
$Assem = ( "$($mongoDbDriverPath)\MongoDB.Bson.dll", "$($mongoDbDriverPath)\MongoDB.Driver.dll","$($mongoDbDriverPath)\MongoDB.Driver.Core.dll")
$Source = @”
namespace profesor79
{
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
public static class Executor
{
public static List<Foo> GetData()
{
var connectionString = "mongodb://localhost:27017";
var _client = new MongoClient(connectionString);
var _database = _client.GetDatabase("deser");
var cole = _database.GetCollection<Foo>("Foo");
cole.InsertOne(new Foo());
var data = cole.Find<Foo>((new BsonDocument())).ToList();
return data;
}
public class Foo
{
public ObjectId Id { get; set; }
[BsonDictionaryOptions]
public Dictionary<string, string> Bar = new Dictionary<string, string>() { { "1", "text" }, { "2", "text" } };
}
}
}
"@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
[profesor79.Executor]::GetData()
スクリーンショットを参照してください: