|
public class MongoDBHelper
{
//定义Mongo服务
private MongoServer mongo = null;
//获取databaseName对应的数据库,不存在则自动创建
private MongoDatabase mongoDatabase;
///
/// Mongo 数据库连接
///
public MongoDBHelper()
{
mongo = MongoServer.Create(MongoDBConfig.gConnectionString);
mongoDatabase = mongo.GetDatabase(MongoDBConfig.gDatabaseName) as MongoDatabase;
mongo.Connect();
}
///
/// Mongo 数据库断开连接
///
public void CloseConnection()
{
if (this.mongo != null)
{
this.mongo.Disconnect();
this.mongo = null;
}
}
///
/// 根据条件查找所有记录
///
///
public IEnumerable FindAll(IMongoQuery pQuery,int currentpage,int pagesize, string pTable)
{
//获取collectionName对应的集合,不存在则自动创建
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
return mongoCollection.Find(pQuery).SetLimit(pagesize).SetSkip(pagesize * currentpage).ToList(); ;
}
///
/// 查找所有记录
///
///
public IEnumerable FindAll(int currentpage,int pagesize,string pTable)
{
//获取collectionName对应的集合,不存在则自动创建
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
return mongoCollection.FindAll().SetLimit(pagesize).SetSkip(pagesize*currentpage).ToList();
}
///
/// 根据条件查找所有记录
///
///
public IEnumerable FindAll(IMongoQuery pQuery, string pTable)
{
//获取collectionName对应的集合,不存在则自动创建
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
return mongoCollection.Find(pQuery);
}
///
/// 查找所有记录
///
///
public IEnumerable FindAll(string pTable)
{
//获取collectionName对应的集合,不存在则自动创建
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
return mongoCollection.FindAll();
}
///
/// 增加一条记录
///
///
public void Add(object obj, string pTable)
{
//获取collectionName对应的集合,不存在则自动创建
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
mongoCollection.Insert(obj);
}
///
/// 删除一条记录
///
public void Delete(string id, string pTable)
{
//获取collectionName对应的集合,不存在则自动创建
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
mongoCollection.Remove(new QueryDocument { { "_id", id } });
}
#region 获取当前连接数据库的指定集合【依据类型】
///
/// 获取当前连接数据库的指定集合【依据类型】
///
///
///
public MongoCollection GetCollection(string name,WriteConcern writeConcern) where T : class
{
return this.mongoDatabase.GetCollection(name,writeConcern);
}
///
/// 获取当前连接数据库的指定集合【根据指定名称】
///
///
/// 集合名称
///
public MongoCollection GetCollection(string pTableName) where T : class
{
return this.mongoDatabase.GetCollection(pTableName);
}
#endregion
#region GridFs 文件处理
///
/// 保存2进制数据到db里面
///
///
///
public string GridFsSave(byte[] byteFile)
{
string filename = Guid.NewGuid().ToString();
//这里GridFile构造函数有个重载,bucket参数就是用来替换那个创建集合名中默认的"fs"的。
MongoGridFS gridFile = new MongoGridFS(mongoDatabase);
using (MongoGridFSStream gridFileStream = gridFile.Create(filename))
{
gridFileStream.Write(byteFile, 0, byteFile.Length);
}
return filename;
}
public void SaveGridFsFile(BsonDocument doc, string pTable)
{
MongoCollection mongoCollection =mongoDatabase.GetCollection(pTable) as MongoCollection;
mongoCollection.Save(doc);
}
///
/// 读取为filename的文件
///
///
///
public byte[] GridFsRead(string filename)
{
MongoGridFS gridFile = new MongoGridFS(mongoDatabase);
MongoGridFSStream gridFileStream = gridFile.OpenRead(filename);
byte[] bytes = new byte[gridFileStream.Length];
gridFileStream.Read(bytes, 0, bytes.Length);
return bytes;
}
///
/// 根据条件取一条数据
///
///
///
///
public BsonDocument GetFsFileInfo(QueryDocument fitter, string pTable)
{
MongoCollection mongoCollection = mongoDatabase.GetCollection(pTable) as MongoCollection;
BsonDocument doc = mongoCollection.FindOne(fitter);
return doc;
}
///
/// 删除文件
///
///
public void GridFsDelete(string filename)
{
MongoGridFS gridFile = new MongoGridFS(mongoDatabase);
gridFile.Delete(new QueryDocument("filename", filename));
}
#endregion
}
备注:由于最近使用.net开发一个项目,有机会用到了mongodb,我自己改成通用类
方法:
1.找到mongodb官网
2.下载mongodb.net的类库,我用的是MongoDB.Bson.dll,MongoDB.Driver.dll的1.7版本,每个版本还不一样.....mongodb对语言访问支持正在加强.
3.以上是底层代码
|
|
|