当前位置: 首页 > news >正文

wordpress上传视频黑屏企业网站优化服务主要围绕什么

wordpress上传视频黑屏,企业网站优化服务主要围绕什么,广告设计电商设计网页设计,网站建设中心怎么做嗨害大家好鸭!我是芝士❤ 一、前言 MongoDB属于 NoSQL(非关系型数据库), 是一个基于分布式文件存储的开源数据库系统。 二、操作 MongoDB 1. 安装 pymongo python 使用第三方库来连接操作 MongoDB, 所以我们首先安…

嗨害大家好鸭!我是芝士❤

在这里插入图片描述

一、前言

MongoDB属于 NoSQL(非关系型数据库),
是一个基于分布式文件存储的开源数据库系统。

二、操作 MongoDB

1. 安装 pymongo

python 使用第三方库来连接操作 MongoDB,
所以我们首先安装此库。

pip3 install pymongodb

2. 连接 MongoDB

使用 MongoClient 类连接,
以下两种参数方式都可以:

from pymongo import MongoClient# 连接方式一
client = MongoClient(host='localhost',port=27017)
# 连接方式二
# client = MongoClient('mongodb://localhost:27017/学习资料无偿领扣扣qun:540305994')

3. 选择数据库

MongoDB 可以创建很多 db,
指定我们需要的 db 即可

# 方式一
db = client.Monitor
# 方式二
# db = client['Monitor']

4. 选择集合

db 内包含很多个集合,
有点类似 mysql 这类关系型数据库中的表

# 方式一
collection = db.test
# 方式二
# collection = db['test']

5. 插入数据

插入一条数据,
MongoDB 每条记录都有一个唯一标识。
返回一个 InsertOneResult 对象,
若需要获取唯一标识,
找到 InsertOneResult 对象的属性 inserted_id 即可

from pymongo import MongoClientclass mongodb:def __init__(self,host,db,port = 27017):''':param host: str mongodb地址:param db: str 数据库:param port: int 端口,默认为27017'''host = hostdb = dbself.port = portclient = MongoClient(host=host,port=port)self.db = client[db]def insert_one(self,table,dic):''':param table: str 数据库中的集合:param dic: dict 要插入的字典:return: 返回一个包含ObjectId类型的对象'''collection = self.db[table]rep = collection.insert_one(dic)return rep
if __name__=='__main__':dic = {'姓名':'小明','English':100,'math':90}db = mongodb(host='localhost',db = 'test')rep = db.insert_one('test',dic)print(rep.inserted_id)

插入多条数据,使用 insert_many 批量插入

from pymongo import MongoClientclass mongodb:def __init__(self,host,db,port = 27017):''':param host: str mongodb地址:param db: str 数据库:param port: int 端口,默认为27017'''host = hostdb = dbself.port = portclient = MongoClient(host=host,port=port)self.db = client[db]def insert_one(self,table,dic):''':param table: str 数据库中的集合:param dic: dict 要插入的字典:return: 返回包含一个ObjectId类型的对象'''collection = self.db[table]rep = collection.insert_one(dic)return repdef insert_many(self,table,lists):''':param table: str 数据库中的集合:param dic: dict 要插入的列表,列表中的元素为字典:return: 返回包含多个ObjectId类型的列表对象'''collection = self.db[table]rep = collection.insert_many(lists)return repif __name__=='__main__':lists = [{'姓名':'小明','English':100,'math':90},{'姓名':'小华','English':90,'math':100}]db = mongodb(host='localhost',db = 'test')rep = db.insert_many('test',lists)for i in rep.inserted_ids:print(i)

6. 查询

1)常规查询

  • find_one :查询单条记录,返回一个字典。
  • find:查询多条记录 ,返回一个游标对象。
from pymongo import MongoClientclass mongodb:def __init__(self,host,db,port = 27017):''':param host: str mongodb地址:param db: str 数据库:param port: int 端口,默认为27017'''host = hostdb = dbself.port = portclient = MongoClient(host=host,port=port)self.db = client[db]def find_one(self,table,dic):''':param table: str 数据库中的集合:param dic: dict 查询条件:return: dict 返回单条记录的字典'''collection = self.db[table]rep = collection.find_one(dic)return repdef find(self,table,dic):''':param table: str 数据库中的集合:param dic: dict 查询条件:return: list 返回查询到记录的列表'''collection = self.db[table]rep = list(collection.find(dic))return repif __name__=='__main__':# 查询 English 成绩为 100 的所有记录dic = {'English':100}db = mongodb(host='localhost',db = 'test')rep = db.find('test',dic)print(rep)

2)范围查询

有时候我们需要范围比较查询,
比如要查询 English 成绩为 80~90 ,
可以使用比较符:dic = {'English':{'$in':[80,90]}}

  • $lt :小于
  • $lte:小于等于
  • $gt:大于
  • $gte:大于等于
  • $ne:不等于
  • $in:在范围内
  • $nin:不在范围内

3)模糊查询

如果想要类似这种 sql 语句查询:
select * from db where name like '%abc%'
在 MongoDB 中可以通过正则表达式来实现

# 模糊搜索key为"姓名",value包含"明"的记录
dic = {'姓名':{'$regex':'明'}}
rep = list(collection.find(dic))

4)字段筛选

如果只想返回指定的字段,使用 find(find_one)方法时加上 projection 参数。类似 sql 语法:select Englist from db where name like ‘%abc%’

