简体   繁体   中英

Inserting BSON Document into Collection (C#)

I am trying to insert a BSON document into a Mongo collection.

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 } ].'

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.

You can not add BsonArray to a collection, because MongoDB document is a set of keys with associated values. 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.

I have tried adding the document directly (ie, not putting it in a BSON Array), but this still throws errors.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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