0

我有这个

{
 "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"

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

我的更新代码有什么问题?

4

1 回答 1

0

代替:

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

您可以使用推送:

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

... 添加到数组中

于 2013-01-28T17:40:11.903 回答