veuillez trouver la solution ci-dessous :il s'agit d'un code flexible utilisant c# dans le script powershell et les derniers pilotes mongo (2.2.3) - vous pouvez donc jouer avec le code c# si nécessaire :-)
$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()
voir capture d'écran :