在 C# 中使一对多关系的一方 MongoDB 了解多方

Making One-Side of One-To-Many Relationship in C# MongoDB know about the Many-Side

全部:

在我们的应用程序中,有一个日志模型 class 和一个日志事件模型 Class。 日志模型 Class(一对多关系)LogEvent 模型 Class.

这是对数模型Class(一侧)

    public class ELLCsLog : ELLCsInterfaceLog
    {

         public Object Id { get; set; }
         public int UserID { get; set; }

         public DateTime DateTimeOfInterest { get; set; }
         public String DutyCycleCode { get; set; }
         public IList<Object> logEventsIdList { get; set; }
    } // end of public class ELLCsLog

这是 LogEvent 模型 Class(多边)

public class ELLCsLogEvent : ELLCsInterfaceLogEvent
{
    public Object Id { get; set; }
    public Object UmbrellaLogId { get; set; }
    public Double StartTime { get; set; }
    public Double EndTime { get; set; }
    public Double Duration { get; set; }
    public int isSubmitted { get; set; }
    public int isFrozen { get; set; }
    public String Remarks { get; set; }
    public String Location { get; set; }
    public String StatusCode { get; set; }
}

在我的代码中,我创建了对象的 BsonDocument 版本,然后将它们插入 MongoDB 数据库。

1-)给定一个代表父 LogId 的 UmbrellaLogId,下面的代码将一个 LogEvent 实例插入到 MongoDB 数据库中。

2-)下一步涉及检索我刚刚插入的同一个 LogEvent 实例,以便我可以获得由 MongoDB 生成的 LogEvent 的 Id。 (我知道它效率低下,但我现在正在尝试让代码正常工作)

3-)我还检索了父日志(即带有 UmbrellaLogId 的日志),以便我可以将 LogEvent 的 ID 添加到 IList logEventsIdList 集合中。

4-) ?这是我真正迷路的地方,我不知道如何将 LogEvent 的 Id 添加到 IList logEventsIdList?我必须使用 BsonDocument 吗?我们该怎么做?

               MongoDB.Bson.ObjectId  UmbrellaLogIdAsObjectId = (ObjectId)UmbrellaLogId;
                BsonDocument logEventBsonDoc = new BsonDocument { 
                                         {"UmbrellaLogId",    (BsonValue)UmbrellaLogIdAsObjectId}
                                        , {"StartTime", StartTime}
                                        , {"EndTime", EndTime}
                                        , {"Duration", Duration}
                                        , { "isSubmitted", isSubmitted}
                                        , { "isFrozen", isFrozen}
                                        , { "Remarks", Remarks }
                                        , { "Location", Location}
                                        , { "StatusCode", StatusCode }
                                     }; // end of new BsonDocument
                objUtility = new Utility();
                objUtility.Insert(logEventBsonDoc,     "FMS_TM_MST_LogEvents");

                string[] arrFields = {"Id",
                                         "UmbrellaLogId", 
                                           "StartTime", 
                                               "EndTime",
                                                 "Duration",
                                                   "isSubmitted",
                                                     "isFrozen",
                                                      "Remarks",
                                                        "Location",
                                                         "StatusCode" };
                IMongoQuery query = Query.EQ("UmbrellaLogId",    (BsonValue)UmbrellaLogIdAsObjectId);
                aLogEvent = DBConnection.database.GetCollection<ELLCsLogEvent>("FMS_TM_MST_LogEvents")
                                          .Find(query).SetFields(arrFields).ToList<ELLCsInterfaceLogEvent>().FirstOrDefault();

               IMongoQuery queryForLogOfInterest = Query.EQ("Id", (BsonValue)UmbrellaLogIdAsObjectId);
              ELLCsInterfaceLog aLog = DBConnection.database.GetCollection<ELLCsLog>("FMS_TM_MST_Logs")
                                                    .Find(queryForLogOfInterest).SetFields(arrFields).ToList<ELLCsInterfaceLog>().FirstOrDefault();

?这是我真正迷路的地方,我不知道如何将 LogEvent 的 Id 添加到 IList logEventsIdList?我必须使用 BsonDocument 吗?我们该怎么做?

全部:

这是使单方实体知道多方实体的代码:

          DBConnection.database.GetCollection<Log>("Logs").Update(
                                     Query<Log>.EQ(l => l.Id, UmbrellaLogIdAsObjectId),
                                     Update<Log>.Push(l => l.logEventsIdList, aLogEvent.Id),
                                                         new MongoUpdateOptions
                                                         {
                                                             WriteConcern = WriteConcern.Acknowledged
                                                         });