SequoiaDB的聚合函数aggregate()在Java开发中的应用

聚合函数

概述

聚合函数提供了对集合中的原始数据记录进行统计计算的能力.通过使用聚合函数,用户能够直接从集合中提取数据记录并获取所需的统计结果.聚合函数提供的操作接口类似于集合中的查询操作,不同的是聚合函数还提供了一系列函数及操作对查询结果进行处理.

聚集符

概述

参数名描述示例
$project选择需要输出的字段名,"1"表示输出,"0"表示不输出,还可以实现字段的重命名{ KaTeX parse error: Expected '}', got 'EOF' at end of input: …d: 0, aliase: "field3" } }
$match实现从集合中选择匹配条件的记录,相当于SQL语句的where{$match: { field: { $lte: value } } }
$limit限制返回的记录条数{ $limit: 10 }
$skip控制结果集的开始点,即跳过结果集中指定条数的记录{ $skip: 5 }
$group实现对记录的分组,类似于SQL的group by语句,"_id"指定分组字段{ KaTeX parse error: Expected '}', got 'EOF' at end of input: group: { _id: "field" } }
$sort实现对结果集的排序,“1"代表升序,”-1"代表降序{ $sort: { field1: 1, field2: -1, … } }

应用举例(快速入门)

这里的快速入门以两种方式(shell和json开发)演示聚合函数的功能.

Shell

创建集合
> var db = new Sdb("localhost",11810)
> db.createCS("foo")
> db.foo.createCL("bar")
插入数据
> db.foo.bar.insert({ name: "张三", income: 10000, age: 31, city: "北京" })
> db.foo.bar.insert({ name: "李四", income: 25000, age: 28, city: "上海" })
> db.foo.bar.insert({ name: "王五", income: 15000, age: 32, city: "上海" })
> db.foo.bar.insert({ name: "赵六", income: 30000, age: 40, city: "北京" })
使用聚合函数筛选

使用"$match"子操作将集合中年龄大于30的数据记录筛选出来

>db.foo.bar.aggregate({$match:{age:{$gt:30}}})

经过筛选后,数据为:

{ name: "张三", income: 10000, age: 31, city: "北京" }
{ name: "王五", income: 15000, age: 32, city: "上海" }
{ name: "赵六", income: 30000, age: 40, city: "北京" }

JSON开发

创建数据库连接
 //创建数据库连接
 public Sequoiadb getConnection() {   
 	Sequoiadb sequoiadb = new Sequoiadb("192.168.248.128:11810", "sdbadmin", "sdbadmin");
 	return sequoiadb;
 }
创建并获得集合
//创建集合空间
public void crtCS() {
 	Sequoiadb sequoiadb = getConnection();
    sequoiadb.createCollectionSpace("foo");
}
//创建集合
public void crtCL() {
	Sequoiadb sequoiadb = getConnection();
    sequoiadb.getCollectionSpace("foo").createCollection("bar");
}
//获取集合
public DBCollection getCollection() {
	Sequoiadb sequoiadb = getConnection();
    CollectionSpace cs = sequoiadb.getCollectionSpace("foo");
    DBCollection cl = cs.getCollection("bar");
    return cl;
}
插入数据
//插入数据
public void insert() {
	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject();
    bsonObject1.put("name", "张三");
    bsonObject1.put("age", 31);
    bsonObject1.put("income", 10000);
    bsonObject1.put("city", "北京");
	......
	list.add(bsonObject1);
	......
	dbCollection.insert(list);
}

