MongoDB - 插入单个工作正常,但数组没有

时间:2014-07-22 13:55:24

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

当我插入我的可序列化类的单个实例时,插入工作正常。当我尝试插入一个Answers数组时,我得到一个例外。当然我不需要将我的答案数组转换为BsonDocuments数组,是吗? (对于单个插入,我不需要这样做!)

/********* 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;

    }

错误:

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 个答案:

答案 0 :(得分:3)

Insert用于将单个文档插入集合。使用InsertBatch插入多个文档:

this.answerCollection.InsertBatch(answers, WriteConcern.Acknowledged);