2016-01-21 49 views
-1

我有更新(替換)MongoDb中的文檔的問題,我已經可以更新文檔,但我無法更新子文檔。mongodb C#驅動程序中的批量更新

文獻內容是:

{ 
    "_id" : 1, 
    "Code" : LUUID("00000000-0000-0000-0000-000000000000"), 
    "Zip" : 123456, 
    "Text" : "sample01", 
    "Tags" : { 
     "_id" : 1, 
     "Tag" : "tag1" 
    } 
} 
{ 
    "_id" : 2, 
    "Code" : LUUID("00000000-0000-0000-0000-000000000000"), 
    "Zip" : 9.87654e+008, 
    "Text" : "sample02", 
    "Tags" : { 
     "_id" : 1, 
     "Tag" : "tag1" 
    } 
} 
{ 
    "_id" : 3, 
    "Code" : LUUID("00000000-0000-0000-0000-000000000000"), 
    "Zip" : 134679, 
    "Text" : "sample03", 
    "Tags" : { 
     "_id" : 1, 
     "Tag" : "tag1" 
    } 
} 

我想更新第一和第二文件的Tags(其中"_id"是1或2)。 我試過這個:

var tags = new PostTag { Id = 8, Tag = "tag8" };   
var filter = Builders<Post>.Filter.In(x => x.Id, new List<int> { 1, 2 }); 
var update = Builders<Post>.Update.Set(z => z.Tags, tags); 
collection.UpdateManyAsync(filter, update); 

但是更新失敗。

+1

和例外是... ???我不明白爲什麼在C#中使用Id而不是_id在PostTag中 – ymz

+1

請首先閱讀[問]幷包括更新失敗的確切原因。 – CodeCaster

+0

你的代碼對我來說看起來是正確的。 'UpdateResult'中的值是什麼,特別是'MatchedCount'和'ModifiedCount'? – Peter

回答

0

代碼的所有行是正確的,而不是這一行:

collection.UpdateManyAsync(filter, update); 

如果你不把它作爲等待,能!
使自己等待,能夠使用這樣的:

collection.UpdateManyAsync(filter, update).Wait();