2016-01-21 56 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();