MongoDB入门(6)- 我们自己封装的MongoDB-C#版本
Wisdombud.Mongo
包含内容
MongoDB.Bson.dll
MongoDB.Bson.xml
MongoDB.Driver.dll
MongoDB.Driver.xml
Wisdombud.Mongo.dll
Wisdombud.Mongo.XML
用法示例
需要注意的地方
- dao实例化需要指定一个URL,现在我们用的都是没有密码的,格式:mongodb://localhost/database_name
- 每个实体要继承MongoDbEntity,MongoDbEntity有一个属性是ID,这个ID不需要赋值
using System.Collections.Generic;
using Wisdombud.Mongo;
namespace MongoTest
{
class Program
{
private static MongoDbBaseDao<Person> dao = new MongoDbBaseDao<Person>("mongodb://localhost/database_name");
static void Main(string[] args)
{
SaveDemo();
UpdateDemo();
}
private static void SaveDemo()
{
Person p = new Person();
p.Name = "wisdombud";
p.Children = new List<string>();
p.Children.Add("jungong");
p.Children.Add("mobile");
dao.Save(p);
}
private static void UpdateDemo()
{
var person = dao.GetByCondition(a => a.Name == "wisdombud");
if (person != null)
{
person.Name = "zhaoaaaaa";
dao.Save(person);
}
}
}
public class Person : MongoDbEntity
{
public string Name { get; set; }
public List<string> Children { get; set; }
}
}
核心源码
#region Public Methods and Operators
/// <summary>
/// Counts the specified condition.
/// </summary>
/// <param name="condition">The condition.</param>
/// <returns></returns>
public virtual long Count(Expression<Func<T, bool>> condition)
{
return this.repository.GetCollection<T>(this.collectioname).AsQueryable().Where(condition).LongCount();
}
/// <summary>
/// Count all elements
/// </summary>
/// <returns></returns>
public virtual long Count()
{
return this.repository.GetCollection<T>(this.collectioname).Count();
}
/// <summary>
/// Deletes the specified object.
/// </summary>
/// <param name="pobject">The object.</param>
public virtual void Delete(T pobject)
{
this.repository.GetCollection<T>(this.collectioname).Remove(Query.EQ("_id", new ObjectId(pobject.Id)));
}
/// <summary>
/// Deletes the specified object.
/// </summary>
/// <param name="condition"></param>
public virtual void Delete(Expression<Func<T, bool>> condition)
{
var todelete = this.GetAll(condition) as List<T>;
if (todelete == null || todelete.Count <= 0)
{
return;
}
foreach (T t in todelete)
{
Delete(t);
}
}
public virtual void DeleteAll()
{
IEnumerable<T> todelete = this.GetAll();
foreach (T t in todelete)
{
Delete(t);
}
}
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
public virtual IEnumerable<T> GetAll()
{
return this.Convert2Show(this.repository.GetCollection<T>(this.collectioname).FindAll().ToList());
}
/// <summary>
/// Gets all using a condition.
/// </summary>
/// <param name="condition">The condition.</param>
/// <returns></returns>
public virtual IEnumerable<T> GetAll(Expression<Func<T, bool>> condition)
{
return this.Convert2Show(this.repository.GetCollection<T>(this.collectioname).AsQueryable().Where(condition).ToList());
}
/// <summary>
/// Gets all using a condition, a TOP clause and an optional orderBy clause.
/// </summary>
/// <param name="condition">The condition.</param>
/// <param name="maxresult">The max result.</param>
/// <param name="orderByDescending">if set to <c>true</c> [order by descending].</param>
/// <returns></returns>
public virtual IEnumerable<T> GetAll(
Expression<Func<T, bool>> condition,
int maxresult,
bool orderByDescending)
{
IQueryable<T> query = this.repository.GetCollection<T>(this.collectioname).AsQueryable().Where(condition);
return this.Convert2Show(orderByDescending
? query.OrderByDescending(x => x.Id).Take(maxresult).ToList()
: query.OrderBy(x => x.Id).Take(maxresult).ToList());
}
/// <summary>
/// Gets the by condition.
/// </summary>
/// <param name="condition">The condition.</param>
/// <returns></returns>
public virtual T GetByCondition(Expression<Func<T, bool>> condition)
{
return this.Convert2Show(this.repository.GetCollection<T>(this.collectioname).AsQueryable().Where(condition).FirstOrDefault());
}
/// <summary>
/// Gets the by MongoId.
/// </summary>
/// <param name="MongoId">The _id.</param>
/// <returns></returns>
public virtual T GetById(string MongoId)
{
return this.Convert2Show(this.repository.GetCollection<T>(this.collectioname).FindOne(Query.EQ("_id", new ObjectId(MongoId))));
}
public T GetFirst(string field, string search)
{
long selectCount;
IEnumerable<T> selectList = this.Search(field, search, 1, 1, out selectCount);
return this.Convert2Show(selectList.FirstOrDefault());
}
/// <summary>
/// Paginates by an specified condition.
/// </summary>
/// <param name="condition">The condition.</param>
/// <param name="pagesize">The page size.</param>
/// <param name="page">The page.</param>
/// <param name="pOrderByClause">The OrderBy Clause.</param>
/// <param name="pOrderByDescending">if set to <c>true</c> [order by descending].</param>
/// <returns></returns>
public virtual IEnumerable<T> Paginate<TKey>(
Expression<Func<T, bool>> condition,
int pagesize,
int page,
out long allRecordCount,
Func<T, TKey> pOrderByClause,
bool pOrderByDescending)
{
IEnumerable<T> query = this.repository.GetCollection<T>(this.collectioname).AsQueryable().Where(condition);
allRecordCount = query.Count();
return this.PaginateQuery(pagesize, page, pOrderByClause, pOrderByDescending, query);
}
/// <summary>
/// Paginates by an specified condition.
/// </summary>
/// <param name="pagesize">The page size.</param>
/// <param name="page">The page.</param>
/// <param name="pOrderByClause">The OrderBy Clause.</param>
/// <param name="pOrderByDescending">if set to <c>true</c> [order by descending].</param>
/// <returns></returns>
public virtual IEnumerable<T> Paginate<TKey>(
int pagesize,
int page,
Func<T, TKey> pOrderByClause,
bool pOrderByDescending)
{
IQueryable<T> query = this.repository.GetCollection<T>(this.collectioname).AsQueryable();
return this.PaginateQuery(pagesize, page, pOrderByClause, pOrderByDescending, query);
}
/// <summary>
/// Saves the specified object.
/// </summary>
/// <param name="pobject">The object.</param>
/// <returns></returns>
public virtual T Save(T pobject)
{
pobject = this.Convert2Db(pobject);
this.repository.GetCollection<T>(this.collectioname).Save(pobject);
return pobject;
}
/// <summary>
/// Search indexes
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="field"></param>
/// <param name="search">The search value</param>
/// <param name="page"></param>
/// <param name="pagesize"></param>
/// <param name="foundedRecords">total number of founded records in index</param>
/// <returns></returns>
public IEnumerable<T> Search(string field, string search, int page, int pagesize, out long foundedRecords)
{
IMongoQuery query = Query.Matches(field, new BsonRegularExpression(search, "i"));
List<T> totallist = this.repository.GetCollection<T>(this.collectioname).Find(query).ToList();
foundedRecords = totallist.Count;
var list = totallist.Skip(pagesize * (page - 1)).Take(pagesize).ToList();
return this.Convert2Show(list);
}
public IEnumerable<T> Search_And(
IDictionary<string, string> keysAndValues,
int page,
int pagesize,
out long foundedRecords)
{
List<IMongoQuery> queries =
keysAndValues.Keys.Select(key => Query.Matches(key, new BsonRegularExpression(keysAndValues[key], "i")))
.ToList();
IMongoQuery query = Query.And(queries);
List<T> totallist = this.repository.GetCollection<T>(this.collectioname).Find(query).ToList();
foundedRecords = totallist.Count;
var list = totallist.Skip(pagesize * (page - 1)).Take(pagesize).ToList();
return this.Convert2Show(list);
}
public IEnumerable<T> Search_And(IList<IMongoQuery> search, int page, int pagesize, out long foundedRecords)
{
IMongoQuery query = Query.And(search);
List<T> totallist = this.repository.GetCollection<T>(this.collectioname).Find(query).ToList();
foundedRecords = totallist.Count;
var list = totallist.Skip(pagesize * (page - 1)).Take(pagesize).ToList();
return this.Convert2Show(list);
}
public IEnumerable<T> Search_Or(
IDictionary<string, string> keysAndValues,
int page,
int pagesize,
out long foundedRecords)
{
List<IMongoQuery> queries =
keysAndValues.Keys.Select(key => Query.Matches(key, new BsonRegularExpression(keysAndValues[key], "i")))
.ToList();
IMongoQuery query = Query.Or(queries);
List<T> totallist = this.repository.GetCollection<T>(this.collectioname).Find(query).ToList();
foundedRecords = totallist.Count;
var list = totallist.Skip(pagesize * (page - 1)).Take(pagesize).ToList();
return this.Convert2Show(list);
}
public IEnumerable<T> Search_Or(IList<IMongoQuery> search, int page, int pagesize, out long foundedRecords)
{
IMongoQuery query = Query.Or(search);
List<T> totallist = this.repository.GetCollection<T>(this.collectioname).Find(query).ToList();
foundedRecords = totallist.Count;
var list = totallist.Skip(pagesize * (page - 1)).Take(pagesize).ToList();
return this.Convert2Show(list);
}
#endregion