mongodb 非对象方式更新、非对象方式删除、事务、插入或者更新怎么使用? 返回

SqlSugar 待处理
149
该叫什么 malong 发布于2周前
悬赏:5 飞吻

mongodb 非对象方式更新、非对象方式删除、事务、插入或者更新怎么使用?

前端参数:{
  "CollectionName": "Student",
  "id": "6a1705da9a3bef5718356b31",
  "Data": "{\"_id\":\"6a1705da9a3bef5718356b31\",\"name\":\"张三\",\"age\":20}"

后端代码:public bool UpdateForm(DynamicUpdateDto dto)
{
    bool result = false;
    using (var db = GetMongoDb("MongoDB"))
    {
        BsonDocument doc = BsonDocument.Parse(dto.Data);
        var dict = doc.ToDictionary();
        result = db.UpdateableByDynamic(dict).AS(dto.CollectionName)
            .Where("_id = @Id", new { Id = ObjectId.Parse(dto.id) })
            .ExecuteCommand() > 0;
    }
    return result;
}    

DynamicUpdateDto 实体参数:public class DynamicUpdateDto
    {
        public string CollectionName { get; set; } = null!;
        public string Data { get; set; }
        public string id { get; set; } = null!;
    } 

报错提示:"A write operation resulted in an error. WriteError: { Category : \"Uncategorized\", Code : 66, Message : \"After applying the update, the (immutable) field '_id' was found to have been altered to _id: ObjectId('6a1705da9a3bef5718356b31')\" }. 虽然报错了但是数据库里面的数据更新成功了。如果不带 _id 则所有文档数据都更新,带上_id只会更新对应的一条数据

热忱回答0