MongoDB的简单操作(asp.net)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using MongoDB.Driver;
  6 using MongoDB.Bson;
  7 using MongoDB.Driver.Builders;
  8 
  9 namespace web
 10 {
 11     public class MongoHelper
 12     {
 13         /// <summary>
 14         /// 数据库连接
 15         /// </summary>
 16         private static string conn = "mongodb://127.0.0.1:27017";
 17         /// <summary>
 18         /// 指定的数据库
 19         /// </summary>
 20         private static string dbName = "mydb";
 21         /// <summary>
 22         /// 指定的表
 23         /// </summary>
 24         private static string tbName = "test";
 25 
 26         /// <summary>
 27         /// 添加
 28         /// </summary>
 29         /// <param name="text">内容</param>
 30         /// <param name="articleId">文章ID</param>
 31         /// <param name="channelId">频道ID</param>
 32         /// <returns></returns>
 33         public static void Add(User t)
 34         {
 35             //创建数据连接
 36             MongoServer server = MongoServer.Create(conn);
 37             //获取指定数据库
 38             MongoDatabase db = server.GetDatabase(dbName);
 39             //获取表
 40             MongoCollection col = db.GetCollection(tbName);
 41             //插入
 42             col.Insert(t);
 43 
 44         }
 45 
 46         /// <summary>
 47         /// 根据ObjectID 删除
 48         /// </summary>
 49         /// <param name="objId"></param>
 50         public static void Delete(int objId)
 51         {
 52             //创建数据连接
 53             MongoServer server = MongoServer.Create(conn);
 54             //获取指定数据库
 55             MongoDatabase db = server.GetDatabase(dbName);
 56             //获取表
 57             MongoCollection<User> col = db.GetCollection<User>(tbName);
 58 
 59             IMongoQuery query = Query.EQ("_id", objId);
 60 
 61             col.Remove(query);
 62         }
 63 
 64         /// <summary>
 65         /// 根据ObjectID 修改
 66         /// </summary>
 67         public static void Update(User t)
 68         {
 69             //创建数据连接
 70             MongoServer server = MongoServer.Create(conn);
 71             //获取指定数据库
 72             MongoDatabase db = server.GetDatabase(dbName);
 73             //获取表
 74             MongoCollection<User> col = db.GetCollection<User>(tbName);
 75 
 76             BsonDocument bd = BsonExtensionMethods.ToBsonDocument(t);
 77 
 78             IMongoQuery query = Query.EQ("_id", t.id);
 79 
 80             col.Update(query, new UpdateDocument(bd));
 81 
 82         }
 83 
 84         //<summary>
 85         //根据ObjectID 查询
 86         //</summary>
 87         public static User SelectOne(int objId)
 88         {
 89             //创建数据连接
 90             MongoServer server = MongoServer.Create(conn);
 91             //获取指定数据库
 92             MongoDatabase db = server.GetDatabase(dbName);
 93             //获取表
 94             MongoCollection<User> col = db.GetCollection<User>(tbName);
 95             //条件查询           
 96             return col.FindOne(Query.EQ("_id", objId));
 97         }
 98 
 99         //<summary>
100         //查询所有
101         //</summary>
102         public static List<User> SelectAll()
103         {
104             List<User> list = new List<User>();
105 
106             //创建数据连接
107             MongoServer server = MongoServer.Create(conn);
108             //获取指定数据库
109             MongoDatabase db = server.GetDatabase(dbName);
110             //获取表
111             MongoCollection<User> col = db.GetCollection<User>(tbName);
112             //查询全部
113             list.AddRange(col.FindAll());
114             return list;
115 
116         }
117 
118     }
119 }

 

posted @ 2016-04-07 09:49  梦锁湘江  阅读(209)  评论(0编辑  收藏  举报