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

        [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);
        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值