繁体   English   中英

使用 C# 驱动程序更新 MongoDb 中数组数组内的字段

[英]Update a field which is inside array of array in MongoDb using C# driver

我正在尝试更新嵌套数组或列表中的字段。 使用.Net Mongo 驱动程序。

我的数据结构是这样的:

[{
  "_id": {
    "$oid": "602cb1627661691f87bb1e6e"
  },
  "Events": [
    {
      "_id": "5ffd8d7f7d553920b9608f38",
      "Roles": [
        {
          "_id": {
            "$oid": "6017a4616609ff520ad2cd26"
          }
        }
      ],
      "Sessions": [],
      "IsProfileVisible": false,
      "Aois": [],
      "ViewOrder": 1,
      "HelloWorldVideo": {
        "Duration": 0,
        "GroupId": "5ffd8d7f7d553920b9608f38",
        "TagId": "TESTING",
        "Title": "Testing title",
        "VideoUrls": "https://cdn.azureedge.net/3734/helloworld/elephants-dream.webm",
        "_id": {
          "$oid": "603752797904007c5cc93e78"
        },
        "ProcessedVideoUrls": "https://cdn.azureedge.net/3734/helloworld/elephants-dream.webmProcessed",
        "TagTypeId": null,
        "Thumbnail": "https://cdn.azureedge.net/3734/helloworld/elephants-dream.webm",
        "VideoType": "1",
        "VttUrl": "https://cdn.azureedge.net/3734/helloworld/elephants-dream.webmvtt"
      },
      "SoapboxVideos": [
        {
          "_id": {
            "$oid": "60373e34ad2fe9624705f40d"
          },
          "VideoPaths": null,
          "Title": "Testing title",
          "TagId": "TESTING",
          "TagTypeId": null,
          "Thumbnail": null,
          "GroupId": "5ffd8d7f7d553920b9608f38",
          "VideoImageUrl": null,
          "VideoType": null,
          "VideoUrls": "https://cdn.azureedge.net/3734/helloworld/elephants-dream.webm",
          "ProcessedVideoUrls": null,
          "VideoId": null,
          "VttUrl": null,
          "IsDeleted": false,
          "Duration": 0
        },
        {
          "_id": {
            "$oid": "603740d4ad2fe9624705f40e"
          },
          "VideoPaths": null,
          "Title": "Testing title",
          "TagId": "TESTING",
          "TagTypeId": null,
          "Thumbnail": null,
          "GroupId": "DigitalEvents_5ffd8d7f7d553920b9608f38",
          "VideoImageUrl": null,
          "VideoType": null,
          "VideoUrls": "https://cdn.azureedge.net/3734/helloworld/elephants-dream1.webm",
          "ProcessedVideoUrls": null,
          "VideoId": null,
          "VttUrl": null,
          "IsDeleted": false,
          "Duration": 0
        },
        {
          "_id": {
            "$oid": "603752ce7904007c5cc93e79"
          },
          "VideoPaths": null,
          "Title": "Testing title",
          "TagId": "TESTING",
          "TagTypeId": null,
          "Thumbnail": null,
          "GroupId": "DigitalEvents_5ffd8d7f7d553920b9608f38",
          "VideoImageUrl": null,
          "VideoType": null,
          "VideoUrls": "https://cdn.azureedge.net/3734/helloworld/elephants-dream.webm",
          "ProcessedVideoUrls": null,
          "VideoId": null,
          "VttUrl": null,
          "IsDeleted": false,
          "Duration": 0
        }
      ]
    }
  ]
}]

由于它需要两个位置运算符,因此我遵循以下方法。 我正在尝试更新 SoapboxVideos 列表中的 ProcessedVideoUrls、VttUrl。

updateResult = await _userCollection.UpdateOneAsync(x => x.Id == updateVideoPath.UserId,            // 602cb1627661691f87bb1e6e
                                               Builders<UserModel>.Update.Set("Events.$[event].SoapboxVideos.$[soapbox].ProcessedVideoUrls", updateVideoPath.VideoUrls)
                                                                          .Set("Events.$[event].SoapboxVideos.$[soapbox].VttUrl", updateVideoPath.VttUrl),
                                               new UpdateOptions
                                               {
                                                   ArrayFilters = new List<ArrayFilterDefinition>
                                                    {
                                                        new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("event.Id", updateVideoPath.EventId)), // 5ffd8d7f7d553920b9608f38
                                                        new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("soapbox.Id", updateVideoPath.Id))   // 603752ce7904007c5cc93e79
                                                    }
                                               });
                                           

当它被执行时,我没有得到任何异常。相反,我将 MatchedCount 设为 1,将 ModifiedCount 设为 0。请注意,我尝试更新的字段确实具有值,这也不是现有值。

任何有助于理解该问题的帮助表示赞赏。 我正在使用 Mongo 驱动程序版本号 2.11.6

以下代码更改解决了该问题

new UpdateOptions
                                               {
                                                   ArrayFilters = new List<ArrayFilterDefinition>
                                                    {
                                                        new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("event._id",  updateVideoPath.EventId)),
                                                        new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("soapbox._id", ObjectId.Parse( updateVideoPath.Id)))
                                                    }
                                               });

实际问题是 Soapbox Id 类型,它是 ObjectId,我使用了属性“ soapbox.Id ”而不是“ soapbox._id ”。 这些变化解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM