1. 程式人生 > >留一個C#版。操作MONgoDB對陣列進行增刪改查

留一個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);
        }



相關推薦

一個C#操作MONgoDB陣列進行刪改

        [HttpPost]         [ValidateInput(false)]         public ActionResult Add(Title model)         {             List<Item> Ite

C#中sqlserver進行刪改操作(簡單易懂)

uid 數據連接 net cti ade 密碼 logs where Go 1、添加引用using System.Data;using System.Data.SqlData;2、建立於數據庫的連接,建議將它做成一個方法,方便多次利用。string sqlconnectio

c#操作MongoDB驗證 全解析 -刪改有空自己玩,

1.7mongo之前與之後C#可用驅動:使用vs2012以上版本的Nuget獲取官方原版MongoDB.Driver(官方git庫下載程式集也會缺少程式集,大可一試) 安裝沒有坑。。。隨便操作>>>>看本文之前應當對Mongodb有一丟丟了解 (mon

python 連線oracle資料庫進行刪改操作

Python 建立連線oracle資料庫的三種方式: 方式一:使用者名稱、密碼和監聽分開寫 import cx_Oracle db=cx_Oracle.connect('username/[email protected]/orcl') db.close()

Solr13 - 通過SolrCloud的RESTful API叢集進行刪改操作

文章目錄 1 建立操作 1.1 建立collection 1.2 建立core 1.3 建立API引數說明 2 刪除操作 2.1 刪除collection 2.2 解除安裝core 2.3 刪

ASP.NET 使用類資料庫進行刪改操作

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Dat

redis叢集環境搭建以及在java裡面叢集進行刪改操作

轉載自:http://baijiahao.baidu.com/s?id=1588544341614288867&wfr=spider&for=pc 要讓叢集正常工作至少需要3個主節點,所以我們要設定3個主節點,3個從節點,共6個叢集節點。 安裝redis wget http

Java操作Hbase進行建表、刪表以及資料進行刪改,條件查詢

1、搭建環境   新建JAVA專案,新增的包有:    有關Hadoop的hadoop-core-0.20.204.0.jar    有關Hbase的hbase-0.90.4.jar、hbase-0.90.4-tests.jar以及Hbase資源包中li

C#中sqlserver進行刪改

1. 新增引用 using System.Data; using System.Data.SqlData; 建立於資料庫的連線。 string sqlconnection = "data source = "註解1";database = 註解2;

hbase進行刪改操作例項

程式碼例項 定義連線需要的靜態常量 private static final String TABLE_NAME = “Student”; private static final String[] FAMILYS = { “name”, “profil

jdbcmysql進行刪改操作(Statement)

創立資料庫school 創立表格students create table students( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(20) NOT NULL, sex VARCHAR(4) NOT N

Django 操作Mysql資料庫 進行刪改

首先說明:以下方法都是根據表已有的欄位新增資料,若想新增欄位,則需在類裡面新建靜態欄位, 可參考Django 操作資料庫建表  ,增刪改查在views.py 操作, 建表和欄位在models.py 操作 1、首先在 models.py 裡定義 任意 1個 模型類 本人定義 Asset,可自動生成對應的表 f

操作DB~~資料的刪改!!sql語句

對資料進行增刪改查: 這裡著重掌握查詢的各種條件 1)插入資料(增加資料):將元組插入到指定的表中。 //插入單條記錄 insert into 表名(欄位名1,欄位名2,....)val

安卓端通過httpMysql進行刪改

name 交流 shm != 用戶 習慣 cti -perm 得到 各類it學習視頻,大家都可以看看哦!我自己本人都是通過這些來學習it只知識的! 下面是視頻鏈接轉自:http://www.cnblogs.com/wangying222/p/5492935.html ht

asp.net core webapi 使用ef mysql進行刪改,並生成Docker鏡像構建容器運行

rri put void userdata pro ext 代理 cte 成功 1.構建運行mysql容器,添加數據庫user 參考Docker創建運行多個mysql容器,地址 http://www.cnblogs.com/heyangyi/p/9288402.html

docker基礎:私庫系列:再探Harbor:(7) 使用restapi專案進行刪改

在上篇文章中介紹了Harbor的RestApi,這篇具體以專案的增刪改查作為例子來進行說明。 前提假定 假定Harbor運行於本機localhost的32031埠 專案查詢 查詢全部專案 curl -X GET “http://localhost:32031/

Zookeeper JavaAPI節點進行刪改(詳細步驟)

import java.io.IOException; import java.util.List; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.ap

ORACLE中建立表、進行刪改的語法

最近在學習ORACLE,現將在ORACLE中建立表、對錶進行增刪該查的語法總結如下: 表是一種資料庫物件,是基本的資料儲存單位,由行和列組成 表的建立(以課程資訊表為例): CREATE TABLE OBJECTS       

mybatis 在前端資料庫進行刪改

Springboot 對資料庫進行增刪改查 首先要實現在idea裡面能實現對資料看的增刪改查,最後再實現前後端互動 1.首先在資料庫裡面先建好資料庫(test01)和表(t_user) 2.配置檔案pom.xml <?xml version="1.0"

Python--綜合練習--檔案進行刪改

知識點應用:strip()  tag       函式封裝      檔案操作      for迴圈      os模組      程式碼解耦 例項 tag = True while tag: print('level') choice = input('level1