MongoDB----数据查询 聚合操作

目录

1、数据查询

1.1 find方法

1.1.1  find方法

1.1.2  findOne方法

 1.1.3  pretty方法

1.2  比较运算符

1.2.1  等于

1.2.2  小于

1.2.3  小于等于

1.2.4  大于

1.2.5  大于等于

1.2.6  不等于

1.3 范围运算符

1.3.1 in

1.4 逻辑运算符 

1.4.1 并且查询

 1.4.2 或者查询

1.5 正则表达式查询

1.5.1  //

1.5.2  regex

1.6 limit 与 skip

1.6.1  limit方法

1.6.2  skip方法

1.7 自定义查询

1.7.1 where

1.8 投影

1.9 排序

1.9.1  sort

1.10 记录统计

1.10.1 count 

1.11 数据去重

1.11.1 distinct

 2. 聚合操作

 2.1 常用管道

2.1.1  group

2.1.2  match

2.1.3  project

2.1.4  sort

2.1.5  limit

2.1.6  skip

2.1.7  unwind

2.2 表达式

2.2.1 sum

2.2.2 avg

 2.2.3 min

2.2.4 max

2.2.5  push

2.2.6  first

2.2.7  last


1、数据查询

1.1 find方法

1.1.1  find方法

find():普通方法

db.集合名称.find({条件文档})
db.collection.find({age:20})

1.1.2  findOne方法

findOne():查询一个

db.集合名称.findOne({条件文档})
db.collection.findOne({age:18})

 1.1.3  pretty方法

pretty(): 将结果格式化

db.集合名称.find({条件文档}).pretty()
db.collection.find().pretty()

1.2  比较运算符

1.2.1  等于

等于:默认是等于判断,没有运算符

db.collection.find({age: 18})

1.2.2  小于

小于:$lt

db.collection.find({age: {$lt: 18}})

1.2.3  小于等于

小于等于:$lte

db.stu_info.find({age: {$lte: 18}})

1.2.4  大于

大于:$gt

db.collection.find({age: {$gt: 18}})

1.2.5  大于等于

大于等于:$gte

db.collection.find({age: {$gte: 18}})

1.2.6  不等于

不等于:$ne

db.collection.find({age: {$ne: 18}})

1.3 范围运算符

1.3.1 in

$in:进行返回查询 符合18或28的返回

db.collection.find({age: {$in: [10, 18]}})

1.4 逻辑运算符 

1.4.1 并且查询

并且查询: db.集合名称.find({查询条件})

db.collection.find({age: 18, country: "china"})

 1.4.2 或者查询

// 或者查询 
db.集合名称.find({$or:[{查询条件}, {查询条件}]})

1.5 正则表达式查询

使用//或$regex编写正则表达式

1.5.1  //

db.collection.find({sku:/^abc/})

1.5.2  regex

db.collection.find({sku: {$regex: "789$"}})

1.6 limit 与 skip

1.6.1 limit方法

limit(): ⽤于读取指定数量的⽂档

db.集合名称.find().limit(NUMBER)

// 查询两条信息
db.collection.find().limit(2)

1.6.2   skip方法

skip():查询除了前两条之外的所有记录

// 查询除了前两条之外的所有记录
db.collection.find().skip(2)

// 组合使用
db.collection.find().skip(2).limit(2)

1.7 自定义查询

1.7.1 where

$where: 使⽤$where后⾯写⼀个函数, 返回满⾜条件的数据


// 查询年龄⼤于30的学⽣
db.collection.find({$where: function(){return this.age<=18}})

1.8 投影

// 如果不想显示_id,需要单独设置_id: 0
// db.集合名称.find({条件}, {字段名称1, 字段名称2...})

/*example*/
db.collection.find({age: {$gt: 18}}, {name: 1, _id: 0})

// 无条件进行投影查询
db.collection.find({}, {name: 1, _id: 0})

1.9 排序

1.9.1  sort

sort : 用于对集合进行排序

