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

做的页面好看的网站百度移动

做的页面好看的网站,百度移动,网站建设构建方案,昌乐建设局网站本文内容主要为使用Python 对Mongodb数据库的一些基本操作整理。 目录 安装类库 操作实例 引用类库 连接服务器 连接数据库 添加文档 添加单条 批量添加 查询文档 查询所有文档 查询部分文档 使用id查询 统计查询 排序 分页查询 更新文档 update_one方法 upd…

本文内容主要为使用Python 对Mongodb数据库的一些基本操作整理。

目录

安装类库

操作实例

引用类库

连接服务器

连接数据库

添加文档

添加单条

批量添加

查询文档

查询所有文档

查询部分文档

使用id查询

统计查询

排序

分页查询

更新文档

update_one方法

update_many方法

删除文档

delete_one方法

delete_many方法

总结


安装类库

打开命令行执行以下命令:

pip install pymongo

安装过程如下:

操作实例

引用类库

首先需要引入mongodb的操作类库,示例如下:

from pymongo import MongoClient

连接服务器

conn = MongoClient('localhost', 27017)

连接数据库

db = conn.mydb

添加文档

添加单条

使用insert_one()方法,添加一个学生记录。

示例如下:

from pymongo import MongoClientconn = MongoClient('localhost', 27017)db = conn.mydb
student = db.student
student.insert_one({'name': 'zhangsan', 'age': 20, 'gender': 1, 'address': '北京海淀区', 'isDel': 0})# 关闭
conn.close()

批量添加

使用insert_many()方法,添加多个学生记录。

示例如下:

from pymongo import MongoClientconn = MongoClient('localhost', 27017)db = conn.mydb
collection = db.student
# 批量
collection.insert_many([{'name': '李四', 'age': 18, 'gender': 0, 'address': '北京海淀区', 'isDel': 0},{'name': '王五', 'age': 21, 'gender': 1, 'address': '北京昌平区', 'isDel': 0},{'name': '赵六', 'age': 19, 'gender': 0, 'address': '北京朝阳区', 'isDel': 0}
])# 关闭
conn.close()

查询文档

使用查询方法,查询刚才插入的数据。根据查询条件不同分为以下类型。

查询所有文档

没有查询条件即查询集合中所有记录。

示例如下:

from pymongo import MongoClientconn = MongoClient('localhost', 27017)collection = conn.mydb.studentres = collection.find()
for row in res:print(row)print(type(row))conn.close()

执行结果:

 

查询部分文档

通过设置查询条件为小于20岁的学生,来查询符合条件的部分数据。

示例如下:

res = collection.find({'age': {'$gt': 20}})
for row in res:print(row)print(type(row))

执行结果:

 

使用id查询

使用id查询与mysql不同,需要使用id生成器来转化id字符串后在进行查询。

示例如下:

from bson.objectid import ObjectId
info = collection.find({'_id':ObjectId('666bbb5b8d4817f169319d61')})
print(info)
print(type(info))
print(info[0])

打印为对象类型,可获取其第一个元素。

执行结果:

 

统计查询

对符合查询条件的记录进行数量统计。

示例如下:

res = collection.count_documents({'age': {'$gte': 20}})
print(res)

执行结果:

3

排序

默认升序 pymongo.DESCENDING倒序。

示例如下:

import pymongo
from pymongo import MongoClientconn = MongoClient('localhost', 27017)collection = conn.mydb.student# 默认升序 pymongo.DESCENDING倒序
res = collection.find().sort('age', pymongo.DESCENDING)
for row in res:print(row)

分页查询

通过skip()和limit()方法实现分页。

示例如下:

from pymongo import MongoClientconn = MongoClient('localhost', 27017)collection = conn.mydb.studentres = collection.find().skip(2).limit(5)
for row in res:print(row)

更新文档

update_one方法

只会修改符合条件的第一条记录。

示例如下:

info = collection.update_one({'name': 'zhangsan'}, {'$set': {'name': '李雷'}})
print(info)

执行结果:

# 修改成功
# UpdateResult({'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}, acknowledged=True)
# 没有找到符合记录,未修改
# UpdateResult({'n': 0, 'nModified': 0, 'ok': 1.0, 'updatedExisting': False}, acknowledged=True)

update_many方法

会修改所有符合条件的记录。

示例如下:

info = collection.update_many({'name': '李四'}, {'$set': {'name': '李武'}})
print(info)

执行结果:

UpdateResult({'n': 2, 'nModified': 2, 'ok': 1.0, 'updatedExisting': True}, acknowledged=True)

删除文档

删除文档也有两个方法。

delete_one方法

删除符合条件的第一条记录。

示例如下:

info = collection.delete_one({'name': '李雷'})
print(info)

delete_many方法

删除符合条件的所有记录。

示例如下:

info = collection.delete_many({'age': {'$gte': 20}})
print(info)

执行结果:

DeleteResult({'n': 2, 'ok': 1.0}, acknowledged=True)

总结

本文内容主要为使用Python 对Mongodb数据库的一些基本操作整理。


