zoukankan      html  css  js  c++  java
  • MongoDB学习笔记~自己封装的Curd操作(查询集合对象属性,更新集合对象)

    回到目录

    我不得不说,mongodb官方驱动在与.net结合上做的不是很好,不是很理想,所以,我决定对它进行了二次封装,这是显得很必然了,每个人都希望使用简单的对象,而对使用复杂,麻烦,容易出错的对象尽而远之,这是正常的,人都是喜欢懒惰的,就像程序员,也是一样,喜欢偷懒,可能说,偷懒是程序员进步的一个标志,呵呵.

    下面我是总结的几种标准的操作,主要是针对我封装的官方驱动而方的(MongoOfficialRepository<TEntity>)

    1  插入对象和子对象

            /// <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 更新对象和子对象集合元素,这是非常不错的功能,对于没有必要更新的记录,可以不去为它赋值

            /// <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 分页,多字段查询和排序,这是项目开发中用的最多的东西了,写了个标准的给大家参考

            /// <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 分组,对于需要按着按些字段进行聚合(统计,求和,总数,最大值,最小值等),及多条件查询,这里有不错的实例

            /// <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仓储进行的一些二次说明,感觉还是比较有必要的,呵呵.

    回到目录

  • 相关阅读:
    C++ UNREFERENCED_PARAMETER函数的作用
    Win32 Console Application、Win32 Application、MFC三者之间的联系和区别
    解决CSDN博客插入代码出现的问题
    C++ std::vector指定位置插入
    计算机如何与人沟通(1)
    C++ fstream文件操作
    using namespace std 和 include 的区别
    找不到windows.h源文件
    C++ 字符串转换
    WPF style 换肤
  • 原文地址:https://www.cnblogs.com/lori/p/4544352.html
Copyright © 2011-2022 走看看