查询数据
//查询数据
public void query() {
	DBCollection dbCollection = getCollection();
    // 查询所有记录,并把查询结果放在游标对象中
    DBCursor cursor = dbCollection.query();
    try {
    	while (cursor.hasNext()) {
        	BSONObject record = cursor.getNext();
            // 可将返回记录映射成自定义实体对象(Person)
            Person person = record.as(Person.class);
            System.out.println(person.toString());
        }
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

使用聚合函数查询数据
 //聚合函数
 public void test() {
 	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject();
    bsonObject1.put("$gt", 30);
    BSONObject bsonObject2 = new BasicBSONObject();
    bsonObject2.put("age", bsonObject1);
    BSONObject bsonObject3 = new BasicBSONObject();
    bsonObject3.put("$match", bsonObject2);
    list.add(bsonObject3);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
        BSONObject record = cursor.getNext();
        // 可将返回记录映射成自定义实体对象(Person)
        Person person = record.as(Person.class);
        System.out.println(person.toString());
        }
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

输出结果:

Person{name='张三', age=31, income=10000, city='北京'}
Person{name='王五', age=32, income=15000, city='上海'}
Person{name='赵六', age=40, income=30000, city='北京'}

Java API中聚合函数的介绍

public DBCursor com.sequoiadb.base.DBCollection.aggregate(List<BSONObject> objs) throws BaseException

对集合进行聚合操作的函数.

参数:

objs-规则列表的bson对象,不能为空

抛出:

BaseException - If error happens.

应用(代码片段)

$project

语法
{ $project: { <字段名1:0 | 1 | "$新字段名1">, [字段名2: 0 | 1 | "$新字段名2", ... ] } }

描述

p r o j e c t 类 似 S Q L 中 的 s e l e c t 语 句 , 通 过 使 用 project类似SQL中的select语句,通过使用 projectSQLselect,使project操作可以从记录中筛选出所需字段:

  • 如果字段的值为1,表示选出
  • 如果字段的值为0,表示不选
  • 通过"$新字段名"可以实现把字段重命名为"新字段名"

如果记录不存在所选字段,则以如下格式输出:“field”:null,其中field为不存在的字段名.对嵌套对象使用点操作符(.)引用字段名

示例

使用$project展示集合bar中的name字段

public void project() {
	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject();
    bsonObject1.put("name", 1);
    bsonObject1.put("age", 0);
    bsonObject1.put("income", 0);
    //bsonObject1.put("city", 1);
    BSONObject bsonObject2 = new BasicBSONObject();
    bsonObject2.put("$project", bsonObject1);
    list.add(bsonObject2);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
        BSONObject record = cursor.getNext();
        // 可将返回记录映射成自定义实体对象(Person)
        Person person = record.as(Person.class);
        System.out.println(person.toString());
    	}
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

输出结果:

Person{name='张三', age=null, income=null, city='null'}
Person{name='李四', age=null, income=null, city='null'}
Person{name='王五', age=null, income=null, city='null'}
Person{name='赵六', age=null, income=null, city='null'}

$match

语法

$match与db.collectionspace.collection.find()方法中的cond参数完全相同.

描述

通过$match可以从集合中选择匹配条件的记录.

示例

该操作表示返回集合foo.bar中年龄大于30和城市为北京的记录.

public void match() {
	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject();
    bsonObject1.put("city", "北京");
    BSONObject bsonObject3 = new BasicBSONObject();
    bsonObject3.put("$gt", 30);
    BSONObject bsonObject2 = new BasicBSONObject();
    bsonObject2.put("age", bsonObject3);
    BasicBSONList arr = new BasicBSONList();
    arr.put("0", bsonObject2);
    arr.put("0", bsonObject1);
    BSONObject bsonObject4 = new BasicBSONObject();
    bsonObject4.put("$and", arr);
    BSONObject bsonObject5 = new BasicBSONObject();
    bsonObject5.put("$match", bsonObject4);
    list.add(bsonObject5);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
        	BSONObject record = cursor.getNext();
            // 可将返回记录映射成自定义实体对象(Person)
            Person person = record.as(Person.class);
            System.out.println(person.toString());
        }
   	} catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

输出结果:

Person{name='张三', age=31, income=10000, city='北京'}
Person{name='赵六', age=40, income=30000, city='北京'}

$limit

语法
{ $limit: <返回记录数> }

描述

$limit实现在结果集中限制返回的记录条数.如果指定的记录条数大于实际的记录总数,那么返回实际的记录总数.

示例

该操作表示集合foo.bar中读取前3条记录。

public void limit() {
	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject();
    bsonObject1.put("$limit", 3);
    list.add(bsonObject1);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
            BSONObject record = cursor.getNext();
            // 可将返回记录映射成自定义实体对象(Person)
            Person person = record.as(Person.class);
            System.out.println(person.toString());
        }
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
   		cursor.close();
    }
}

