python-pymongo-常用查询方法含聚合.

普通查询


按照所需字段排序.

db_set.find().sort("field_name ",pymongo.ASCENDING) --升序
db_set.find().sort("field_name ",pymongo.DESCENDING) --降序

查询数据库表的全部内容

# 第一种:
 db_set.find({})

#第二种:
db_set.find()

精确查询

db_set.find({“field_name”:”value”})
db_set.find({“field_name”:”value”, “field_name”:”value”})

只返回所需要的字段信息

find的第二参数可以帮助我们只把需要的键值信息返回,需要将我们需要的键指定为1,另外默认的”_id”默认是返回的,我们不需要它返回的话将它的值设为0

db_set.find({}, {“field_name_one”:1, “field_name_two”:1,”_id”:0})

比较查询

首先 $lt和<,$lte和<=,$gt和>,gte和>=,ne和!=是一一对应的

db_set.find({"field_name": {"$lt": value, "$gt": value}})

关联查询

如果只想查询一个键的多个值,或取除某个值之外所有的数据那么就用到了, $in和$nin

#比如我只想获取field_name为1,5,8的数据:

db_set.find({"field_name": {"$in": [1,5,8]}})
#如果想获取field_name为5之外的所有数据:

db_set.find({"field_name": {"$nin": [5]}})

多条件模糊查询

$regex为模糊查询的字符串提供正则表达式功能

db_set.find({"$or": [{"field_name": {'$regex': value}},{"field_name": {'$regex': value}}]})

聚合


语法格式: db.集合名称.aggregate([{管道:{表达式}}])

list_a = list(ROLE_TREE.aggregate([
    {"$group":
        {
            "_id": '$update_time',  # 我想查询的字段
            "counter": {"$sum": 1}  # 产生的数量
        }
    }
]))
print(list_a)

res= [{'_id': 1649238526, 'counter': 1}, {'_id': 1649405332, 'counter': 1}, {'_id': 1649405568, 'counter': 1}, {'_id': 1649237591, 'counter': 1}, {'_id': 1649237314, 'counter': 1}, {'_id': 1649405347, 'counter': 1}]


常用管道

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

常用表达式

  • $sum:计算总和,$sum:1同count表示计数
  • $avg:计算平均值
  • $min:获取最小值
  • $max:获取最大值
  • $push:在结果文档中插入值到一个数组中
  • $first:根据资源文档的排序获取第一个文档数据
  • $last:根据资源文档的排序获取最后一个文档数据

$group

将集合中的文档分组,可用于统计结果 ,_id表示分组的依据,使用某个字段的格式为’$字段’

list(ROLE_TREE.aggregate([
    {"$group":
        {
            "_id": '$update_time',  # 我想查询的字段
            "counter": {"$sum": 1}  # 产生的数量
        }
    }
]))
# "counter"为自定义名称,用来存储结果的变量

Group by null:将集合中所有文档分为一组

list_a = list(ROLE_TREE.aggregate([
    {"$group":
        {
            "_id": None,  # 为空
            "counter": {"$sum": 1}  # 产生的数量
        }
    }
]))
res = [{'_id': None, 'counter': 6}]

$match

用于过滤数据,只输出符合条件的文档,使用MongoDB的标准查询操作

list_a = list(ROLE_TREE.aggregate([
    {'$match': {'update_time': {'$lt': int(time.time())}}}, # 查找
    {'$group': {'_id': '$update_time', 'counter': {'$sum': 1}}} # 分组

]))
res= [{'_id': 1649405332, 'counter': 1}, {'_id': 1649405347, 'counter': 1}, {'_id': 1649237314, 'counter': 1}, {'_id': 1649237591, 'counter': 1}, {'_id': 1649405568, 'counter': 1}, {'_id': 1649238526, 'counter': 1}]
# ---------------------------------
list_a = list(ROLE_TREE.aggregate([
    {'$match': {'update_time': int(time.time())}},  # 查找
    # {'$group': {'_id': '$update_time', 'counter': {'$sum': 1}}} # 分组

]))
res = []

$project

修改输入文档的结构,如重命名、增加、删除字段、创建计算结果(类似查找中投影,值为1表示显示,值为0不显示)

list_a = list(ROLE_TREE.aggregate([
    {"$project": {"_id": 1, "create_time": 1, 'org_id': 1}},  # _id  一定在前
    {'$group': {'_id': '$create_time', 'counter': {'$sum': 1}}}  # 分组

]))
res = [{'_id': 1649237314, 'counter': 1}, {'_id': 1649405347, 'counter': 1}, {'_id': 1649405568, 'counter': 1}, {'_id': 1649238526, 'counter': 1}, {'_id': 1649405332, 'counter': 1}, {'_id': 1649237591, 'counter': 1}]

$sort

将输入文档排序后输出 ,1 升序 -1降序

list_a = list(ROLE_TREE.aggregate([
    {'$sort': {'update_time': 1}},
    {"$project": {"_id": 0, 'update_time': 1}}

]))
res = [{'update_time': 1649237314}, {'update_time': 1649237591}, {'update_time': 1649238526}, {'update_time': 1649405332}, {'update_time': 1649405347}, {'update_time': 1649405568}]

$limit

限制聚合管道返回的文档数

list_a = list(ROLE_TREE.aggregate([
    {"$limit": 2},
    {"$project": {'_id': 1}}

]))
res=  [{'_id': '8796e0d2a75ee0d84c1fbcb5ac4e7cc5'}, {'_id': 'bbd7664aa4b1d7fbfaa0256b05a78fd1'}]

$skip

跳过指定数量的文档,并返回余下的文档

list_a = list(ROLE_TREE.aggregate([
    {"$skip": 3},  # 跳过
    {"$project": {'_id': 1}}

]))
res = [{'_id': '26702db7682ef817047b9681cd685987'}, {'_id': 'b64f271c735c8a57b34a0feefd292d65'}, {'_id': '5974aaaf7848fd8af6426cad375c4b62'}]

先写skip,再写limit

list_a = list(ROLE_TREE.aggregate([
    {"$skip": 1},  # 跳过
    {"$limit": 1},  # 分组数量
    {"$project": {'_id': 1}}

]))
res = [{'_id': 'bbd7664aa4b1d7fbfaa0256b05a78fd1'}]
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值