Python中非关系型数据库存储(MongoDB)

NoSQL,全称Not Only SQL,意为不仅仅是SQL,泛指非关系型数据库。NoSQL是基于键值对的,而且不需要经过SQL层的解析,数据之间没有耦合性,性能非常高。

  • 键值存储数据库:代表有Redis、Voldemort和Oracle BDB等。
  • 列存储数据库:代表有Cassandra、HBase和Riak等。
  • 文档型数据库:代表有CouchDB和MongoDB等。
  • 图形数据库:代表有Neo4J、InfoGrid和InfiniteGraph等。

MongoDB存储

  • 需要安装好MongoDB并且启动。
  • 安装好Python的PyMongo库。

1.连接MongoDB

import pymongo
# 使用MongoClient()创建连接对象,只需传入IP以及端口号
cilent = pymongo.MongoClient(host='localhost', port=27017)

2.指定数据库

# 指定名为test的数据库
db = client.test
# db = client['test']

3.指定集合

# 指定集合
# MongoDB的每个数据库包含了许多集合,它们类似于关系型数据库中的表
collection = db.students
# collection = db['students']

4.插入数据

# 插入数据
# 对于students这个集合,新建一条学生数据,以字典形式表示
student = {
    
    
    'id': '20171703',
    'name': 'Irving',
    'age': 20,
    'gender': 'male'
}
# 调用insert_one()方法插入单条数据
result = collection.insert_one(student)
# insert_many()插入多条数据(以列表形式传递)
# result = collection.insert_many([student, student1])
print(result)

在MongoDB中,每条数据都有一个_id属性来唯一标识。
5.查询
find_one():查询得到的是单个结果

# 查询name为Kyrie的数据
result = collection.find_one({
    
    'name': 'Kyrie'})
print(type(result))
print(result)

find(): 返回一个生成器对象

# 查询age为20的多条数据
results = collection.find({
    
    'age': 20})
print(result)
# 返回结果是Cursor类型,相当于一个生成器,需要遍历取到所有结果
for result in results:
    # 每个结果都是字典类型
    print(result)

如果要查询年龄大于20的数据,则需要将查询条件写为如下形式:

results = collection.find({
    
    'age': {
    
    '$gt': 20}})

这里查询条件已经不是数字,而是字典。其键名为比较符号。
比较符号
在这里插入图片描述
6.计数
调用count()方法可以统计查询结果有多少条数据。

# 统计所有数据条数
count = collection.find().count()
print(count)


# 统计符合某个条件的数据
count = collection.find({
    
    'age': 20}).count()

7.排序
直接调用sort()方法,再在里面传入排序的字段以及升降序标志即可。

results = collection.find().sort('name', pymongo.ASCENDING)
print([result['name'] for result in results])

ASCENDING:升序 DESCENDING:降序
8.偏移
在只想取某几个元素时,可以调用skip()方法偏移位置。

# 忽略前两个元素,得到第三个及以后的元素
results = collection.find().sort('name', pymongo.ASCENDING).skip(2)
print([result['name'] for result in results])

limit()方法则是指定要取的结果个数:

# 截取两个结果返回
results = collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)
print([result['name'] for result in results])

9.更新
可使用update()方法,指定更新的条件和更新后的数据即可。

# 1指定查询条件
condition = {
    
    'name': 'Kyrie'}
# 2将数据查询出来
student = collection.find_one(condition)
# 3修改年龄
student['age'] = 44
# 4调用update()方法将原条件修改后的数据传入
result = collection.update(condition, student)

使用$set操作符对数据进行更新:

result = collection.update(condition, {
    
    '$set': student})

update_one()

# 1指定查询条件
condition = {
    
    'name': 'Kyrie'}
# 2将数据查询出来
student = collection.find_one(condition)
# 3修改年龄
student['age'] = 44
# 4调用update()方法将原条件修改后的数据传入
result = collection.update_one(condition, {
    
    '$set': student})
# 分别调用matched_count和modified_count属性,可以获取匹配的数据条数与影响的数据条数
print(result.matched_count, result.modified_count)

update_many()

# 指定查询条件
condition = {
    
    'age': {
    
    '$gt': 18}}
# 调用update_one()方法将原条件修改后的数据传入
result = collection.update_many(condition, {
    
    '$inc': {
    
    'age': 1}})
# 分别调用matched_count和modified_count属性,可以获取匹配的数据条数与影响的数据条数
print(result.matched_count, result.modified_count)

这里的更新条件的意思为:age加1
查询age大于18的所有数据,然后再设置更新条件为{’$inc’: {‘age’: 1}},执行之后就会将所有符合条件的数据的age都加1。
10.删除
直接调用remove()方法指定删除条件即可。

result = collection.remove({
    
    'name': Kyrie})

delete_one()

# 删除第一条符合条件的数据
result = collection.delete_one({
    
    'name': Kyrie})
# 调用deleted_count可以获取删除的数据条数
print(result.deleted_count)

delete_many()

# 删除所有符合条件的数据(age小于25)
result = collection.delete_many({
    
    'age': {
    
    '$lt': 25}})

PyMongo的详细用法,可参考官方文档
MongoDB数据库和集合的操作

猜你喜欢

转载自blog.csdn.net/weixin_43670190/article/details/106642394