输出结果:

Person{name='张三', age=31, income=10000, city='北京'}
Person{name='李四', age=28, income=25000, city='上海'}
Person{name='王五', age=32, income=15000, city='上海'}

$sort

语法
{ $sort: { 字段1: -1 | 1 [, 字段2: -1 | 1, ... ] } }

描述

$sort用来指定结果集的排序规则.对嵌套对象使用点操作符(.)引用字段名

  • 字段取值为-1表示降序排列
  • 字段取值为1表示升序排列
示例

该操作表示从集合foo.bar中读取记录,并以age的字段值进行降序排序(1表示升序,-1表示降序);

当记录间age字段值相同时,则以income字段值进行升序排序。

 public void sort() {
 	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject();
    bsonObject1.put("age", -1);
    bsonObject1.put("income", 1);
    BSONObject bsonObject2 = new BasicBSONObject();
    bsonObject2.put("$sort", bsonObject1);
    list.add(bsonObject2);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
        	BSONObject record = cursor.getNext();
            // 可将返回记录映射成自定义实体对象(Person)
            Person person = record.as(Person.class);
            System.out.println(person.toString());
        }
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
   		e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

输出结果:

Person{name='赵六', age=40, income=30000, city='北京'}
Person{name='王五', age=32, income=15000, city='上海'}
Person{name='张三', age=31, income=10000, city='北京'}
Person{name='李四', age=28, income=25000, city='上海'}

$skip

语法
 $skip: <记录数> }

描述

$skip参数控制结果集的开始点,即跳过结果集中指定条数的记录.如果跳过的记录数大于总记录数,返回0条记录.

示例

该操作表示从集合foo.bar中读取记录,并跳过前面2条,从第3条记录开始返回。

public void skip() {
	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject = new BasicBSONObject("$skip", 2);
    list.add(bsonObject);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
        	BSONObject record = cursor.getNext();
            // 可将返回记录映射成自定义实体对象(Person)
            Person person = record.as(Person.class);
            System.out.println(person.toString());
        }
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

输出结果:

Person{name='王五', age=32, income=15000, city='上海'}
Person{name='赵六', age=40, income=30000, city='北京'}

$group

语法

{ $group: { _id:"$分组字段名", 显示字段名: { 聚集函数: "$字段名"},[显示字段名2: { 聚集函数: "$字段名"}, ...] } }

描述

$group实现对结果集的分组,类似SQL中的group by语句.

首先指定分组键(_id),通过"_id"来标识分组字段,分组字段可以是单个,也可以是多个,格式如下:

单个分组键:

{_id:"$field"}

多个分组键

{_id:{field1:"$field1",field2:"$field2",...}}

示例

该操作表示从集合foo.bar中读取记录,并按city字段进行分组.

 public void group() {
 	DBCollection dbCollection = getCollection();
    List<BSONObject> list = new ArrayList<BSONObject>();
    BSONObject bsonObject1 = new BasicBSONObject("_id","$city");
    BSONObject bsonObject2 = new BasicBSONObject("$group",bsonObject1);
    list.add(bsonObject2);
    DBCursor cursor = dbCollection.aggregate(list);
    try {
    	while (cursor.hasNext()) {
        	BSONObject record = cursor.getNext();
        	// 可将返回记录映射成自定义实体对象(Person)
        	Person person = record.as(Person.class);
            System.out.println(person.toString());
        }
    } catch (BaseException e) {
    	System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
    } catch (Exception e) {
    	e.printStackTrace();
    } finally {
    	cursor.close();
    }
}

输出结果:

Person{name='李四', age=28, income=25000, city='上海'}
Person{name='张三', age=31, income=10000, city='北京'}

