为什么我对子文档的更新会删除子文档的数组性质

时间:2013-01-28 17:19:21

标签: c# mongodb

我有这个

{
 "ClockID" : "fd51b6e0-5b81-49ab-8424-71fd768281b2",
 "ClockName" : "AAA-TEST123-002",
 "FilesList" : [{
                 "FileName" : "AAA-TEST123-002.mpg",
                 "FileLocationHistory" :    [{
                                               "FullPath" : "192.168.32.166/Ingestion",
                                               "AllowDeleteOnCancel" : false,
                                               "_id" : "565b7343-9dc5-4916-a788-0f392fce2502"
                                    }],
                  "_id" : "15b54499-a0be-4278-82ed-58c82c13bd40",
               }],
 "_id" : ObjectId("510164d0a63cfa2250fd6d19"),
 "_t" : "ClockRecord"

当我使用以下命令更新FileLocationHistory数组文档时:

    public void UpdateClockFilesLocationRecord(string collectionName, ClockFileLocationHistoryRecord clockFileLocationHistory, BsonObjectId clockDocumentID, string clockFileDocumentID)
    {
        var mongoCollection = MongoDatabase.GetCollection<ClockRecord>("Clocks");
        var update = Update.Set("FilesList.$.FileLocationHistory", BsonDocumentWrapper.Create<ClockFileLocationHistoryRecord>(clockFileLocationHistory));
        var modeResult = mongoCollection.Update(
             Query.And(
                Query.EQ("_id", clockDocumentID),
                Query.EQ("FilesList._id", clockFileDocumentID)
                ), 
              update, UpdateFlags.Upsert
              );
    }

更新通过,但文档不再是数组:

{
 "ClockID" : "fd51b6e0-5b81-49ab-8424-71fd768281b2",
 "ClockName" : "AAA-TEST123-002",
 "FilesList" : [{
                 "FileName" : "AAA-TEST123-002.mpg",
                 "FileLocationHistory" :    {
                                               "FullPath" : "192.168.32.166/Ingestion",
                                               "AllowDeleteOnCancel" : false,
                                               "_id" : "565b7343-9dc5-4916-a788-0f392fce2502"
                                    },
                  "_id" : "15b54499-a0be-4278-82ed-58c82c13bd40",
               }],
 "_id" : ObjectId("510164d0a63cfa2250fd6d19"),
 "_t" : "ClockRecord"

这意味着文档会尝试反序列化为驱动程序无法识别的对象。

我的更新代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

而不是:

var update = Update.Set("FilesList.$.FileLocationHistory", BsonDocumentWrapper.Create<ClockFileLocationHistoryRecord>(clockFileLocationHistory));

你可以使用push:

var update = MongoUpdate.Push("FilesList.$.FileLocationHistory", BsonDocumentWrapper.Create<ClockFileLocationHistoryRecord>(clockFileLocationHistory));

...添加到数组