Python 操作 MongoDB 数据库

MongoDB是一个存储文档型的数据库(非关系型数据库)

利用pymongo连接MongoDB

import pymongo
client = pymongo.MongoClient(host='localhost', port=27017)
# 或 pymongo.MongoClient('mongodb://localhost:23017/')
# 默认端口为:27017

指定数据库

# 指定操作test数据库
db = client.test 或 db = client['test']

指定集合

# 指定一个集合要操作的集合students
collection = db.students 或 collection = db['students']

插入数据

插入单条数据

# 插入一条数据
insert_one()方法
import pymongo

# 连接MongoDB
client = pymongo.MongoClient(host='localhost', port=27017)

# 指定数据库
db = client.test

# 指定集合
collection = db.students

# 数据
student = {
    'id': '20180001',
    'name': 'Jordan',
    'age': 20,
    'gender': 'male'
}

# 利用insert_one()方法插入一条数据
result = collection.insert_one(student)
print(result)

# 运行输出:<pymongo.results.InsertOneResult object at 0x11089b448>
# 在MongoDB中,每条数据其实都有一个_id属性来唯一标识。如果没有显式指明该属性,MongoDB会自动产生一个ObjectId类型的_id属性。
# 使用 insert_one()和 insert_many()方法来分别插入单条记录和多条记录

插入多条数据

# 插入多条数据
insert_many()方法
import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

student1 = {
    'id': '20180002',
    'name': 'Lee Hua',
    'age': 20,
    'gender': 'male'
}
student2 = {
    'id': '20180003',
    'name': 'Mike',
    'age': 21,
    'gender': 'male'
}

result = collection.insert_many([student1, student2])
print(result)
print(result.inserted_ids)
# 调用inserted_ids属性可以获取数据的_id列表


# 运行输出:
<pymongo.results.InsertManyResult object at 0x110826d88>
[ObjectId('5d28b293e834575faf929428'), ObjectId('5d28b293e834575faf929429')]

查询

查询一条数据

# 查询一条数据
find_one()
import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

result = collection.find_one({'name': 'Lee Hua'})
print(result)


# 输出:
{'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'}

查询多条数据

# 查询一条数据
find()
import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

result = collection.find()
print(result)
for r in result:
    print(r)


# find()方法返回一个迭代器,用for循环逐条输出
# 输出结果:
<pymongo.cursor.Cursor object at 0x10e0f7320>
{'_id': ObjectId('5d28ae0360105a198d9d501a'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5d28ae2d8b3d004feb604874'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5d28b293e834575faf929429'), 'id': '20180003', 'name': 'Mike', 'age': 21, 'gender': 'male'}

可以在这两个方法里面添加条件,如:

find(
    {
        'name': {'$regex': '^M.*'}
    }
)
这里查找的是以'M'开头的名字的那些数据,
$regex指定的是正则表达式,
^M.*是一条正则表达式
更多功能符号(如$regex)、数值比较符号的查看MongoDB官方文档:https://docs.mongodb.com/?searchProperty=manual

计数

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

count = collection.count_documents(
    {
        'id': {'$regex': '^(2018)'}
    }
)
# 输出id为2018开头的数据的条数
print(count)

排序

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

result = collection.find().sort('id', pymongo.ASCENDING)
for r in result:
    print(r)


# 以id升序输出所有的数据:
{'_id': ObjectId('5d28ae0360105a198d9d501a'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5d28ae2d8b3d004feb604874'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('5d28b293e834575faf929429'), 'id': '20180003', 'name': 'Mike', 'age': 21, 'gender': 'male'}


# sort()方法进行排序
# pymongo.ASCENDING指定升序
# pymongo.DESCENDING指定降序

偏移

skip() 方法

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

results = collection.find().sort('id', pymongo.DESCENDING).skip(1)
print(
    [
        result['id'] for result in results
    ]
)


# 输出:
['20180002', '20180001', '20180001']
# skip(1)表示偏移1,即忽略前面一个元素

limit()方法

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students

results = collection.find().sort('id', pymongo.DESCENDING).skip(1).limit(2)
print(
    [
        result['id'] for result in results
    ]
)

# 输出:
['20180002', '20180001']
# limit(2) 即表示限制输出的数据条数为两条

注意:数据量很大时,不使用大的偏移量来查询数据。

更新

update_one()方法

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client['test']
collection = db['students']

# 查询条件:age >= 20
query_condition = {
    'age': {'$gte': 20}
}

# 更新条件:数据的age加1
update_condition = {
    '$inc': {'age': 1}
}

result = collection.update_one(query_condition, update_condition)
print(result)
print(result.matched_count, result.modified_count)


# 输出:
<pymongo.results.UpdateResult object at 0x110a11c88>
1 1


# 返回的结果是UpdateResul类型的
# 调用matched_count属性,获得匹配数据的条数
# 调用modified_count属性,获得影响数据的条数
# $gte : 大于等于
# $inc : 将字段递增指定的值
# updata_one()更新与筛选器匹配的单个文档

update_many()方法

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client['test']
collection = db['students']

query_condition = {
    'age': {'$gte': 20}
}
update_condition = {
    '$inc': {'age': 1}
}

result = collection.update_many(query_condition, update_condition)
print(result)
print(result.matched_count, result.modified_count)


# 输出:
<pymongo.results.UpdateResult object at 0x111c84448>
4 4

删除

delete_one() 方法

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client['test']
collection = db['students']

result = collection.delete_one({'age': 21})
print(result.deleted_count)


# delete_one()方法:删除第一条符合条件的数据
# delete_count属性:获取删除数据的条数

delete_many() 方法

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db = client['test']
collection = db['students']

result = collection.delete_many({'age': 21})
print(result.deleted_count)


# delete_many()方法:删除所有符合条件的数据

PyMongo的详细用法

官方文档:http://api.mongodb.com/python/current/api/pymongo/collection.html

posted @ 2019-07-13 18:43  LeeHua  阅读(502)  评论(0编辑  收藏  举报