MongoDB.Driver 管道 Aggregate
public IAsyncCursor<BsonDocument> GetAggregate(MongodbHost host,PipelineDefinition<BsonDocument, BsonDocument> pipeline)
{
MongoClient client = new MongoClient(host.Connection);
var dataBase = client.GetDatabase(host.DataBase);
var dataTb=dataBase.GetCollection<BsonDocument>(host.Table);
return dataTb.Aggregate(pipeline);
}
public class MongodbHost
{
/// <summary>
/// 连接字符串
/// </summary>
public string Connection { get; set; }
/// <summary>
/// 库
/// </summary>
public string DataBase { get; set; }
/// <summary>
/// 表
/// </summary>
public string Table { get; set; } }
MongodbHost mongodbHost = new MongodbHost();
mongodbHost.Connection = "mongodb://test1:test1@127.0.0.1:27017/mytest";
mongodbHost.DataBase = "mytest";
mongodbHost.Table = "testtb1"; const string pipelineJson1 = "{$group:{_id:'$Symbol',Value:{$max:'$Value'}}}";//" {$project : {DTime : 1 ,AdvInf : 1}}";
const string pipelineJson2 = " { $match : { AdvInf : { $eq :3 } } }";
const string pipelineJson3 = " { $match : { DTime : { $gte :ISODate('2018-12-04T02:20:12') } } }"; IList<IPipelineStageDefinition> stages = new List<IPipelineStageDefinition>();
PipelineStageDefinition<BsonDocument, BsonDocument> stage1 =
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(pipelineJson1);
PipelineStageDefinition<BsonDocument, BsonDocument> stage2 =
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(pipelineJson2);
PipelineStageDefinition<BsonDocument, BsonDocument> stage3 =
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(pipelineJson3); stages.Add(stage1);
//stages.Add(stage2);
//stages.Add(stage3); PipelineDefinition<BsonDocument, BsonDocument> pipeline = new PipelineStagePipelineDefinition<BsonDocument, BsonDocument>(stages); var gdValue = GetAggregate(mongodbHost,pipeline);
管道的概念
管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数。
MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。
表达式:处理输入文档并输出。表达式是无状态的,只能用于计算当前聚合管道的文档,不能处理其它的文档。
这里我们介绍一下聚合框架中常用的几个操作:
- $project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
- $match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
- $limit:用来限制MongoDB聚合管道返回的文档数。
- $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
- $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
- $group:将集合中的文档分组,可用于统计结果。
- $sort:将输入文档排序后输出。
- $geoNear:输出接近某一地理位置的有序文档。
管道操作符实例
1、$project实例
db.article.aggregate(
{ $project : {
title : 1 ,
author : 1 ,
}}
);
这样的话结果中就只还有_id,tilte和author三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:
db.article.aggregate(
{ $project : {
_id : 0 ,
title : 1 ,
author : 1
}});
2.$match实例
db.articles.aggregate( [
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
$match用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段$group管道操作符进行处理。
3.$skip实例
db.article.aggregate(
{ $skip : 5 });
经过$skip管道操作符处理后,前五个文档被"过滤"掉。
MongoDB.Driver 管道 Aggregate的更多相关文章
- PHP7 - MongoDB Driver 使用心得
php7 只能使用Mongodb driver来驱动mongodb. 使用Mongodb Driver连接数据库 刚开始使用Mongodb Driver的时候我是拒绝的.查看官方文档只看到一排的类和不 ...
- MongoDB 聚合(管道与表达式)
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). aggregate() 方法 MongoDB中 ...
- C# .NET Core 3.1中使用 MongoDB.Driver 更新嵌套数组元素和关联的一些坑
C# .NET Core 3.1中使用 MongoDB.Driver 更新数组元素和关联的一些坑 前言: 由于工作的原因,使用的数据库由原来的 关系型数据库 MySQL.SQL Server 变成了 ...
- MongoDB Driver 简单的CURD
c#中我们可以使用MongoDB.Driver驱动进行对MongoDB数据库的增删改查. 首先需要在NuGet中安装驱动 安装完毕后会发现会有三个引用 其中 MongoDB.Driver和MongoD ...
- c# MongoDB Driver 官方教程翻译
先贴官方文档地址:http://mongodb.github.io/mongo-csharp-driver/2.5/getting_started/quick_tour/ 安装部分很简单,nuget搜 ...
- 基于MongoDB.Driver的扩展
由于MongoDB.Driver中的Find方法也支持表达式写法,结合[通用查询设计思想]这篇文章中的查询思想,个人基于MongoDB扩展了一些常用的方法. 首先我们从常用的查询开始,由于MongoD ...
- MongoDB系列:五、MongoDB Driver使用正确的姿势连接复制集
MongoDB复制集(Replica Set)通过存储多份数据副本来保证数据的高可靠,通过自动的主备切换机制来保证服务的高可用.但需要注意的时,连接副本集的姿势如果不对,服务高可用将不复存在. 使用复 ...
- C# mongoDB Driver 使用对象方式查询语法大全
#region 查询方法 /// <summary> /// 获取单个对象 /// </summary> /// <typeparam name="T" ...
- php MongoDB driver 查询实例
//是否只查mx $mx_on_switch = I("post.mx_on_switch"); //mx模糊查询 $mx_vague_check = I("post.m ...
随机推荐
- 【SAE 部署 JavaWeb 项目报 404 错误】
个人学习整理,如有不足之处,请不吝不吝赐教.转载请注明:@CSU-Max 今天写了一个小的 JavaWeb 项目传到 SAE 上.訪问的时候出错. 本地測试是正常的,并且曾经做微信平台开发的时候上传的 ...
- gen_server的enter_loop分析
http://my.oschina.net/astute/blog/119250?p=1 在看ranch user guide的过程中,发现实现protocol handler需要使用特殊的gen_s ...
- 【u240】棋子放置
Time Limit: 1 second Memory Limit: 128 MB 小虎刚刚上了幼儿园,老师让他做一个家庭作业:首先画3行格子,第一行有三个格子,第二行有2个格子,第三行有1个格子. ...
- NYOJ 36 最长公共子序列 (还是dp)
这个好多算法书上都有,不仅限于<算法导论> 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 咱们就不拐弯抹角了,如题.须要你做的就是写一个程序,得出最长公 ...
- 手动安装huson插件的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 首先到jenkins-ci.org下载插件,地址为:http://updates.jenkins-ci.org/dow ...
- 【38.24%】【codeforces 621E】 Wet Shark and Blocks
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- android studio的模拟器waiting for target device to come online原因
android studio的模拟器一直waiting for target device to come online,demo也运行不上去 如图所示: 你很可能运行的android 6.0 (AP ...
- Android源码分析-点击事件派发机制
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17339857 概述 一直想写篇关于Android事件派发机制的文章,却一直没 ...
- Bit error testing and training in double data rate (ddr) memory system
DDR PHY interface bit error testing and training is provided for Double Data Rate memory systems. An ...
- git与svn的不同
假设你在读这篇文章,说明你跟大多数开发人员一样对GIT感兴趣,假设你还没有机会来试一试GIT,我想如今你就要了解它了. GIT不不过个版本号控制系统,它也是个内容管理系统(CMS),工作管理系统等.假 ...