我在以下位置收到此错误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"}