2017-01-16 75 views
2

据我所知,mongodb知道Dictionary作为一个对象,并且它不能执行任何与数组有关的操作。我改变了序列化并尝试了各种类型的字典序列化。但没有机会。
所以我加载我的领域(字典)(全部)到内存中,更新它并将其设置回mongodb。
有没有什么办法来upsert在mongodb用c#驱动程序字典?MongoDb中的Upsert字典


我的文档类型:

public class Site 
    { 
     public string Id { get; set; } 
     //[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] 
     public Dictionary<string,string> Properties { get; set; } 
    } 

我的更新操作:

public ServiceResult UpdateProperties(string id, Dictionary<string,string> properties) 
     { 
      var baseList = Collection.Find(m => m.Id == id) 
       .Project(s => s.Properties) 
       .FirstOrDefault(); 

      if (baseList == null) 
      { 
       baseList = properties; 
      } 
      else 
      { 
       baseList.Upsert(properties); //update,insert dic by new one 
      } 

      var filter = Builders<Site>.Filter 
       .Eq(m => m.Id, id); 

      var update = Builders<Site>.Update 
       .Set(m => m.Properties, baseList); 

      try 
      { 
       Collection.UpdateOne(filter, update); 

       return ServiceResult.Okay(Messages.ItemUpdated); 

      } 
      catch (Exception ex) 
      { 
       return ServiceResult.Exception(ex); 
      }  
     } 

我真的很感激任何帮助,您可以提供。


歧义:

public static class DictionaryExtensions 
    { 
     public static void Upsert<TKey, TValue>(this Dictionary<TKey, TValue> source, 
              Dictionary<TKey, TValue> newOne) 
     { 
      foreach (var item in newOne) 
      { 
       source[item.Key] = item.Value; 
      } 
     } 
    } 

回答

1

你可以去通过你想更新/插入所有属性和做它为每个属性:

UpdateDefinition<Site> upsert = null; 
if (properties.Any()) 
{ 
    var firstprop = properties.First(); 
    upsert = Builders<Site>.Update.Set(nameof(Site.Properties) + "." + firstprop.Key, 
           firstprop.Value); 

    foreach (var updateVal in properties.Skip(1)) 
    { 
     upsert = upsert.Set(nameof(Site.Properties) + "." + updateVal.Key, 
              updateVal.Value); 
    } 

    collection.UpdateOne(r => r.Id == "YourId", upsert, 
               new UpdateOptions { IsUpsert = true }); 
} 

答案以前的版本,有多个更新:

foreach (var updateVal in properties) 
{ 
    collection.UpdateOne(r => r.Id == "YourId", 
     Builders<Site>.Update.Set(nameof(Site.Properties)+ "." + updateVal.Key, 
            updateVal.Value), 
            new UpdateOptions { IsUpsert = true}); 
} 

请注意,这只会添加新的键/值或更新现有的,这不会删除任何东西。

+0

它的工作原理。真正好点的人。让看看是否有任何建议,以避免多连接(更新)。到目前为止,这是我接受的答案。 – Soren

+0

我已经更新了答案,这个执行更新只是一次:) –

+0

工作就像一个魅力。感谢您的时间和考虑。 – Soren