温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用python包操作mongodb数据库

发布时间:2022-04-19 15:17:46 来源:亿速云 阅读:140 作者:iii 栏目:开发技术

本文小编为大家详细介绍“怎么用python包操作mongodb数据库”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用python包操作mongodb数据库”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一、安装

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数据库”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI