python 操作 mongodb 数据库详情

作者:autofelix 时间:2024-01-19 17:53:45 

一、安装

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)))

来源:https://blog.51cto.com/autofelix/5195732

标签:python,操作,mongodb
0
投稿

猜你喜欢

  • 浅析MySQL replace into 的用法

    2024-01-18 15:58:38
  • Python使用回溯法子集树模板解决迷宫问题示例

    2021-07-09 14:42:43
  • 使用python的pyplot绘制函数实例

    2021-07-27 01:21:30
  • Python 多进程和数据传递的理解

    2021-06-01 02:30:07
  • python判断、获取一张图片主色调的2个实例

    2022-07-26 18:27:00
  • vue3中使用ref和emit来减少props的使用示例详解

    2024-04-27 16:02:02
  • python中无法导入本地安装好的第三方库问题

    2022-06-25 12:24:31
  • 教你一分钟在win10终端成功安装Pytorch的方法步骤

    2023-09-01 19:32:38
  • MySQL删除数据Delete与Truncate语句使用比较

    2024-01-15 05:12:42
  • Python高级特性之闭包与装饰器实例详解

    2021-09-19 03:17:25
  • PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)

    2023-10-21 05:36:58
  • python反编译教程之2048小游戏实例

    2023-07-24 08:04:47
  • Oracle终极彻底卸载的完整步骤

    2024-01-13 14:37:59
  • Python实现对二维码数据进行压缩

    2022-10-22 12:51:59
  • javascript实现瀑布流自适应遇到的问题及解决方案

    2024-04-16 10:35:23
  • vscode ssh安装librosa处理音频的解决方法

    2022-04-25 04:33:54
  • Go语言基础map用法及示例详解

    2024-04-26 17:33:37
  • 运用python去除图片水印

    2021-05-06 10:54:20
  • MySQL数据库备份与恢复方法

    2024-01-18 08:54:10
  • 使用Python的Supervisor进行进程监控以及自动启动

    2022-11-19 16:55:35
  • asp之家 网络编程 m.aspxhome.com