2017-01-16 49 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