python中mongodb包操作数据库


Posted in Python onApril 19, 2022

一、安装

pip install pymongo

二、连接数据库

import pymongo

# 方式一
client = pymongo.MongoClient('mongodb://localhost:27017')
# 方式二
client = pymongo.MongoClient('localhost',27017)
# 方式三,有密码认证
client = pymongo.MongoClient('localhost', 27017, username='xxx', password='xxx')

三、创建数据库

import pymongo

# 连接
client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test # 或者 db = client['test']
print(db)

四、所有数据库

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
dbs = client.list_database_names()

五、创建集合

  • 也就是数据库中的表
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user # 或者 collections = db['user']
# 删除表
collections.drop()

六、插入数据

  • insert_one:插入一条数据
  • insert_many:插入多条数据
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 创建文档数据
user1 = {
'name': 'autofelix',
'age': '25',
'height': '172',
'weight': '60'
}

user2 = {
'name': '飞兔小哥',
'age': '28',
'height': '182',
'weight': '70'
}

# 插入一条文档集合
result = collections.insert_one(user1)
print(result)
print(result.inserted_id)

# 插入多条文档集合
result = collections.insert_many([user1, user2])
print(result)
print(result.inserted_ids)

七、查询数据

  • find:查询多条数据
  • find_one:查询一条数据
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 查询所有
collections.find()
# 查询最近一条
collections.find_one()
# 根据条件查询
collections.find_one({'age':25})

八、高级查询

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 跳过第一条查到的数据
collections.find({'age':{'$gt':10}},['height','age']).skip(1)
# limit限制查询条数
collections.find({'age':{'$gt':10}},['height','age']).limit(1)
# 多条件查询
collections.find_one({'height':{'$gt':150},'age':{'$lt':26,'$gt':10}})
# in查询,查询年龄在25,26,32的数据
collections.find({'age':{'$in':[25, 26, 32]}})
# or查询,查询年龄小于等于23或者大于等于29的数据
collections.find({'$or':[{'age':{'$lte':23}}, {'age':{'$gte':29}}]})
# exists查询
collections.find({'age':{'$exists':True}})
# 正则查询
collections.find({'name':{'$regex':r'.*auto.*'}})

九、count统计

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 统计集合中总共有多少条数据
collections.find().count()
# 统计集合中年龄大于10岁的共有多少条数据
collections.find({'age':{'$gt':10}}).count()

十、修改数据

  • update_one:修改一条数据
  • update_many:修改多条数据
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 修改一条数据
collections.update_one({'name': 'autofelix'}, {'$set': {'name': '大神'}})
# 修改多条数据
collections.update_many({'name': 'autofelix'}, {'$set': {'name': '大神'}})

十一、删除数据

  • delete_one:删除一条数据
  • delete_many:删除多条数据
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 删除一条数据
collections.delete_one({'name': 'autofelix'})
# 删除多条数据
collections.delete_many({'name': 'autofelix'})
# 删除所有数据
collections.delete_many({})

十二、数据排序

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 创建test数据库
db = client.test
# 创建表
collections = db.user

# 对字段 age 按升序排序
collections.find().sort('age')
# 对字段 age 按降序排序
collections.find().sort('age', -1)
# 多字段排序
collections.find().sort((('age',pymongo.ASCENDING),('height',pymongo.ASCENDING)))

到此这篇关于python 包操作 mongodb 数据库详情的文章就介绍到这了!

Python 相关文章推荐
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 Python
Python中psutil的介绍与用法
May 02 Python
python实现BP神经网络回归预测模型
Aug 09 Python
wxPython实现文本框基础组件
Nov 18 Python
wxPython电子表格功能wx.grid实例教程
Nov 19 Python
postman和python mock测试过程图解
Feb 22 Python
Python爬虫实现HTTP网络请求多种实现方式
Jun 19 Python
python try...finally...的实现方法
Nov 25 Python
python 如何上传包到pypi
Dec 24 Python
Python的信号库Blinker用法详解
Dec 31 Python
解析目标检测之IoU
Jun 26 Python
Python+OpenCV实现在图像上绘制矩形
Mar 21 Python
Elasticsearch 聚合查询和排序
Apr 19 #Python
Elasticsearch 基本查询和组合查询
Apr 19 #Python
Elasticsearch 批量操作
Apr 19 #Python
Elasticsearch 数据类型及管理
Apr 19 #Python
Elasticsearch 索引操作和增删改查
Apr 19 #Python
python中redis包操作数据库的教程
Apr 19 #Python
python中pymysql包操作数据库方法
Apr 19 #Python
You might like
基于xcache的配置与使用详解
2013/06/18 PHP
JavaScript isArray()函数判断对象类型的种种方法
2010/10/11 Javascript
影响jQuery使用的14个方面
2014/09/01 Javascript
JS模拟酷狗音乐播放器收缩折叠关闭效果代码
2015/10/29 Javascript
JavaScript禁止用户多次提交的两种方法
2016/07/24 Javascript
基于Layer+jQuery的自定义弹框
2020/05/26 Javascript
jquery Easyui Datagrid实现批量操作(编辑,删除,添加)
2017/02/20 Javascript
discuz表情的JS提取方法分析
2017/03/22 Javascript
node.js中debug模块的简单介绍与使用
2017/04/25 Javascript
Angular 4.x中表单Reactive Forms详解
2017/04/25 Javascript
深入研究jQuery图片懒加载 lazyload.js使用方法
2017/08/16 jQuery
解决vue-cli3 使用子目录部署问题
2018/07/19 Javascript
关于AngularJS中ng-repeat不更新视图的解决方法
2018/09/30 Javascript
浅谈angular表单提交中ng-submit的默认使用方法
2018/09/30 Javascript
JS动画实现回调地狱promise的实例代码详解
2018/11/08 Javascript
laravel-admin 与 vue 结合使用实例代码详解
2019/06/04 Javascript
JS实现关闭小广告特效
2021/01/29 Javascript
基于javascript实现日历功能原理及代码实例
2020/05/07 Javascript
tornado框架blog模块分析与使用
2013/11/21 Python
Python 列表(List)操作方法详解
2014/03/11 Python
python实现在字符串中查找子字符串的方法
2015/07/11 Python
用python记录运行pid,并在需要时kill掉它们的实例
2017/01/16 Python
Python多线程实现同步的四种方式
2017/05/02 Python
使用celery执行Django串行异步任务的方法步骤
2019/06/06 Python
django mysql数据库及图片上传接口详解
2019/07/18 Python
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2019/10/04 Python
Python高阶函数、常用内置函数用法实例分析
2019/12/26 Python
美国网上眼镜商城:Zenni Optical
2016/11/20 全球购物
法国美发器材和产品购物网站:Beauty Coiffure
2016/12/05 全球购物
松本清官方海外旗舰店:日本最大的药妆连锁店
2017/11/21 全球购物
Otticanet意大利:最顶尖的世界名牌眼镜, 能得到打折季的价格
2019/03/10 全球购物
平面网站制作专科生的自我评价分享
2013/12/11 职场文书
食品工程专业求职信
2014/06/15 职场文书
人间正道是沧桑观后感
2015/06/15 职场文书
六种css3实现的边框过渡效果
2021/04/22 HTML / CSS
超外差式晶体管收音机的组装与统调
2021/04/22 无线电