gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:50 26 4
gpt4 key购买 nike

好吧,我正在尝试执行插入批处理操作。为此,我正在创建一个 BsonDocuemt 数组。对于数组中的每个 BsonDocument,我都添加了 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,而不能使用字典类型的属性。

我希望数据库中的最终插入应该是这样的:

{
"_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?

最佳答案

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

关于c# - 如何在 BsonDocument 中添加字典类型的对象作为 BsonElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27120124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com