当前位置:Gxlcms > 数据库问题 > 在C#中使用官方驱动操作MongoDB

在C#中使用官方驱动操作MongoDB

时间:2021-07-01 10:21:17 帮助过:11人阅读

  • * sql : SELECT * FROM table WHERE ConfigID > 5 AND ObjID = 1            
  • *---------------------------------------------              
  • */              
  • QueryDocument query = new QueryDocument();              
  • BsonDocument b = new BsonDocument();              
  • b.Add("$gt", 5);                         
  • query.Add("ConfigID", b);  
  • query.Add("ObjID", 1);  
  • MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);  
  •   
  • /*---------------------------------------------             
  • * sql : SELECT * FROM table WHERE ConfigID > 5 AND ConfigID < 10            
  • *---------------------------------------------              
  • */              
  • QueryDocument query = new QueryDocument();              
  • BsonDocument b = new BsonDocument();              
  • b.Add("$gt", 5);     
  • b.Add("$lt", 10);                   
  • query.Add("ConfigID", b);  
  • MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);  
  •  

     

     

     1     public class News
     2     {
     3         public int _id { get; set; }
     4         public int count { get; set; }
     5         public string news { get; set; }
     6         public DateTime time { get; set; }
     7     }
     8 
     9 MongoCursor<BsonDocument> allDoc = coll.FindAllAs<BsonDocument>();
    10 BsonDocument doc = allDoc.First(); //BsonDocument类型参数
    11 
    12 MongoCursor<News> allNews = coll.FindAllAs<News>();
    13 News aNew = allNews.First(); //News类型参数
    14 
    15 News firstNews = coll.FindOneAs<News>(); //查找第一个文档
    16 
    17 QueryDocument query = new QueryDocument(); //定义查询文档
    18 query.Add("_id", 10001);
    19 query.Add("count", 1);
    20 MongoCursor<News> qNews = coll.FindAs<News>(query);
    21 
    22 
    23 BsonDocument bd = new BsonDocument();//定义查询文档 count>2 and count<=4
    24 bd.Add("$gt", 2);
    25 bd.Add("$lte", 4);
    26 QueryDocument query_a = new QueryDocument();
    27 query_a.Add("count",bd);
    28 
    29 FieldsDocument fd = new FieldsDocument();
    30 fd.Add("_id", 0);
    31 fd.Add("count", 1);
    32 fd.Add("time", 1);
    33 
    34 MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);//只返回count和time
    35 
    36 var time = BsonDateTime.Create("2011/9/5 23:26:00");
    37 BsonDocument db_t = new BsonDocument();
    38 db_t.Add("$gt", time);
    39 QueryDocument qd_3 = new QueryDocument();
    40 qd_3.Add("time", db_t);
    41 
    42 MongoCursor<News> mNews = coll.FindAs<News>(qd_3);//

     

     

    http://blog.csdn.net/lansetiankong12/article/details/51767630

    在C#中使用官方驱动操作MongoDB

    标签:没有   datetime   dal   public   参数   结果   copy   desc   keyword   

    人气教程排行