# projection字典参数。1显示,0不显示
# 以下查询结果只返回key为English的相关字段
rep = list(collection.find(dic,projection={'English':1,'_id':0}))

5)计数

直接调用 count() 方法,返回一个 int 类型的数字

# 计数查询只需要在普通查询后加上 count() 即可
count = collection.find().count()  
# count = collection.find({'English':{'$gt':90}}).count()

6)排序

排序时,直接调用sort()方法,并在其中传入排序的字段及升降序标志,返回一个游标对象# 正序 ASCENDING,倒序 DESCENDING。list()将游标对象转成列表
data = list(collection.find(dic).sort('姓名',pymongo.DESCENDING))

7. 更新数据

首选查到需要更新的数据,然后将该数据更新,返回一个 UpdataResult 对象, raw_result 属性中包含 update 生效的个数。

  • update_one:更新查询到的第一条数据
  • update_many:更新多条数据
from pymongo import MongoClientclass mongodb:def __init__(self,host,db,port = 27017):''':param host: str mongodb地址:param db: str 数据库:param port: int 端口,默认为27017'''host = hostdb = dbself.port = portclient = MongoClient(host=host,port=port)self.db = client[db]def update_one(self,table,condition,dic):''':param table: str 数据库中的集合:param condition: dict 查询条件:param dic: dict 更新的数据:return: 返回UpdateResult对象'''collection = self.db[table]# $set 表示只更新dic字典内存在的字段rep = collection.update_one(condition,{'$set':dic})# 会把之前的数据全部用dic字典替换,如果原本存在其他字段,则会被删除# rep = collection.update_one(condition, dic)return repdef update_many(self,table,condition,dic):''':param table: str 数据库中的集合:param condition: dict 查询条件:param dic: dict 更新的数据:return:返回UpdateResult对象'''collection = self.db[table]# $set 表示只更新dic字典内存在的字段rep = collection.update_many(condition,{'$set':dic})# 会把之前的数据全部用dic字典替换,如果原本存在其他字段,则会被删除# rep = collection.update_many(condition, dic)return repif __name__=='__main__':condition = {'English':80}dic = {'English':60}db = mongodb(host='mongodb-monitor.monitor.svc.test.local',db = 'test')rep = db.update_one('test',condition,dic)print(rep.raw_result)# 输出 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

8. 删除

删除和 update 类似,删除数据后,返回一个 DeleteResult 对象, raw_result 属性中包含 delete 的个数

  • delete_one:删除查询到的第一条数据
  • delete_many:批量删除符合查询条件的数据
from pymongo import MongoClientclass mongodb:def __init__(self,host,db,port = 27017):''':param host: str mongodb地址:param db: str 数据库:param port: int 端口,默认为27017'''host = hostdb = dbself.port = portclient = MongoClient(host=host,port=port)self.db = client[db]def delete_one(self,table,dic):''':param table: str 数据库中的集合:param dic: dict 查询条件:return: 返回DeleteResult对象'''collection = self.db[table]rep = collection.delete_one(dic)return repdef delete_many(self,table,dic):''':param table: str 数据库中的集合:param dic: dict 查询条件:return: 返回DeleteResult对象'''collection = self.db[table]rep = collection.delete_many(dic)return repif __name__=='__main__':dic = {'English':60}db = mongodb(host='localhost',db = 'test')rep = db.delete_many('test',dic)print(rep.raw_result)# 输出 {'n': 21, 'ok': 1.0}

今天的文章就是这样~

我们下篇文章再见啦(✿◡‿◡)

在这里插入图片描述

http://www.yayakq.cn/news/430179/

相关文章:

  • 贵阳网页网站制作网站建设的相关新闻
  • 高密哪里有做网站的网页设计模板图片手绘
  • wordpress适用于图片站的主题福建省建设人才与科技发展中心网站
  • 做网站的天空网群排名优化软件
  • 毕业设计做网站选题网站设计流程大致分为几个阶段
  • 软文范例大全100字seo的内容主要有哪些方面
  • 个人做网站哪种类型的网站好怎么做网站赚钱软件
  • 开发个网站开票名称是什么意思网页制作代码示例
  • 外贸网站支付系统做网站像素大小
  • 网站 维护方案光谷网站开发
  • 制作模板网站牙科网站模板
  • 做室内概念图的网站注册网站的步骤
  • 温州企业网站开发wordpress资讯站模板
  • 电子商务网站建设的开发流程做网站的公司主要是干啥
  • 网站建设的公司选择哪家好南昌建站费用
  • 建一个个人网站一年多少钱合肥网站搭建公司哪家好
  • 基础微网站开发咨询263net企业邮箱
  • 建设一个网站费用专门做水产海鲜的网站吗
  • 网站有什么到期凡客v网上商城
  • 成都中方互动做网站怎样网站内页如何做排名
  • eclipse网站开发环境搭建wordpress导航主题
  • 网站建设策划方案怎么写wordpress 检索插件
  • 惠州市seo网站设计台州建站网站模板
  • 做网站需要多少钱济宁西宁网络推广服务网
  • div使用太多影响网站收录哪里不好就去建设
  • 大型公司网站建设有没有专门做渔具的网站
  • 自己如何做一个网络平台泰安网站优化公司
  • 做片头网站网站开发开票内容
  • 哪里的佛山网站建设南充做网站略奥网络
  • 中国装修网官方网站怎样建设论坛网站