Wisdombud.Mongo

包含内容

 MongoDB.Bson.dll
MongoDB.Bson.xml
MongoDB.Driver.dll
MongoDB.Driver.xml
Wisdombud.Mongo.dll
Wisdombud.Mongo.XML

用法示例

需要注意的地方

  1. dao实例化需要指定一个URL,现在我们用的都是没有密码的,格式:mongodb://localhost/database_name
  2. 每个实体要继承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

MongoDB入门(6)- 我们自己封装的MongoDB-C#版本的更多相关文章

  1. [MongoDB知识体系] 一文全面总结MongoDB知识体系

    MongoDB教程 - Mongo知识体系详解 本系列将给大家构建MongoDB全局知识体系.@pdai MongoDB教程 - Mongo知识体系详解 知识体系 学习要点 学习资料 官网资料 入门系 ...

  2. .Net Core ORM选择之路,哪个才适合你 通用查询类封装之Mongodb篇 Snowflake(雪花算法)的JavaScript实现 【开发记录】如何在B/S项目中使用中国天气的实时天气功能 【开发记录】微信小游戏开发入门——俄罗斯方块

    .Net Core ORM选择之路,哪个才适合你   因为老板的一句话公司项目需要迁移到.Net Core ,但是以前同事用的ORM不支持.Net Core 开发过程也遇到了各种坑,插入条数多了也特别 ...

  3. MongoDB入门(5)- 我们自己封装的MongoDB-Java版本

    用法 实体定义 package com.wisdombud.mongotool; import java.io.Serializable; import java.util.Date; import ...

  4. Nodejs学习笔记(十)--- 与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门

    目录 简介 MongoDB安装(windows) MongoDB基本语法和操作入门(mongo.exe客户端操作) 库操作 插入 查询 修改 删除 存储过程 nodejs操作MongoDB 插入 查询 ...

  5. MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门

    MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshell.c ...

  6. Nodejs学习笔记(十)—与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门

    简介 MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshel ...

  7. MongoDB入门实践

    MongoDB入门实践 简单介绍MongoDB,包括MongoDB的使用场景.和MySQL的对比.安装部署.Java客户端访问及总结 MongoDB? 我们遵循需求驱动技术的原则,通过一个场景来引入M ...

  8. MongoDB入门(7)- SpringDataMongoDB

    入门 本文介绍如何应用SpringDataMongoDB操作实体和数据库,本文只介绍最基本的例子,复杂的例子在后面的文章中介绍. SpringDataMongoDB简介 SpringDataMongo ...

  9. mongodb入门学习小记

    Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...

  10. MongoDB 入门之基础 DCL

    此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...

随机推荐

  1. C Program进阶-数组

    (一)数组的内存布局 对于语句int a[5]; 我们明白这里定义了一个数组,数组里有5个元素,每一个元素都是int类型,我们可以用a[0],a[1]等访问数组里的元素,但是这些元素的名字就是a[0] ...

  2. Zen Coding && Emmet-Sublime 安装

    Sublime Text 插件之:Emmet,旧版称:ex-Zen Coding 更名之后增加了CSS3和HTML5许多新特性.项目地址也从 code.google 移 github. 安装: Pac ...

  3. android AndroidManifest.xml uses-feature 详解

    如果你是一个Android用户,而且你有一个老旧的安装有android 1.5 的android设备,你可 能会注意到一些高版本的应用没有在手机上的Android Market 中显示.这必定是应用使 ...

  4. 代码编写规范Asp.Net(c#)

    1        目的 为了统一公司软件开发的设计过程中关于代码编写时的编写规范和具体开发工作时的编程规范,保证代码的一致性,便于交流和维护,特制定此规范. 2        范围 本规范适用于开发组 ...

  5. java 基础 --集合--012

    1, 数组与集合 A:长度不同 数组的长度固定,集合的长度可变 B:内容不同 数组里存储的是同一种类型的元素,而集合可以存储不同类型的元素 C:元素的数据类型问题 数组可以存储基本数据类型,也可以存储 ...

  6. centos7 nginx端口转发出现502的其中一种原因

    在排查了一系列可能的原因后仍无法解决,经资料查阅可能是SELinux造成,SELinux很强大但若配置不当也会造成很多组件无法正常使用,这里直接将其关闭: //打开配置文件 vi /etc/selin ...

  7. HTML5 不再支持哪些元素

    HTML5中已经不支持以下的元素,不建议在进行开发时再使用以下的元素. 1.acronym(建议abbr):定义首字母缩写 2.applet(建议object):定义applet 3.basefont ...

  8. CF546E Soldier and Traveling

    题目描述 In the country there are n n n cities and m m m bidirectional roads between them. Each city has ...

  9. BZOJ1058:[ZJOI2007]报表统计——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1058 https://www.luogu.org/problemnew/show/P1110#su ...

  10. BZOJ4872:[SHOI2017]分手是祝愿——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4872 https://www.luogu.org/problemnew/show/P3750 Zei ...