"""
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>
引自倪興國,侵刪