欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python-pymongo常用查詢(xún)方法含聚合問(wèn)題

 更新時(shí)間:2023年05月08日 09:28:48   作者:L'y  
這篇文章主要介紹了python-pymongo常用查詢(xún)方法含聚合問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

普通查詢(xún)

按照所需字段排序

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

查詢(xún)數(shù)據(jù)庫(kù)表的全部?jī)?nèi)容

# 第一種:
 db_set.find({})
#第二種:
db_set.find()

精確查詢(xún)

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

只返回所需要的字段信息

find的第二參數(shù)可以幫助我們只把需要的鍵值信息返回,需要將我們需要的鍵指定為1,另外默認(rèn)的”_id”默認(rèn)是返回的,我們不需要它返回的話(huà)將它的值設(shè)為0

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

比較查詢(xún)

首先 $lt和<,$lte和<=,$gt和>,gte和>=,ne和!=是一一對(duì)應(yīng)的

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

關(guān)聯(lián)查詢(xún)

如果只想查詢(xún)一個(gè)鍵的多個(gè)值,或取除某個(gè)值之外所有的數(shù)據(jù)那么就用到了, $in和$nin

#比如我只想獲取field_name為1,5,8的數(shù)據(jù):
db_set.find({"field_name": {"$in": [1,5,8]}})
#如果想獲取field_name為5之外的所有數(shù)據(jù):
db_set.find({"field_name": {"$nin": [5]}})

多條件模糊查詢(xún)

$regex為模糊查詢(xún)的字符串提供正則表達(dá)式功能

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

聚合

語(yǔ)法格式:db.集合名稱(chēng).aggregate([{管道:{表達(dá)式}}])

list_a = list(ROLE_TREE.aggregate([
    {"$group":
        {
            "_id": '$update_time',  # 我想查詢(xún)的字段
            "counter": {"$sum": 1}  # 產(chǎn)生的數(shù)量
        }
    }
]))
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:將集合中的文檔分組,可用于統(tǒng)計(jì)結(jié)果
  • $match:過(guò)濾數(shù)據(jù),只輸出符合條件的文檔
  • $project:修改輸入文檔的結(jié)構(gòu),如重命名、增加、刪除字段、創(chuàng)建計(jì)算結(jié)果
  • $sort:將輸入文檔排序后輸出
  • $limit:限制聚合管道返回的文檔數(shù)
  • $skip:跳過(guò)指定數(shù)量的文檔,并返回余下的文檔

常用表達(dá)式

  • $sum:計(jì)算總和,$sum:1同count表示計(jì)數(shù)
  • $avg:計(jì)算平均值
  • $min:獲取最小值
  • $max:獲取最大值
  • $push:在結(jié)果文檔中插入值到一個(gè)數(shù)組中
  • $first:根據(jù)資源文檔的排序獲取第一個(gè)文檔數(shù)據(jù)
  • $last:根據(jù)資源文檔的排序獲取最后一個(gè)文檔數(shù)據(jù)

$group

將集合中的文檔分組,可用于統(tǒng)計(jì)結(jié)果 ,_id表示分組的依據(jù),使用某個(gè)字段的格式為’$字段’

list(ROLE_TREE.aggregate([
    {"$group":
        {
            "_id": '$update_time',  # 我想查詢(xún)的字段
            "counter": {"$sum": 1}  # 產(chǎn)生的數(shù)量
        }
    }
]))
# "counter"為自定義名稱(chēng),用來(lái)存儲(chǔ)結(jié)果的變量

Group by null:將集合中所有文檔分為一組

list_a = list(ROLE_TREE.aggregate([
    {"$group":
        {
            "_id": None,  # 為空
            "counter": {"$sum": 1}  # 產(chǎn)生的數(shù)量
        }
    }
]))
res = [{'_id': None, 'counter': 6}]

$match

用于過(guò)濾數(shù)據(jù),只輸出符合條件的文檔,使用MongoDB的標(biāo)準(zhǔn)查詢(xún)操作

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

修改輸入文檔的結(jié)構(gòu),如重命名、增加、刪除字段、創(chuàng)建計(jì)算結(jié)果(類(lèi)似查找中投影,值為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

限制聚合管道返回的文檔數(shù)

list_a = list(ROLE_TREE.aggregate([
    {"$limit": 2},
    {"$project": {'_id': 1}}
]))
res=  [{'_id': '8796e0d2a75ee0d84c1fbcb5ac4e7cc5'}, {'_id': 'bbd7664aa4b1d7fbfaa0256b05a78fd1'}]

$skip

跳過(guò)指定數(shù)量的文檔,并返回余下的文檔

list_a = list(ROLE_TREE.aggregate([
    {"$skip": 3},  # 跳過(guò)
    {"$project": {'_id': 1}}
]))
res = [{'_id': '26702db7682ef817047b9681cd685987'}, {'_id': 'b64f271c735c8a57b34a0feefd292d65'}, {'_id': '5974aaaf7848fd8af6426cad375c4b62'}]

先寫(xiě)skip,再寫(xiě)limit

list_a = list(ROLE_TREE.aggregate([
    {"$skip": 1},  # 跳過(guò)
    {"$limit": 1},  # 分組數(shù)量
    {"$project": {'_id': 1}}
]))
res = [{'_id': 'bbd7664aa4b1d7fbfaa0256b05a78fd1'}]

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論