您的位置:首页 > 数据库 > Mongodb

留一个C#版。操作MONgoDB对数组进行增删改查

2014-01-23 16:37 423 查看
[HttpPost]

[ValidateInput(false)]

public ActionResult Add(Title model)

{

List<Item> Item = new List<Item>();

//生成俩选项并且添加

Item item = new Item();

item.cItemID = new ObjectId(Guid.NewGuid().ToString().Replace("-", "").Trim().Substring(0, 23));

item.cItemName = Request["option1"] == null ? "1" : Request["option1"].ToString();

Item item1 = new Item();

item1.cItemID = new ObjectId(Guid.NewGuid().ToString().Replace("-", "").Trim().Substring(0, 23));

item1.cItemName = Request["option2"] == null ? "2" : Request["option2"].ToString();

Item.Add(item1);

Item.Add(item);

//将获取的List添加到实体的List中

model.list = Item;

string errorMsg;

//插入

bool result = _dbHelper.Insert<Title>(model, "Title", out errorMsg);

string str = "/List/Index";

return Success("保存成功!", str);

}

[HttpGet]

public ActionResult Edit(string id = "")

{

IMongoQuery condition = Query.EQ("_id", new ObjectId(id));

Title title = new Title();

title.cTitleId = new ObjectId(id);

//获取实体

title = _dbHelper.FindOne<Title>(condition, "Title");

return View(title);

}

/// <summary>

/// 保存详情页面

/// </summary>

/// <param name="model"></param>

/// <returns></returns>

[HttpPost]

[ValidateInput(false)]

public ActionResult Edit(Title model)

{

//方式一:全删除。在重新插入

//方式二:修改相应的值 利用下下标



//IMongoQuery query = Query.EQ("_id", new ObjectId(Request["Id"].ToString()));


//List<IMongoUpdate> updateList = new List<IMongoUpdate>();

//updateList.Add(Update.Set("cTitleName", model.cTitleName));

////设置list中各项的值。主键规律。list.下标.属性

//for (int i = 0; i < 2; i++)

//{

// string strCondition = "list." + i + ".cItemName";

// string strValue = Request["option" + (i + 1)] == null ? "" : Request["option" + (i + 1)].ToString();

// if (!String.Empty.Equals(strValue))

// {

// updateList.Add(Update.Set(strCondition, strValue));

// }

//}

////更新

//_dbHelper.Update<Title>(query, Update.Combine(updateList.ToArray()), "Title");

//利用通配符$

IMongoQuery query = Query.EQ("list._id", new ObjectId("0b05d84ea89234ff7be99a4d"));

List<IMongoUpdate> updateList = new List<IMongoUpdate>();

string strCondition = "list.$.cItemName";


string strValue = "12334";

updateList.Add(Update.Set(strCondition, strValue));

//更新

_dbHelper.Update<Title>(query, Update.Combine(updateList.ToArray()), "Title");

string str = "/List/Index";

return Success("保存成功!", str);

}

/// <summary>

/// 删除

/// </summary>

/// <returns></returns>

public ActionResult Delete(string id)

{

//删除指定的Model

_dbHelper.Remove<Title>("Title", id);

string str = "/List/Index";

return Success("删除成功!", str);


}

/// <summary>

/// 删除

/// </summary>

/// <returns></returns>

public ActionResult DelItem(string id, string strTitleId)

{

IMongoQuery query = Query.EQ("_id", new ObjectId(id));

//更新此资源的信息。删除list数组中_id位strTitleId的属性

IMongoUpdate updateList = Update.Pull("list", new BsonDocument { { "_id", new ObjectId(strTitleId) } });

_dbHelper.Update<Title>(query, updateList, "Title");

string str = "/List/Edit?id=" + id;

return Success("删除成功!", str);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