Use c # to MongoDB query (1)

Use c # to MongoDB query (1)

1.BsonDocument objects

    In the presence of a namespace MongoDB.Bson BsonDocument class, MongoDB document object, representing a section of the solid model data MongoDB irregularities. May be used BsonDocument irregular operation data, the type inherits IEnumberable <> type, that is in turn seen as a set of each entity model, we can obtain the value of the subscript embodiment of the mockup

Copy the code

     // 
        // Summary: 
        // Gets, or sets A value by position. 
        // 
        // Parameters: 
        // index: 
        . At The position // 
        // 
        // return the result: 
        . // at The value 
        public BsonValue the override the this [int index ] {GET; SET;} 
        // 
        // summary: 
        // Gets, or A value sets by name. 
        // 
        // parameters: 
        // name: 
        // name of The. 
        // 
        // return the result: 
        // value of The. 
        the override BsonValue the this public [String name] {GET; SET;}  
        //
        // summary:
        //     Gets the value of an element or a default value if the element is not found.
        //
        // 参数:
        //   name:
        //     The name of the element.
        //
        //   defaultValue:
        //     The default value to return if the element is not found.
        //
        // 返回结果:
        //     Teh value of the element or a default value if the element is not found.
        [Obsolete("Use GetValue(string name, BsonValue defaultValue) instead.")]
        public virtual BsonValue this[string name, BsonValue defaultValue] { get; }

Copy the code

  2. connection configuration

Copy the code

// connection address 
        Private Conn static String = "MongoDB: //192.168.11.51: 40000"; 
        // database name 
        Private dbName static String = "Yan"; 
        // set name 
        Private colName static String = "Demo"; 
        // Connect server 
       static MongoClient Client = new new MongoClient (Conn); 
        // Get the specified database 
       static IMongoDatabase DB = client.GetDatabase (dbName); 
        // Get the specified database set BsonDocument document object 
       static IMongoCollection <BsonDocument> coll = db.GetCollection <BsonDocument> (colName);

Copy the code

3. Insert Data

Copy the code

  var doc = new[]
            {
                new BsonDocument{
                    { "DepartmentName","开发部"},
                    { "People",new  BsonArray
                        {
                            new BsonDocument{ { "Name", "狗娃" },{"Age",20 } },
                             new BsonDocument{ { "Name", "狗剩" },{"Age",22 } },
                              new BsonDocument{ { "Name", "铁蛋" },{"Age",24 } }
                        }
                    },
                    {"Sum",18 },
                      { "dim_cm", new BsonArray { 14,}} 21 is 
                    { "the DepartmentName", "test section"},


                },
                 new BsonDocument{
                    { "People",new  BsonArray
                        {
                            new BsonDocument{ { "Name", "张三" },{"Age",11 } },
                             new BsonDocument{ { "Name", "李四" },{"Age",34 } },
                              new BsonDocument{ { "Name", "王五" },{"Age",33 } }
                        }
                    }
                     ,
                     { "Sum",4 }
                     ,
                       { "dim_cm", new BsonArray { 14, 21 } }

                },
                  new BsonDocument{
                    { "DepartmentName","运维部"},
                    { "People",new  BsonArray
                        {
                            new BsonDocument{ { "Name", "闫" },{"Age",20 } },
                             new BsonDocument{ { "Name", "王" },{"Age",22 } },
                              new BsonDocument{ { "Name", "赵" },{"Age",24 } }
                        }
                    },
                     { "Sum",2 },
                       { "dim_cm", new BsonArray { 22.85, 30 } }

                }
            };

            coll.InsertMany(doc);

Copy the code

4. Query

  4.1 Query sector is the Ministry of Information Development

Copy the code

// create a constraint generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFilter = <BsonDocument> .Filter; 
            // constraint 
            FilterDefinition <BsonDocument> filter = builder.Eq ( "DepartmentName", " Development"); 
            // get the data 
            var result = coll.Find <BsonDocument> (filter) .ToList (); 
            the foreach (var in Item Result) 
            { 
                // remove the entire value 
                Console.WriteLine (item.AsBsonValue); 
            }

Copy the code

    4.2 Sum greater than the data acquisition 4

Copy the code

// create a constraint generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFIlter = <BsonDocument> .Filter; 
            // constraint 
            FilterDefinition <BsonDocument> = builder.Gt filter ( "the Sum",. 4); 
            var = coll.Find Result <BsonDocument> ( filter) .ToList (); 
            the foreach (var in Item Result) 
            { 
                // remove the entire value 
                Console.WriteLine (item.AsBsonValue); 
            }