// db.集合名称.find().sort({字段: 1})

// 1为升序 -1为降序
db.collection.find().sort({age: -1})

// 多条件排序
db.collection.find().sort({age: -1, gender: -1})

// 筛选年龄大于18并使用age进行正序排序
db.collection.find({age: {$gt: 18}}).sort({age: 1})

1.10 记录统计

1.10.1 count 

count :记录统计

// 统计学生个数
db.collection.find().count()

// 统计年龄大于18的学生个数
db.collection.find({age: {$gt: 18}}).count()

// 第二种写法
db.collection.count()
db.collection.count({age: {$gt: 18}})

1.11 数据去重

1.11.1 distinct

distinct:数据去重

// 对地址进行去重
db.collection.distinct("hometown")

// 结合条件查询去重
db.collection.distinct("hometown", {age: {$gt: 20}})

 2. 聚合操作

聚合(aggregate)是基于数据处理的聚合管道,每个文档通过一个由多个阶段(stage)组成的管道,可以对每个阶段的管道进行分组、过滤等功能,然后经过一系列的处理,输出相应的结果。

 db.集合名称.aggregate({管道:{表达式}})

 2.1 常用管道

$group: 
  将集合中的⽂档分组, 可⽤于统计结果
$match: 
  过滤数据, 只输出符合条件的⽂档
$project: 
  修改输⼊⽂档的结构, 如重命名、增加、删除字段、创建计算结果
$sort: 
  将输⼊⽂档排序后输出
$limit: 
  限制聚合管道返回的⽂档数
$skip: 
  跳过指定数量的⽂档, 并返回余下的⽂档
$unwind: 
  将数组类型的字段进⾏拆分

2.1.1  group

$group:将集合中的⽂档分组, 可⽤于统计结果 

db.collection.aggregate(
  {
    $group: {_id: "$gender", counter: {$sum: 1}}
  }
)

2.1.2 match

$match:过滤数据, 只输出符合条件的⽂档

db.collection.aggregate(
  { $match: { status: "A" } }
)

2.1.3 project

$project: 修改输⼊⽂档的结构, 如重命名、增加、删除字段、创建计算结果

db.collection.aggregate(
  {
    $project: {_id: 0, name: 1, age: 1}
  }
)

2.1.4 sort

$sort: 将输⼊⽂档排序后输出

db.collection.aggregate(
  { $sort: { total: -1 } }
)

2.1.5 limit

$limit:限制聚合管道返回的⽂档数

db.collection.aggregate([
  { $match: { status: "A" } },
  { $limit: 5 }
])

 2.1.6 skip

$skip:跳过指定数量的⽂档, 并返回余下的⽂档

db.collection.aggregate([
  { $match: { status: "A" } },
  { $skip: 5 },
  { $limit: 10 }
])

2.1.7 unwind

 $unwind:将数组类型的字段进⾏拆分

db.collection.aggregate([
  { $unwind: "$arrayField" }
])

2.2 表达式

2.2.1 sum

$sum: 计算总和, $sum:1 表示以⼀倍计数

db.collection.aggregate(
  {
    $group: {_id: "$gender", counter: {$sum: 1}}
  }
)

2.2.2 avg

$avg: 计算平均值

db.collection.aggregate(
  {
    $group: {_id: "$gender", count: {$sum: 1}, avg_age: {$avg: "$age"}}
  }
)

 2.2.3 min

$min: 获取最⼩值

db.collection.aggregate([
  { $group: { _id: null, minField: { $min: "age" } } }
])

2.2.4 max

 $max: 获取最⼤值

db.collection.aggregate([
  { $group: { _id: null, minField: { $max: "$fieldName" } } }
])

2.2.5  push

$push: 在结果⽂档中插⼊值到⼀个数组中

2.2.6  first

$first: 根据资源⽂档的排序获取第⼀个⽂档数据

2.2.7  last

$last: 根据资源⽂档的排序获取最后⼀个⽂档数据

  • 29
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值