首页 新闻 会员 周边

c#操作mongodb部分更新实体字段问题,求助高手!!!

0
悬赏园豆:20 [已解决问题] 解决于 2018-09-20 18:07

实体描述如下:

public class BaseModel
{
public string Id;
}

public class PersonType : BaseModel
{
public string Code { get; set; }
public string Display { get; set; }
}
public class Person : BaseModel
{
//如果对应多个分类,则格式为:,3,34,2
public string PersonType { get; set; }
public string Name { get; set; }
public bool Sex { get; set; }
public int Age { get; set; }
public List<Address> Addresses { get; set; }
public List<string> Courses { get; set; }
}

public class Address
{
public string Province { get; set; }
public string City { get; set; }
}

添加数据方法如下:webapi调用添加

public string Post([FromBody]object value)
{
var model = JsonConvert.DeserializeObject<Person>(value.ToString());
model.Id = ObjectId.GenerateNewId().ToString();
try
{
col.Insert(model);
return model.Id;
}
catch (WebException ex)
{
throw ex;
}

}

添加成功后数据格式如下:

{
"_id" : "544df5930d661514a427bed2",
"PersonType" : "1001",
"Name" : "j4ack",
"Sex" : true,
"Age" : 422,
"Addresses" : [
{
"Province" : "henan",
"City" : "zz"
},
{
"Province" : "henan",
"City" : "kf"
}
],
"Courses" : [
"shuxue",
"yingyu"
]
}

修改数据方法如下:

public int Put(string id, [FromBody]object value)
{
try
{
var query = new QueryDocument { { "_id", id } };
var dicData = JsonConvert.DeserializeObject<Dictionary<string, object>>(value.ToString());
var update = new UpdateDocument { { "$set", new QueryDocument(dicData) } };
col.Update(query, update);
return 1;
}
catch (WebException ex)
{

throw ex;
}

}

 

问题如下:

只能修改单个属性,对于嵌套数组和嵌套实体集合无法修改,而且对于整形修改,例如修改内容体:

{
Name:"adga",
"Sex" : false,
Age:99

}

则修改后Age变为NumberLong(99),在取数据时,转换失败,如何让修改后的数据为99?

梦亦晓的主页 梦亦晓 | 初学一级 | 园豆:112
提问于:2014-10-28 17:53
< >
分享
最佳答案
0

已解决

梦亦晓 | 初学一级 |园豆:112 | 2014-11-12 14:59
其他回答(1)
-1

这是我做的实例:

public bool UpdateUserInfo(UserModel userModel)
        {
            bool isTag = false;

            var userCollection = mongoDatabase.GetCollection<UserModel>(userModel.GetType().Name);

            var query = userCollection.AsQueryable().FirstOrDefault(t => t.UserId == userModel.UserId);
            if (query != null)
            {
                query.Product = userModel.Product;
                query.UserAge = userModel.UserAge;

                //对已经存在的用户信息进行修改。
                WriteConcernResult writeConcernResult = userCollection.Save(query);

                isTag = writeConcernResult == null;
                #region  非linq修改方法
                //                var list = new List<UpdateBuilder>
                //                {
                //                    Update.Set("", ""), 
                //                    Update.Set("", ""),
                //                    Update.Inc("LoginCount", 1)
                //                };
                //                IMongoUpdate mongoUpdate = Update.Combine(list);
                //
                //                writeConcernResult = userCollection.Update(Query.EQ(PropertyManagement.GetPropertyName<UserModel, int>(t => t.UserId), userModel.UserId), mongoUpdate);
                #endregion
            }

            return isTag;
        }

有两个方法修改。你可以参考一下。

收获园豆:20
悟行 | 园豆:12559 (专家六级) | 2014-10-28 20:13

这两个方法都不能满足我的需求。

我的mongodb是在webapi基础上实现的,所以方法public int Put(string id, [FromBody]object value){}中的value的格式为Dictionary<string, object>。而需要传递给value的格式为json格式。

我的需求是(根据id):

第一:部分字段更新(不能先查后改)

第二:要更新的字段不确定,例如:过来的数据格式为{Name:"jack"}则将Name修改为jack。过来的数据格式为{Name:"jack",Age:22}则将Name修改为jack,Age修改为22。

现在的问题是:

只能修改单个属性,对于嵌套数组和嵌套实体集合无法修改,而且对于整形修改,例如修改内容体:

{
Name:"adga",
"Sex" : false,
Age:99

}

则修改后Age变为NumberLong(99),在取数据时,转换失败,如何让修改后的数据为99?

 

支持(0) 反对(0) 梦亦晓 | 园豆:112 (初学一级) | 2014-10-29 08:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册