Copy the code

   4.3 And Constraints  

Copy the code

// create a constraint generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFilter = <BsonDocument> .Filter; 
            // constraint 
            FilterDefinition <BsonDocument> filter = builder.And ( builder.Gt ( "Sum", "2"), builder.Eq ( "DepartmentName", "O & M")); 
            var = coll.Find Result <BsonDocument> (filter) .ToList (); 
            the foreach (var in Item Result) 
            { 
                // remove the entire value 
                Console.WriteLine (item.AsBsonValue) ; 
            }

Copy the code

  4.4 query specified value

Copy the code

  //创建约束生成器
            FilterDefinitionBuilder<BsonDocument> builderFilter = Builders<BsonDocument>.Filter;

            ProjectionDefinitionBuilder<BsonDocument> builderProjection = Builders<BsonDocument>.Projection;
            //Include 包含某元素    Exclude  不包含某元素
            ProjectionDefinition<BsonDocument> projection = builderProjection.Include("DepartmentName").Exclude("_id");
            var result = coll.Find<BsonDocument>(builderFilter.Empty).Project(projection).ToList();
            foreach (var item in result)
            {
                //取出整条值
                Console.WriteLine(item.AsBsonValue);
            }

Copy the code

  4.5 Sorting

Copy the code

// Create generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFilter = <BsonDocument> .Filter; 
            // sequencing producer 
            SortDefinitionBuilder <BsonDocument> Builders builderSort = <BsonDocument> .Sort; 
            // Sort Ascending positive sequence restraint Descending descending 
            SortDefinition <BsonDocument> sort builderSort.Ascending = ( "the Sum"); 
            var = coll.Find Result <BsonDocument> (builderFilter.Empty) .Sort (Sort) .ToList (); 
            the foreach (var in Item Result) 
            { 
                // remove the entire value 
                Console. the WriteLine (item.AsBsonValue); 
            }

Copy the code

  4.6 In the query

Copy the code

  // Create generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFilter = <BsonDocument> .Filter; 
            FilterDefinition <BsonDocument> = builderFilter.In filter ( "the DepartmentName" new new [] { "test section", "Development"}); 
            var Result coll.Find = <BsonDocument> (filter) .ToList (); 
            the foreach (var in Item Result) 
            { 
                // remove the entire value 
                Console.WriteLine (item.AsBsonValue); 
            }

Copy the code

   4.7 paging query

Copy the code

// Create generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFilter = <BsonDocument> .Filter; 
            // Query tab Skip Skip Limit number 
            var result = coll.Find <BsonDocument> ( builderFilter.Empty) .Skip (1) .Limit (1 ) .ToList (); 
            the foreach (var in Item Result) 
            { 
                // remove the entire value 
                Console.WriteLine (item.AsBsonValue); 
            }

Copy the code

  4.8 The total number of entries in the query

// Create generator 
            FilterDefinitionBuilder <BsonDocument> Builders builderFilter = <BsonDocument> .Filter; 
            // total number of entries 
            var result = coll.Find <BsonDocument> ( builderFilter.Empty) .Count ();

  4.9Linq inquiry

Copy the code

 //创建生成器//linq
            var result = from y in coll.AsQueryable() select new { DepartmentName = y["DepartmentName"], id = y["_id"] };
            foreach (var item in result)
            {

                Console.WriteLine("DepartmentName:" + item.DepartmentName + "====Id:"+item.id);
            }

Copy the code

   4.10 grouping queries

Copy the code

 //创建生成器
            FilterDefinitionBuilder<BsonDocument> builderFilter = Builders<BsonDocument>.Filter;
            //分组
            var result = from y in coll.AsQueryable()  group y by  y["DepartmentName"] into s select new { DepartmentName = s.Key, Count = s.Count() };
            foreach (var item in result)
            {

                Console.WriteLine("DepartmentName:" + item.DepartmentName + "====Count:"+item.Count);
            }

Copy the code

  4.11连表查询

Copy the code

 //linq
            //连表查询   在这里是自己连自己
            var result = from u in coll.AsQueryable() join o in coll.AsQueryable() on u["_id"] equals o["_id"] select new { DepartmentName1 = u["DepartmentName"], DepartmentName2 = u["DepartmentName"] };
            foreach (var item in result)
            {

                Console.WriteLine("DepartmentName1:" + item.DepartmentName1 + "====DepartmentName2:" + item.DepartmentName2);
            }

Copy the code

 

Category:  MongoDB

Guess you like

Origin blog.csdn.net/cxu123321/article/details/93336121