简体   繁体   中英

MongoDb 3.0 C# updating subdocument of subdocument

Below is an example of my document. I am trying to update the CostReports part based on the id of the CostReportingPeriods element.

{
"_id" : "240106",
"CostReportingPeriods" : [ 
    {
        "FyBgnDt" : ISODate("2000-01-01T05:00:00.000Z"),
        "FyEndDt" : ISODate("2000-12-31T05:00:00.000Z"),
        "_id" : "240106-20000101-20001231",
        "CostReports" : []
    }, 
    {
        "FyBgnDt" : ISODate("2001-01-01T05:00:00.000Z"),
        "FyEndDt" : ISODate("2001-12-31T05:00:00.000Z"),
        "_id" : "240106-20010101-20011231",
        "CostReports" : []
    }, 
    {
        "FyBgnDt" : ISODate("2002-01-01T05:00:00.000Z"),
        "FyEndDt" : ISODate("2002-12-31T05:00:00.000Z"),
        "_id" : "240106-20020101-20021231",
        "CostReports" : []
    }, 
    {
        "FyBgnDt" : ISODate("2003-01-01T05:00:00.000Z"),
        "FyEndDt" : ISODate("2003-12-31T05:00:00.000Z"),
        "_id" : "240106-20030101-20031231",
        "CostReports" : []
    }
]

I am using the following code to try and update that element but am getting an error that says cannot use the element (CostReportingPeriods of CostReportingPeriods.CostReports) to traverse the element. If I add CostReportingPeriods.0.CostReports it will add it to the first element of the array, regardless of the filter.

var builder = Builders<MongoModels.Provider>.Filter;
var filter = builder.Eq("_id",costReport.PRVDR_NUM) & builder.Eq("CostReportingPeriods._id", costReport.UNIQUE_ID);
var update = Builders<MongoModels.Provider>.Update.AddToSet("CostReportingPeriods.CostReports", Mapper.Map<CostReport, MongoModels.CostReport>(costReport, opt =>
          {
              opt.AfterMap((src, dest) => dest.Worksheets = CreateWorksheets(dest.RptRecNum).ToList());
          }));

How do I get it to update the element I want it to update based on the id of the subdocument?

在尝试了许多不同的事情之后,通过将更新过滤器从“ CostReportingPeriods.CostReports”更改为“ CostReportingPeriods。$。CostReports”,它可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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