"""
MongoDB存储
在这里咱们来看一下Python3下MongoDB的存储操做,在本节开始以前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python
的PyMongo库。html
链接MongoDB
链接MongoDB咱们须要使用PyMongo库里面的MongoClient,通常来讲传入MongoDB的IP及端口便可,第一个参数为地址host,
第二个参数为端口port,端口若是不传默认是27017。
"""
import pymongo
client = pymongo.MongoClient(host='localhost', port=27017)
"""
这样咱们就能够建立一个MongoDB的链接对象了。另外MongoClient的第一个参数host还能够直接传MongoDB的链接字符串,以mongodb开头,
例如:client = MongoClient('mongodb://localhost:27017/')能够达到一样的链接效果。
"""python
db = client.test正则表达式
collection = db.students
collection = db['students']mongodb
student = {
'id': '20170101',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}数据库
result = collection.insert(student)
print(result)api
student1 = {
'id': '20170101',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}3d
student2 = {
'id': '20170202',
'name': 'Mike',
'age': 21,
'gender': 'male'
}htm
result = collection.insert([student1, student2])
print(result)对象
student = {
'id': '20170101',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}blog
result = collection.insert_one(student)
print(result)
print(result.inserted_id)
<pymongo.results.InsertOneResult object at 0x10d68b558>
5932ab0f15c2606f0c1cf6c5
student1 = {
'id': '20170101',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
student2 = {
'id': '20170202',
'name': 'Mike',
'age': 21,
'gender': 'male'
}
result = collection.insert_many([student1, student2])
print(result)
print(result.inserted_ids)
result = collection.find_one({'name': 'Mike'})
print(type(result))
print(result)
<class'dict'>
{'_id': ObjectId('5932a80115c2606a59e8a049'), 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}
from bson.objectid import ObjectId
result = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')})
print(result)
{' ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
results = collection.find({'age': 20})
print(results)
for result in results:
print(result)
<pymongo.cursor.Cursor object at 0x1032d5128>
{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('593278c815c2602678bb2b8d'), 'id': '20170102', 'name': 'Kevin', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('593278d815c260269d7645a8'), 'id': '20170103', 'name': 'Harden', 'age': 20, 'gender': 'male'}
results = collection.find({'age': {'$gt': 20}})
"""
符号含义示例
$lt小于{'age': {'$lt': 20}}
$gt大于{'age': {'$gt': 20}}
$lte小于等于{'age': {'$lte': 20}}
$gte大于等于{'age': {'$gte': 20}}
$ne不等于{'age': {'$ne': 20}}
$in在范围内{'age': {'$in': [20, 23]}}
$nin不在范围内{'age': {'$nin': [20, 23]}}
"""
results = collection.find({'name': {'$regex': '^M.*'}})
"""
符号含义示例示例含义
$regex匹配正则{'name': {'$regex': '^M.*'}}name以M开头
$exists属性是否存在{'name': {'$exists': True}}name属性存在
$type类型判断{'age': {'$type': 'int'}}age的类型为int
$mod数字模操做{'age': {'$mod': [5, 0]}}年龄模5余0
$text文本查询{'$text': {'$search': 'Mike'}}text类型的属性中包含Mike字符串
$where高级条件查询{'$where': 'obj.fans_count == obj.follows_count'}自身粉丝数等于关注数
"""
https://docs.mongodb.com/manual/reference/operator/query/
count = collection.find().count()
print(count)
count = collection.find({'age': 20}).count()
print(count)
results = collection.find().sort('name', pymongo.ASCENDING)
print([result['name'] for result in results])
['Harden', 'Jordan', 'Kevin', 'Mark', 'Mike']
results = collection.find().sort('name', pymongo.ASCENDING).skip(2)
print([result['name'] for result in results])
['Kevin', 'Mark', 'Mike']
results = collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)
print([result['name'] for result in results])
['Kevin', 'Mark']
condition = {'name': 'Kevin'}
student = collection.find_one(condition)
student['age'] = 25
result = collection.update(condition, student)
print(result)
{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}
condition = {'name': 'Kevin'}
student = collection.find_one(condition)
student['age'] = 26
result = collection.update_one(condition, {'$set': student})
print(result)
print(result.matched_count, result.modified_count)
<pymongo.results.UpdateResult object at 0x10d17b678>
condition = {'age': {'$gt': 20}}
result = collection.update_one(condition, {'$inc': {'age': 1}})
print(result)
print(result.matched_count, result.modified_count)
<pymongo.results.UpdateResult object at 0x10b8874c8>
condition = {'age': {'$gt': 20}}
result = collection.update_many(condition, {'$inc': {'age': 1}})
print(result)
print(result.matched_count, result.modified_count)
<pymongo.results.UpdateResult object at 0x10c6384c8>
result = collection.remove({'name': 'Kevin'})
print(result)
{'ok': 1, 'n': 1}
result = collection.delete_one({'name': 'Kevin'})
print(result)
print(result.deleted_count)
result = collection.delete_many({'age': {'$lt': 25}})
print(result.deleted_count)
<pymongo.results.DeleteResult object at 0x10e6ba4c8>
引自倪兴国,侵删