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

时间:2014-11-25 06:28:30

标签: c# mongodb dictionary mongodb-.net-driver

我正在尝试执行插入批处理操作。为此,我正在创建一个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中添加string类型的属性作为BsonElement,但不能使用Dictionary类型的属性。

我希望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中添加Dictionary类型的属性作为BsonElement?

1 个答案:

答案 0 :(得分:4)

我只需要将嵌套的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);
}