2013-01-14 20 views
0

我在SafeModeResult result = _test.Update(query, update);得到這個錯誤:

ExceptionMessage=Safemode detected an error 'Cannot apply $push/$pushAll modifier to non-array'. (Response was { "err" : "Cannot apply $push/$pushAll modifier to non-array", "code" : 10141, "n" : 0, "lastOp" : { "$timestamp" : NumberLong(0) }, "connectionId" : 513625, "ok" : 1.0 }). 

型號:

public class Test 
     { 
      [BsonId] 
      public string Id { get; set; } 
      public string Name { get; set; } 
      public string Address { get; set; } 
      public IList<Comment> Comments { get; set; } 
      public int TotalComments { get; set; } 
      public DateTime LastModified { get; set; } 
     } 

     public class Comment 
     { 
      [BsonId] 
      public string Id { get; set; } 
      public string Detail { get; set; } 
      public DateTime LastModified { get; set; } 
     } 

更新代碼:

IMongoQuery query = Query.EQ("_id", id); 
comment.Id = ObjectId.GenerateNewId().ToString(); 
comment.LastModified = DateTime.UtcNow; 
IMongoUpdate update = Update 
    .PushWrapped("Comments", comment) 
    .Inc("TotalComments", 1); 
SafeModeResult result = _test.Update(query, update); 

     return result.UpdatedExisting; 

提琴手PUT :

http://localhost:49432/api/test/50f3f8365029d61bbc307f52 

User-Agent: Fiddler 
Content-Length: 37 
Content-Type: application/json 
Host: localhost:49432 

{"Detail" : "This is great"} 

回答

2

我想這是因爲當我插入一個新的文檔時,我沒有將註釋設置爲空數組。

+0

我也浪費了幾個小時。感謝您的回覆:) –