IT记录

日常学习、工作的点滴记录


  • 首页

  • categories

  • archive

  • tags

  • 搜索

pymongo查询某字段是否存在

发表于 2021-10-09   |   分类于 Python , 数据库   |   暂无评论
import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient["gaokao"]
mycol = mydb["college"]    
colleges = mycol.find({'code':{'$exists': 'true'}},{ "_id": 0, "name": 1, "code": 1 })

在MongoDB管理程序中,{'$exists': 'true'}不需要使用括号。

Mysql中替代字段中的字符

发表于 2021-10-09   |   分类于 数据库   |   暂无评论

UPDATE college SET NAME=REPLACE(NAME,'(','(');

Debian 9 Stretch 跨版本升级到 Debian 10 Buster

发表于 2021-10-09   |   分类于 系统应用   |   暂无评论

为Debian系统做升级准备

确保系统为最新的Debian 9 Stretch

#apt-get update
#apt-get upgrade
#apt-get dist-upgrade

更换Debian 10 Buster清华源

$ sudo apt install apt-transport-https ca-certificates

修改sources.list文件

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free

升级到 Debian Buster

# apt-get upgrade
# apt-get dist-upgrade

查看版本:

# lsb_release -a

docker安装并配置修改使用阿里云镜像仓库

发表于 2021-10-09   |   分类于 系统应用   |   暂无评论

卸载原先安装的docker版本

sudo apt-get remove docker docker-engine docker.io

安装依赖

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

获取信任 Docker 的 GPG 公钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

添加软件仓库,并安装

sudo add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce

配置修改使用阿里云镜像仓库

进入阿里云的控制面板,“产品与服务”中选择“容器镜像服务”,开通“镜像加速器”,并根据提供的极速器地址,修改docker配置:

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://编号.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

Python操作Dynamo

发表于 2021-10-09   |   分类于 Python   |   暂无评论

创建Dynamo表

import boto3
dynamodb = boto3.resource('dynamodb')

# Create the DynamoDB table.
table = dynamodb.create_table(
    TableName='waihui',
    
    AttributeDefinitions=[        
        {
            'AttributeName': 'code',
            'AttributeType': 'S'
        }
        
        
    ],
    KeySchema=[
        {
            'AttributeName': 'code',
            'KeyType': 'HASH'
        }
        
    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 5,
        'WriteCapacityUnits': 5
    }
    
)
# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='waihui')
# Print out some data about the table.
print(table.item_count)

注意事项:1.AttributeDefinitions与KeySchema项目保持一致。

2.ProvisionedThroughput为系统默认,必须设置

Dynamo表中插入项目

import boto3
import decimal
# Get the service resource.
dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('waihui')

table.put_item(
   Item={
        'code': 'USDCAD',
        'high': Decimal('1.3200'),
        'low': Decimal('1.3000'),
    }
)

如果插入项目中数值为小数,需要使用Decimal转换为Dynamo数据类型N。

获取单一项目

import boto3    
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('waihui')
response = table.get_item(
   Key={
        'code': 'USDCAD'        
    }
)
item = response['Item']
print(item)

更新项目

import boto3
import decimal
# Get the service resource.
dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('waihui')
table.update_item(
    Key={
        'code': 'USDCAD'
    },
    UpdateExpression='SET low = :val1',
    ExpressionAttributeValues={
        ':val1': Decimal('1.2980')
    }
)

批量更新项目

with table.batch_writer() as batch:
    for i in range(50):
        batch.put_item(
            Item={
                'account_type': 'anonymous',
                'username': 'user' + str(i),
                'first_name': 'unknown',
                'last_name': 'unknown'
            }
        )

删除项目

import boto3
# Get the service resource.
dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('waihui')

table.delete_item(
   Key={
        'code': 'USDCAD'        
    }
)

删除表

import boto3
# Get the service resource.
dynamodb = boto3.resource('dynamodb')    
table = dynamodb.Table('waihui')
table.delete()

扫描查询

response = table.scan(
    FilterExpression=Attr('first_name').begins_with('J') & Attr('account_type').eq('super_user')
)
items = response['Items']
print(items)

可以设定记录中的key值为查询扫描条件

response = table.scan(
    FilterExpression=Attr('address.state').eq('CA')
)
items = response['Items']
print(items)
1...14151617181920212223

一个高端大气上档次的网站

115 文章
5 分类
51 标签
GitHub 知乎 V2EX SF
© 2026 IT记录
Typecho
主题 - NexT.Pisces