无法将$ push / $ pushAll修饰符应用于非数组

时间:2013-01-14 14:24:14

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

我在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;

Fiddler PUT:

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

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

{"Detail" : "This is great"}

1 个答案:

答案 0 :(得分:2)

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