• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

浅析Python与Mongodb数据库之间的操作方法

python 搞代码 4年前 (2022-01-09) 27次浏览 已收录 0个评论
文章目录[隐藏]

MongoDB 是目前最流行的 NoSQL 数据库之一,使用的数据类型 BSON(类似 JSON)。

1. 安装Mongodb和pymongo

Mongodb的安装和配置

Mongodb的安装教程请网上搜索, 安装完成后,    进行以下配置过程:

1.1 创建目录, 该目录为Mongodb数据文件的存放目录:

*注: 本人使用的不是root用户, 所以修改目录的拥有者. *

sudo mkdir /data
sudo chown -R python:python /data
mkdir /data/db

1.2 分别执行命令:

第一条命令为指定端口和保存路径, 第二条为运行mongodb数据库.

mongod --port 27017 --dbpath /data/db
mongo --port 27017

1.3 安装pymongo

sudo pip3 install pymongo

2. 连接数据库、指定数据库、指定集合、插入数据:

mongodb存储数据以键值形式, 因此在Python中使用字段插入数据.

import pymongo
#连接mongodb
client = pymongo.MongoClient('mongodb://localhost:27017/')
#指定数据库
db = client.test4
#指定集合
collection = db.<div>本文来源gaodai.ma#com搞##代!^码@网3</div>students
#数据
student1 = {
 'id': '201801',
 'name': 'Jack',
 'age': 20,
 'gender': 'male'
}
student2 = {
 'id': '201802',
 'name': 'Tom',
 'age': 22,
 'gender': 'male'
}
student3 = {
 'id': '201803',
 'name': 'Rose',
 'age': 21,
 'gender': 'female'
}
student4 = {
 'id': '201804',
 'name': 'Mike',
 'age': 20,
 'gender': 'female'
}
student5 = {
 'id': '201805',
 'name': 'Ray',
 'age': 20,
 'gender': 'female'
}
student6 = {
 'id': '201806',
 'name': 'Alan',
 'age': 21,
 'gender': 'male'
}
#插入一条数据
result1 = collection.insert_one(student1)
print(result1)
print(result1.inserted_id)
# #插入多条数据
result2 = collection.insert_many([student2, student3, student4, student5, student6])
print(result2)
print(result2.inserted_ids)

运行结果:

insert方法:

5b3a1942971951218d41c02b
[ObjectId(‘5b3a1942971951218d41c02c’), ObjectId(‘5b3a1942971951218d41c02d’)]

官方推荐:

<pymongo.results.InsertOneResult object at 0x7fa4cc363ec8>
5b3a1942971951218d41c02e
<pymongo.results.InsertManyResult object at 0x7fa4cc363f08>
[ObjectId('5b3a1942971951218d41c02f'), ObjectId('5b3a1942971951218d41c030')]

3. 查询、计数、排序、偏移:

import pymongo
from bson.objectid import ObjectId
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client.test4
collection = db.students
#查询一条数据
print('单条数据','='*50)
result = collection.find_one({'name': 'Jack'})
print(result)
print('多条数据','='*50)
#查询多条数据
for res in collection.find({'age': {'$mod': [5, 0]}}):
 print(res)
#计数
print('计数','='*50)
count = collection.find({'age': {'$mod': [5, 0]}}).count()
print(count)
#排序
print('排序','='*50)
results = collection.find().sort('name', pymongo.ASCENDING) #升序, pymongo.DESCENDING为降序
print([result['name'] for result in results])
#偏移
print('偏移','='*50)
results = collection.find().sort('name', pymongo.ASCENDING).skip(2) #偏移2位,忽略前两个数据
print([result['name'] for result in results])
results = collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2) #只输出2个数据
print([result['name'] for result in results])
find({‘age': {'$mod': [5, 0]}}): 表示查找年龄取余5余0的值. 还有很多比较符号, 请百度.

运行结果:

单条数据 ==================================================
{'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 20, 'gender': 'male'}
多条数据 ==================================================
{'_id': ObjectId('5b3a1942971951218d41c02b'), 'id': '201801', 'name': 'Jack', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5b3a1942971951218d41c02e'), 'id': '201804', 'name': 'Mike', 'age': 20, 'gender': 'female'}
{'_id': ObjectId('5b3a1942971951218d41c02f'), 'id': '201805', 'name': 'Ray', 'age': 20, 'gender': 'female'}
计数 ==================================================
3
排序 ==================================================
['Alan', 'Jack', 'Mike', 'Ray', 'Rose', 'Tom']
偏移 ==================================================
['Mike', 'Ray', 'Rose', 'Tom']
['Mike', 'Ray']

4. 更新:

4.1  不使用$set更新数据:


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:浅析Python与Mongodb数据库之间的操作方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址