简体   繁体   English

将BSON文档插入收藏夹(C#)

[英]Inserting BSON Document into Collection (C#)

I am trying to insert a BSON document into a Mongo collection. 我正在尝试将BSON文档插入Mongo集合中。

MongoDB.Driver.MongoCommandException: 'Command insert failed: error parsing element 0 of field documents :: caused by :: wrong type for '0' field, expected object, found 0: [ { Constituency: "Aberdeen North", Election: "2010 General Election", Electorate: 64808, Majority: 8361, ResultOfElection: "Lab Hold", Turnout: 37701, TurnoutPercentage: 100.0 } ].' MongoDB.Driver.MongoCommandException:'命令插入失败:解析字段文档::的元素0时出错,由::导致'0'字段的类型错误,预期对象,找到0:[{选区:“北阿伯丁”,选举:“ 2010年大选”,选举人:64808,多数:8361,ResultOfElection:“实验室保留”,投票率:37701,投票率:100.0}]。

Below is my code. 下面是我的代码。

    public void retrieveElectionResuts(string url)
    {
        var client = new RestClient(url);
        var request = new RestRequest(Method.GET);
        request.AddHeader("Accept", "application/json");

        IRestResponse response = client.Execute(request);
        var con = response.Content;

        //Cleaning JSON format
        RootObject result = JsonConvert.DeserializeObject<RootObject>(con);
        List<Item> items = result.result.items;
        List<ConstituencyResult> electionResults = new List<ConstituencyResult>();
        MongoContext context = new MongoContext();
        var db = context.connect();
        var collection = db.GetCollection<BsonArray>("constituencyResult");
        foreach (Item item in items)
        {
            ConstituencyResult constituencyResult = new ConstituencyResult
            {
                Constituency = item.constituency.label._value.ToString(),
                Election = item.election.label._value.ToString(),
                Electorate = item.electorate,
                Majority = item.majority,
                ResultOfElection = item.resultOfElection,
                Turnout = item.turnout,
                TurnoutPercentage = ((item.electorate) / (item.turnout)) * 100               
            };


            BsonArray array = new BsonArray();
            array.Add(constituencyResult.ToBsonDocument());
            collection.InsertOne(array);


        }
    }

I have tried adding the document directly (ie, not putting it in a BSON Array), but this still throws errors. 我尝试过直接添加文档(即,不将其放入BSON数组中),但这仍然会引发错误。

You can not add BsonArray to a collection, because MongoDB document is a set of keys with associated values. 您不能将BsonArray添加到集合中,因为MongoDB文档是一组具有关联值的键。 When you add array, what should be the key? 添加数组时,关键是什么?

You could of course have a field that holds an array. 您当然可以有一个包含数组的字段。 It could be added in the following way: 可以通过以下方式添加:

var collection = database.GetCollection<Object>("constituencyResult");
collection.InsertOne(new
{
    SomeArray = array
});

In this case document will have SomeArray field that will hold array data. 在这种情况下,文档将具有SomeArray字段来保存数组数据。

I have tried adding the document directly (ie, not putting it in a BSON Array), but this still throws errors. 我尝试过直接添加文档(即,不将其放入BSON数组中),但这仍然会引发错误。

If proposed answer is not what you need and you get exceptions when adding ConstituencyResult objects to the collection, please update your question with definition of ConstituencyResult and exception that you get. 如果建议的答案不是您所需要的,并且在将ConstituencyResult对象添加到集合中时出现异常,请使用ConstituencyResult定义和所获取的异常来更新您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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