MongoDB學習筆記~本身封裝的Curd操做(查詢集合對象屬性,更新集合對象)

回到目錄html

我不得不說,mongodb官方驅動在與.net結合上作的不是很好,不是很理想,因此,我決定對它進行了二次封裝,這是顯得很必然了,每一個人都但願使用簡單的對象,而對使用複雜,麻煩,容易出錯的對象盡而遠之,這是正常的,人都是喜歡懶惰的,就像程序員,也是同樣,喜歡偷懶,可能說,偷懶是程序員進步的一個標誌,呵呵.程序員

下面我是總結的幾種標準的操做,主要是針對我封裝的官方驅動而方的(MongoOfficialRepository<TEntity>)mongodb

1  插入對象和子對象lua

        /// <summary>
        /// 添加對象
        /// </summary>
        static public void Insert()
        {
            List<Person> list = new List<Person>();
            for (int i = 0; i < 10; i++)
            {
                //添加新對象

                list.Add(new Person
                {
                    Address = new Address
                    {
                        City = "北京",
                        District = "鸞翔鳳集",
                        Province = "luanxian",
                    },
                    AddList = new List<Address>
                {
                 new Address
                 {
                    Seconds=1,
                    City = "湖北",
                    District = "鸞翔鳳集",
                    Province = "luanxian",
                 },
                  new Address
                 {
                    Seconds=1,
                    City = "湖南",
                    District = "小區",
                    Province = "luanxian",
                 }
                },
                    Age = 35,
                    Birthday = DateTime.Now,
                    LastContact = DateTime.Now,
                    Name = "wangwu"
                });
            }
            repository1.Insert(list);
        }

2 更新對象和子對象集合元素,這是很是不錯的功能,對於沒有必要更新的記錄,能夠不去爲它賦值spa

        /// <summary>
        /// 集合查詢
        /// </summary>
        static public void Update()
        {
            repository1.Update<Person>(i => new Person
              {
                  Id = "556bfd1b2683c82060c2edd0",
                  AddList = new List<Address>
                  {
                    new Address
                    {
                      Id = "556bfd1b2683c82060c2edd3",
                      City = "佔佔大師123",
                      District = "鸞翔鳳集",
                      Seconds=2
                    }
                 }
              });
        }

3 分頁,多字段查詢和排序,這是項目開發中用的最多的東西了,寫了個標準的給你們參考.net

        /// <summary>
        /// 分頁,排序,查詢
        /// </summary>
        static public void Select()
        {
            //排序和檢索
            var m1 = repository1.GetModel(new
            {
                Address = new
                {
                    City = "北京"
                },
                AddList = new
                {
                    Seconds = 1
                }
            }, new { Name = OrderType.Desc }, 1, 20);

4 分組,對於須要按着按些字段進行聚合(統計,求和,總數,最大值,最小值等),及多條件查詢,這裏有不錯的實例code

        /// <summary>
        /// 分組
        /// </summary>
        static public PagedList<Person> Group(string keyword, int? age, int page)
        {
            Specification<Person> spec = new TrueSpecification<Person>();

            //過濾
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                spec &= new DirectSpecification<Person>(i => i.Name == keyword);
            }

            if (age.HasValue)
            {
                spec &= new DirectSpecification<Person>(i => i.Age == age);
            }

            //分組
            var linq = from data1 in repository1.GetModel().Where(spec.SatisfiedBy())
                       group data1 by new
                       {
                           data1.Id,
                           data1.Name
                       } into g
                       select new Person
                       {
                           Id = g.Key.Id,
                           Name = g.Key.Name,
                           Total = new Total
                           {
                               Count = g.Count(),
                               Max = g.Max(i => i.Age),
                           }

                       };
            return new PagedList<Person>(linq, page, 10);
        }

OK,以上是針對個人MongoDB倉儲進行的一些二次說明,感受仍是比較有必要的,呵呵.htm

回到目錄對象

相關文章
相關標籤/搜索