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;

提琴手放置:

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

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

{"Detail" : "This is great"}
4

1 回答 1

2

我想这是因为当我插入一个新文档时,我没有将 Comments 设置为空数组。

于 2013-01-15T13:17:21.407 回答