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 ...
随机推荐
- [TypeScript] Increase TypeScript's type safety with noImplicitAny
TypeScript tries to infer as much about your code as it can. But sometimes there really is not enoug ...
- ssh 自动登录脚本
ssh 一般要输入密码,自动设置的方法有两个: 1.通过expect来建立 #!/usr/bin/expect -f # expect在哪个目录下用whereis找下,不同的系统expect安装路径不 ...
- Java 出现内存溢出的定位以及解决方案
在上一节中Java虚拟机内存分布 说了Java虚拟机中分为五个区域,而且也知道了在Java程序计数器区域不会出现OOM(OutOfMemeryError),那么以下就对除了程序计数器以外的四个区域 ...
- JavaScript函数实现鼠标指向后带图片的提示效果
转载:http://www.cnblogs.com/jack86514/archive/2009/04/01/1427584.html 当我们在写一个网页程序的时候,很多方法可以提供页面的动态显示,从 ...
- Java开发环境安装,环境变量
下载地址:www.oracle.com Java 9下载地址:https://www.oracle.com/technetwork/java/javase/downloads/java-archive ...
- 聊聊QPS/TPS/并发量/系统吞吐量的概念
原文:聊聊QPS/TPS/并发量/系统吞吐量的概念 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/cainiao_user/article/deta ...
- 数据访问层之Repository
数据访问层之Repository 接上文 项目架构开发:数据访问层之Logger 本章我们继续IRepository开发,这个仓储与领域模式里边的仓储有区别,更像一个工具类,也就是有些园友说的“伪 ...
- C# 使用外部别名
原文:C# 使用外部别名 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博客.本文 ...
- url操作等
取得url ?及以前: baseUrl = url.substr(0,url.indexOf('?')+1) searchParam = searchParam.slice(0, -1);//去掉最后 ...
- jquery validate 详细说明
jQuery校验 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库 <script src=&q ...