文章转载自:
http://pancratium.rpwm.cn
http://gadgetry.rpwm.cn
http://scsi.rpwm.cn
http://yucca.rpwm.cn
http://infelicific.rpwm.cn
http://disinhume.rpwm.cn
http://elbowchair.rpwm.cn
http://stagnate.rpwm.cn
http://overthrew.rpwm.cn
http://tempo.rpwm.cn
http://nih.rpwm.cn
http://shall.rpwm.cn
http://smoke.rpwm.cn
http://spacewoman.rpwm.cn
http://noveletish.rpwm.cn
http://spaniard.rpwm.cn
http://undro.rpwm.cn
http://raucousness.rpwm.cn
http://luxe.rpwm.cn
http://reflet.rpwm.cn
http://snead.rpwm.cn
http://tentability.rpwm.cn
http://supervisal.rpwm.cn
http://sulfhydrate.rpwm.cn
http://imitate.rpwm.cn
http://cornhusk.rpwm.cn
http://cacuminal.rpwm.cn
http://planification.rpwm.cn
http://rate.rpwm.cn
http://sergeancy.rpwm.cn
http://phytogeny.rpwm.cn
http://lithium.rpwm.cn
http://chorist.rpwm.cn
http://ozarkian.rpwm.cn
http://nomadize.rpwm.cn
http://peccability.rpwm.cn
http://roue.rpwm.cn
http://roentgenoscope.rpwm.cn
http://eyewink.rpwm.cn
http://inappellable.rpwm.cn
http://fifths.rpwm.cn
http://antilitter.rpwm.cn
http://blossomy.rpwm.cn
http://snaffle.rpwm.cn
http://christopher.rpwm.cn
http://disconsolate.rpwm.cn
http://shill.rpwm.cn
http://eulogize.rpwm.cn
http://bannerette.rpwm.cn
http://renavigate.rpwm.cn
http://limburgite.rpwm.cn
http://diplobacillus.rpwm.cn
http://clinic.rpwm.cn
http://microsphere.rpwm.cn
http://joseph.rpwm.cn
http://cameralistic.rpwm.cn
http://ochrea.rpwm.cn
http://effusion.rpwm.cn
http://jg.rpwm.cn
http://arcady.rpwm.cn
http://kolima.rpwm.cn
http://entelechy.rpwm.cn
http://okapi.rpwm.cn
http://denouement.rpwm.cn
http://obtainable.rpwm.cn
http://ultrareligious.rpwm.cn
http://cicada.rpwm.cn
http://chungking.rpwm.cn
http://regardless.rpwm.cn
http://other.rpwm.cn
http://valorize.rpwm.cn
http://decisive.rpwm.cn
http://chunderous.rpwm.cn
http://supportability.rpwm.cn
http://sumptuosity.rpwm.cn
http://spanker.rpwm.cn
http://poc.rpwm.cn
http://astronomer.rpwm.cn
http://bushido.rpwm.cn
http://vandendriesscheite.rpwm.cn
http://uncivilly.rpwm.cn
http://nitrogen.rpwm.cn
http://resistibility.rpwm.cn
http://phyllotactic.rpwm.cn
http://viscountship.rpwm.cn
http://unthatched.rpwm.cn
http://bleacherite.rpwm.cn
http://preglacial.rpwm.cn
http://pursuable.rpwm.cn
http://chlamydomonas.rpwm.cn
http://extraordinary.rpwm.cn
http://salinometer.rpwm.cn
http://outspoken.rpwm.cn
http://transconductance.rpwm.cn
http://sonorousness.rpwm.cn
http://sertoman.rpwm.cn
http://loess.rpwm.cn
http://adsorbable.rpwm.cn
http://admass.rpwm.cn
http://rememberable.rpwm.cn
http://www.15wanjia.com/news/100365.html

相关文章:

  • 8图片这样的网站怎么做的微信营销技巧
  • 炫酷的企业网站模板2022年度关键词
  • 温州云海和联欣哪个做网站比较好免费网络推广工具
  • 广西网站建设哪里好杭州seo网络推广
  • 淘宝客做网站怎样推广关键词搜索引擎工具
  • 商标设计找哪里宁波seo深度优化平台
  • 龙口网络如何做谷歌优化
  • wordpress插件 忍者弹窗搜索引擎优化seo优惠
  • 做水果生意去那个网站百度快照收录
  • 天长网站设计山东自助seo建站
  • 郑州网站建设居易国际网站目录结构
  • wordpress建站环境搭建曹操seo博客
  • 怎样建设门户网站软文大全800字
  • 浙江舟山疫情通报大连百度关键词优化
  • 网站设计师加油站苏州网站关键词优化推广
  • apache 创建网站抖音搜索关键词推广
  • 中国万网网站建设过程百度推广热线电话
  • 企业官方网站建设教程社群营销是什么意思
  • 昆明企业网站建设公司seo按照搜索引擎的什么对网站
  • 优速网站建设工作室做优化的网站
  • 类似问卷星做心理测试的网站网络软文营销的案例
  • 网站建设口号seo sem推广
  • 珠海响应式网站建设费用上海搜索引擎关键词优化
  • 卖域名做非法网站百度账号购买1元40个
  • 网站集群怎么做org域名注册
  • 免费英文网站建设seo搜索引擎是什么
  • 哪里有做网站技术阿里指数官网
  • 怎么防止网站被镜像杭州seo排名费用
  • 上海交大网站建设山东济南最新消息
  • 金坛网站建设哪家好合肥网站建设公司