$group支持的聚集函数
函数名描述
$addtoset将字段添加到数组中,相同的字段值只会添加一次
$first取分组中第一条记录中的字段值
$last取分组中最后一条记录中的字段值
$max取分组中字段值最大的
$min取分组中字段值最小的
$avg取分组中字段值的平均值
$push将所有字段添加到数组中,即使数组中已经存在相同的字段值,也继续添加
$sum取分组中字段值的总和
$count对记录分组后,返回表所有的记录条数
$addtoset

记录分组后,使用$addtoset 将指定字段值添加到数组中,相同的字段值只会添加一次。对嵌套对象使用点操作符(.)引用字段名。

示例

如下操作对记录分组后将指定字段值添加到数组中输出:

> db.foo.bar.aggregate({ $group: { _id: "$dep", Dep: { $first: "$dep" }, addtoset_major: { $addtoset: "$major" } } })
{
  "Dep": "物电学院",
  "addtoset_major": [
    "物理学",
    "光学",
    "电学"
  ]
}
{
  "Dep": "计算机学院",
  "addtoset_major": [
    "计算机科学与技术",
    "计算机软件与理论",
    "计算机工程"
  ]
}

此操作对记录按dep字段值进行分组,并使用 f i r s t 输 出 每 个 组 第 一 条 记 录 的 d e p 字 段 , 输 出 字 段 名 为 D e p ; 又 将 m a j o r 字 段 的 值 使 用 first输出每个组第一条记录的dep字段,输出字段名为Dep;又将 major字段的值使用 firstdepDepmajor使addtoset 放入数组中返回,输出字段名为addtoset_major,如下:

$count

记录分组后,用$count 取出分组所包含的总记录条数。

示例

对记录分组后,返回表所有的记录条数:

> db.foo.bar.aggregate({ $group: { Total: { $count: "$dep" } } })
{
  "Total": 1001
}

$first

记录分组后,取分组中第一条记录指定的字段值,对嵌套对象使用点操作符(.)引用字段名。

示例

对记录分组后,输出每个分组第一条记录的指定字段值:

> db.foo.bar.aggregate({ $group: { _id: "$dep", Dep: { $first: "$dep" }, Name: { $first: "$info.name" } } })
{
  "Dep": "物电学院",
  "Name": "Lily"
}
{
  "Dep": "计算机学院",
  "Name": "Tom"
}

此操作对记录按dep字段分组,取每个分组中第一条记录的dep字段值和嵌套对象name字段值,输出字段名分别为 Dep 和 Name。

$avg

记录分组后,取分组中指定字段的平均值返回,对嵌套对象使用点操作符(.)引用字段名。

示例

对记录分组后,返回分组中指定字段的平均值:

> db.foo.bar.aggregate({ $group: { _id: "$dep", avg_age: { $avg: "$info.age" }, max_age: { $max: "$info.age" }, min_age: { $min: "$info.age" } } })
{
  "avg_age": 23.727273,
  "max_age": 36,
  "min_age": 15
}
{
  "avg_age": 24.5,
  "max_age": 30,
  "min_age": 20
}

此操作对记录按dep字段分组,使用 a v g 返 回 每 个 分 组 中 的 嵌 套 对 象 a g e 字 段 的 平 均 值 , 输 出 字 段 名 为 a v g a g e ; 又 使 用 avg返回每个分组中的嵌套对象age字段的平均值,输出字段名为avg_age;又使用 avgageavgage使min返回每个分组中嵌套对象age字段的最小值,输出字段名为min_age,使用$max 返回每个分组中嵌套对象 age字段的最大值,输出字段名为max_age。

$max

记录分组后,取分组中指定字段的最大值返回,对嵌套对象使用点操作符(.)引用字段名。

示例

对记录分组后,返回分组中指定字段的最大值:

> db.foo.bar.aggregate({ $group: { _id: "$dep", max_score: { $max:"$score" }, Name: { $last: "$info.name" } } })
{
  "max_score": 93,
  "Name": "Kate"
}
{
  "max_score": 90,
  "Name": "Jim"
}

