如何在 BsonDocument 中添加字典类型的对象作为 BsonElement

本文关键字:对象 BsonElement 类型 字典 BsonDocument 添加 | 更新日期: 2023-09-27 18:31:54

好吧,我正在尝试执行插入批处理操作。为此,我正在创建一个BsonDocuemt的数组。对于每个 BsonDocument In 数组,我正在添加 BsonElements。

问题类(模型):

public class Question
{
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
        public string QuestionName { get; set; }
        public Dictionary<string, VariableDetails> Rules { get; set; }
        public List<Question> QuestionsList { get; set; }
}
public class VariableDetails
{
        public string variableType { get; set; }
        public string min { get; set; }
        public string max { get; set; }
}

现在我正在尝试形成 BsonDocuemts 数组,如下所示:

public void batchInsert(Question Model)
{
     _collection = _db.GetCollection<Question>("Question");
     BsonDocument[] batch = new BsonDocument[Model.QuestionsList.Count];
     int count = 0;
     foreach (Question question in Model.QuestionsList )
     {
            BsonDocument bsonDoc = new BsonDocument();
            bsonDoc.Add(new BsonElement("QuestionName", question.QuestionName ));
            //Following line is in error
            bsonDoc.Add(new BsonElement("Rules", question.Rules));
            //Argument type 'Systems.Collections.Generic.Dictionary<string, VariableDetails>' is not assignable to parameter type 'MongoDB.Bson.BsonValue'.
            batch[count] = bsonDoc;
            count++;
     }
     _collection.InsertBatch(batch);
}

我能够在 BsonDocument 中添加字符串类型的属性作为 BsonElement,无法使用字典类型的属性这样做。

我希望 db 中的最终插入应该是这样的:

{
    "_id" : ObjectId("54757796bb63bc08b481ad86"),
    "Name" : "Question1",
    "Rules" : {
        "a1" : {
            "variableType" : "1dPI",
            "min" : "1",
            "max" : "9"
        },
        "a2" : {
            "variableType" : "1dPI",
            "min" : "1",
            "max" : "9"
        }
    }
},
{
    "_id" : ObjectId("54757796bb63bc08b481ad86"),
    "Name" : "Question2",
    "Rules" : {
        "d1" : {
            "variableType" : "1dPI",
            "min" : "1",
            "max" : "9"
        },
        "f3" : {
            "variableType" : "1dPI",
            "min" : "1",
            "max" : "9"
        }
    }
}

我的问题是如何在 BsonDocuemt 中添加字典类型的属性作为 BsonElement?

如何在 BsonDocument 中添加字典类型的对象作为 BsonElement

我只需要将嵌套的BsonDocument作为元素添加到父BsonDocument。就是这样!

public void batchInsert(Question Model)
{
 _collection = _db.GetCollection<Question>("Question");
 BsonDocument[] batch = new BsonDocument[Model.QuestionList.Count];
 int count = 0;
 foreach (Question question in Model.QuestionList)
 {
     BsonDocument rulesBsonDoc = new BsonDocument();
     foreach (KeyValuePair<string, VariableDetails> qTemp in question.Rules)
     {
         string variableName = qTemp.Key;
         VariableDetails variableDetails = qTemp.Value;
         string variableType = variableDetails.variableType;
         string min = variableDetails.min;
         string max = variableDetails.max;
         BsonDocument childBsonDoc = new BsonDocument();
         childBsonDoc.Add(new BsonElement("variableType", variableType));
         childBsonDoc.Add(new BsonElement("min", min));
         childBsonDoc.Add(new BsonElement("max", max));    
         rulesBsonDoc.Add(new BsonElement(variableName, childBsonDoc));
      }
      BsonDocument bsonDoc = new BsonDocument();
      bsonDoc.Add(new BsonElement("Name", question.Name));
      bsonDoc.Add(new BsonElement("Rules", rulesBsonDoc));
      batch[count] = bsonDoc;
      count++;
  }
  _collection.InsertBatch(batch);
}