1

When I insert a single instance of my serializable class Answer, insert works fine. When I try to insert an array of Answers, I get an exception. Surely I don't need to cast my array of Answers to an array of BsonDocuments, do I? (I didn't need to do so for a single insert!)

/********* DOES NOT WORK - THROWS EXCEPTION *****************/

public bool addAnswers(AnswerDataModel[] answers)
    {
        bool returnval = true;

        try
        {
            this.answerCollection.Insert(answers, WriteConcern.Acknowledged);
        }
        catch (Exception ex)
        {
            LoggerModel.Log(String.Empty, DateTime.UtcNow, (int)LogSeverity.Severity.Critical, ex.Message + Environment.NewLine + ex.StackTrace);
            returnval = false;
        }

        return returnval;

    }

    /******** WORKS ********/
    public bool addAnswer(AnswerDataModel answer)
    {
        bool returnval = true;

        try
        {
            this.answerCollection.Insert(answer, WriteConcern.Acknowledged);
        }
        catch (Exception ex)
        {
            LoggerModel.Log(String.Empty, DateTime.UtcNow, (int)LogSeverity.Severity.Critical, ex.Message + Environment.NewLine + ex.StackTrace);
            returnval = false;
        }

        return returnval;

    }

And error:

Exception details:
Serializer ArraySerializer<AnswerDataModel> expected serialization options of type ArraySerializationOptions, not DocumentSerializationOptions.
  at MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.EnsureSerializationOptions[TSerializationOptions](IBsonSerializationOptions options) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Bson\Serialization\Serializers\BsonBaseSerializer.cs:line 147
  at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`1.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Bson\Serialization\Serializers\EnumerableSerializerBase.cs:line 369
  at MongoDB.Driver.Internal.MongoInsertMessage.AddRequest(BsonBuffer buffer, InsertRequest request) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\Communication\Messages\MongoInsertMessage.cs:line 149
  at MongoDB.Driver.Internal.MongoInsertMessage.WriteBodyTo(BsonBuffer buffer) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\Communication\Messages\MongoInsertMessage.cs:line 91
  at MongoDB.Driver.Internal.MongoRequestMessage.WriteTo(BsonBuffer buffer) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\Communication\Messages\MongoRequestMessage.cs:line 66
  at MongoDB.Driver.Operations.InsertOpcodeOperation.Execute(MongoConnection connection) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\Operations\InsertOpcodeOperation.cs:line 83
  at MongoDB.Driver.MongoCollection.InsertBatch(Type nominalType, IEnumerable documents, MongoInsertOptions options) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\MongoCollection.cs:line 1545
  at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document, MongoInsertOptions options) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\MongoCollection.cs:line 1396
  at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document, WriteConcern writeConcern) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\MongoCollection.cs:line 1410
  at MongoDB.Driver.MongoCollection.Insert[TNominalType](TNominalType document, WriteConcern writeConcern) in d:\jenkins\workspace\mongo-csharp-driver-1.x-build\MongoDB.Driver\MongoCollection.cs:line 1368
1
  • What Language is this? Edit: NVM saw in errors it is c# Commented Jul 22, 2014 at 14:01

1 Answer 1

3

Insert is for inserting a single document to a collection. Use InsertBatch to insert multiple docs:

this.answerCollection.InsertBatch(answers, WriteConcern.Acknowledged);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Why does this not break at compile time?
@goldfinger Because Insert is a generic that can be called using any type, not just ones that work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.