Python 官方文档:入门教程 => 点击学习
目录一、安装二、连接数据库三、创建数据库四、所有数据库五、创建集合六、插入数据七、查询数据八、高级查询九、count统计十、修改数据十一、删除数据十二、数据排序一、安装 pip in
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()
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)
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.*'}})
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()
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': '大神'}})
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 操作mongodb内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python 操作 mongodb 数据库详情
本文链接: https://www.lsjlt.com/news/117418.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0