此操作对记录按dep字段分组,使用$max 返回每个分组中score 字段的最大值,输出字段名为max_score,又使用 $last取每个分组中最后一条记录嵌套对象name字段值,输出字段名为Name。

$min

记录分组后,取分组中指定字段的最小值返回,对嵌套对象使用点操作符(.)引用字段名。

示例

对记录分组后,返回分组中指定字段的最小值:

> db.foo.bar.aggregate({ $group: { _id: "$dep", min_score: { $min: "$score" }, Name: { $last: "$info.name" } } })
{
  "min_score": 72,
  "Name": "Kate"
}
{
  "min_score": 69,
  "Name": "Jim"
}

此操作对记录按dep字段分组,使用$min 返回每个分组中score字段的最小值,输出字段名为min_score,又使用 $last取每个分组中最后一条记录嵌套对象name字段值,输出字段名为Name。

$last

记录分组后,取分组中最后一条记录指定的字段值,对嵌套对象使用点操作符(.)引用字段名。

示例

对记录分组后,输出每个分组最后一条记录的指定字段值:

> db.foo.bar.aggregate({ $group: { _id: "$dep", Major: { $addtoset: "$major" }, Name: { $last: "$info.name" } } })
{
  "Major": [
    "物理学",
    "光学",
    "电学"
  ],
  "Name": "Kate"
}
{
  "Major": [
    "计算机科学与技术",
    "计算机软件与理论",
    "计算机工程"
  ],
  "Name": "Jim"
}

此操作对记录按dep字段分组,使用 l a s t 取 每 个 分 组 中 最 后 一 条 记 录 嵌 套 对 象 n a m e 字 段 值 , 输 出 字 段 名 为 N a m e , 并 且 将 每 个 分 组 中 的 m a j o r 字 段 值 使 用 last取每个分组中最后一条记录嵌套对象name字段值,输出字段名为Name,并且将每个分组中的major字段值使用 lastnameNamemajor使addtoset填充到数组中返回,返回字段名为Major。

$push

记录分组后,使用$push将指定字段值添加到数组中,即使数组中已经存在相同的值,也继续添加。对嵌套对象使用点操作符(.)引用字段名。

示例
> db.foo.bar.aggregate({ $group: { _id: "$dep", Dep: { $first: "$dep" }, push_age: { $push: "$info.age" } } })
{
  "Dep": "物电学院",
  "push_age": [
    28,
    18,
    20,
    30,
    28,
    20
  ]
}
{
  "Dep": "计算机学院",
  "push_age": [
    25,
    20,
    22
  ]
}

此操作对记录按dep字段值进行分组,每个分组中嵌套对象age字段的值使用$push放入数组中返回,输出字段名为 push_age,如下:

$sum

记录分组后,返回每个分组中指定字段值的总和,对嵌套对象使用点操作符(.)引用字段名。

示例
> db.foo.bar.aggregate({ $group: { _id: "$dep", sum_score: { $sum: "$score" }, Dep: { $first: "$dep" } } })
{
  "sum_score": 888,
  "Dep": "物电学院"
}
{
  "sum_score": 476,
  "Dep": "计算机学院"
}

此操作对记录按dep字段分组,使用 s u m 返 回 每 个 分 组 中 s c o r e 字 段 值 的 总 和 , 输 出 字 段 名 为 s u m s c o r e ; 又 使 用 sum返回每个分组中score字段值的总和,输出字段名为sum_score;又使用 sumscoresumscore使first取每个分组中第一条记录的dep字段值,输出字段名为Dep。

备注(SQL to Aggregate映射表)

下表主要是描述 SQL 关键字与 SequoiaDB 聚集操作符的对照表。

SQL关键字SequoiaDB聚集操作符
where$match
group by$group
having对 $group 后的字段进行 $match
select$project
order by$sort
top 或者 limit$limit
offset$skip

下表主要描述标准SQL语句与SequoiaDB聚集语句之间的对照。

SQL语句SequoiaDB语句描述
select product_id as p_id , price from foo.bardb.foo.bar.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: project:{p_id:"product_id",price:1,date:0}})返回所有记录的 product_id 和 price 字段,其中 product_id 重命名为 p_id,对记录中的 date 字段不返回。
select sum(price) as total from foo.bardb.foo.bar.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: …id:null,total:{sum:"$price"}}})对 table 中的字段 price 值求和,并重命名为 total。
select product_id, sum(price) as total from foo.bar group by product_iddb.foo.bar.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{ _id:"product_id",product_id:{ f i r s t : &quot; first:&quot; first:"product_id"},total:{ s u m : &quot; sum:&quot; sum:"price"}}})对表 table 中的记录按 product_id 字段分组;求每个分组中字段 price 值的累加和,并重命名为 total。
select product_id, sum(price) as total from foo.bar group by product_id order by totaldb.foo.bar.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"product_id",product_id:{ f i r s t : &quot; first:&quot; first:"product_id"},total:{ s u m : &quot; sum:&quot; sum:"price"}}},{$sort:{total:1}})对表 table 中的记录按 product_id 字段分组;求每个分组中字段 price 值的累加和,并重命名为 total;对结果集按字段名 total 的值升序排序。
select product_type_id, product_id, sum(price) as total from foo.bar group by product_type_id, product_iddb.foo.bar.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: …oduct_type_id:"product_type_id",product_id:“KaTeX parse error: Expected 'EOF', got '}' at position 12: product_id"}̲,product_id:{first:“KaTeX parse error: Expected 'EOF', got '}' at position 12: product_id"}̲,total:{sum:”$price”}}})对表 table 中的记录按首先按 product_type_id 字段分组,再按 product_id 字段分组;求每个分组中字段 price 值的累加和,并重命名为 total。
select product_id, sum(price) as total from foo.bar group by product_id having total > 1000db.foo.bar.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"product_id",product_id:{ f i r s t : &quot; first:&quot; first:"product_id"},total:{ s u m : &quot; sum:&quot; sum:"price"}}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: match:{total:{gt:1000}}})对表 table 中的记录按 product_id 字段分组;求每个分组中字段 price 值的累加和,并重命名为 total;只返回满足条件 total 字段值大于1000的分组。
select product_id, sum(price) as total from foo.bar where product_type_id = 1001 group by product_iddb.foo.bar.aggregate({KaTeX parse error: Expected 'EOF', got '}' at position 29: …t_type_id:1001}}̲,{group:{_id:“KaTeX parse error: Expected '}', got 'EOF' at end of input: …d",product_id:{first:“KaTeX parse error: Expected 'EOF', got '}' at position 12: product_id"}̲,total:{sum:”$price”}}})选择符合条件 product_type_id = 1001 的记录;对选出的记录按 product_id 进行分组;对每个分组中的 price 字段值就和,并重命名为 total。
select product_id, sum(price) as total from foo.bar where product_type_id = 1001 group by product_id having total > 1000db.foo.bar.aggregate({KaTeX parse error: Expected 'EOF', got '}' at position 29: …t_type_id:1001}}̲,{group:{_id:"KaTeX parse error: Expected '}', got 'EOF' at end of input: …d",product_id:{first:"KaTeX parse error: Expected 'EOF', got '}' at position 12: product_id"}̲,total:{sum:"KaTeX parse error: Expected 'EOF', got '}' at position 7: price"}̲}},{match:{total:{$gt:1000}}})选择符合条件 product_type_id = 1001 的记录;对选出的记录按 product_id 进行分组;对每个分组中的 price 字段值就和,并重命名为 total;只返回满足条件 total 字段值大于1000的分组。
select top 10 * from foo.bardb.foo.bar.aggregate({KaTeX parse error: Expected 'EOF', got '}' at position 17: …roup:{_id:null}}̲,{limit:10})返回结果集中的前10条记录。
select * from foo.bar offset 50 rows fetch next 10db.foo.bar.aggregate({KaTeX parse error: Expected 'EOF', got '}' at position 17: …roup:{_id:null}}̲,{skip:50},{$limit:10})跳过结果集中前50条记录之后,返回接下来的10条记录。
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值