【问题标题】:MongoDB Update nested Array object where there is a match in array documentMongoDB更新嵌套数组对象,其中数组文档中有匹配项
【发布时间】:2017-02-20 05:44:01
【问题描述】:

这是我的 MongoDB 文档

{  
   "_id":20,
   "GroupId":"45",
   "Name":"Some Name",
   "NestedArray":[  
      {  
         "Id":3,
         "Name":"NesName",
         "IsDeleted":false
      }
   ]
}

我需要写一个更新语句,比如(在 SQL 解释中)

update MyCollections.NestedArray set MyCollections.NestedArray[x].IsDeleted = true where MyCollections.NestedArray[x].Id = 3

这是我尝试过的

 var groupFilter = Builders<MyType>.Filter.Eq(x => x.Id, 45);
 var nestedArayDocUpdate = Builders<MyType>.Update.Set(x => x.NestedArray[0].IsDeleted, true);
 mongoDbRepository.UpdateMany(groupFilter, nestedArayDocUpdate,
               new UpdateOptions {IsUpsert = false, BypassDocumentValidation = false});

使用 MongoDB 3.2 我怎样才能提出 MongoDB C# 驱动程序查询?

【问题讨论】:

    标签: c# mongodb


    【解决方案1】:

    这就是我最终做到的方式。

     var updateBuilder = Builders<Type>.Update.
                               .Set(x => x.NestedArray[-1].IsActive, false)
                               .Set(x => x.NestedArray[-1].IsDeleted, true);
    
    mongoDbRepository.UpdateOne( Builders<Type>.Filter.Where( 
       x => x.NestedArray.Any(c => c.Id == categoryId)), updateBuilder);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多