用python查询mongodb数据库时查询语句的调用

# -*- coding: utf-8 -*-
# @Time    : 2024/1/15 14:37
# @Author  : hjcui
# @Site    : 
# @File    : mongodb.py
# @Software: PyCharm
import time,os
import datetime
import pymongo
from collections import Counter
from pymongo import MongoClient
import requests

dbName = 'target_507'
tele_dir = r'.\telephone'

# 查询120个集合
for i in range(0,120):
    num = '%04d' % i
    table = 'speaker_{}'.format(num)
    mongo = MongoClient("mongodb://172.5.20.4:27017/")
    database = mongo[dbName]
    collection = database[table]
    
    query = {}
    # 查询列表长度大于2的记录
    query["$where"] = u"this.languageList.length > 2"
    query["createTime"] = {
        u"$gt": 1705593600
    }
    # 根据文档中的字段查询,projection在python脚本中的用法
    projection = {}
    projection["telNum"] = 1.0
    projection["audioMetaList"] = 1.0
    projection["createTime"] = 1.0
    projection["languageList"] = 1.0
    sort = [("createTime", 1)]

    cursor = collection.find(query, projection=projection, sort=sort).limit(1000)
    for metalist in cursor:
        root = metalist['audioMetaList']
        for num in range(len(root)):
            print(metalist['telNum'],metalist['createTime'],root[num]['filepath'])
            link = root[num]['filepath']
            filename = os.path.basename(root[num]['filepath'])
            tele_num = metalist['telNum']
            storage_path = os.path.join(tele_dir,tele_num)
            response = requests.get(url=link)
            if not os.path.exists(storage_path):
                os.makedirs(storage_path)
            with open(os.path.join(storage_path,filename),'wb') as ff:
                ff.write(response.content)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值