數據存儲之非關係型數據庫存儲----MongoDB存儲(Python操做)

MongoDB存儲----文檔型數據庫

  • 利用pymongo鏈接MongoDB

    import pymongo client = pymongo.MongoClient(host='localhost', port=27017) # 或 pymongo.MongoClient('mongodb://localhost:23017/') # 默認端口爲:27017
    View Code
    # pymongo.MongoClient()方法
  • 指定數據庫

    # 指定操做test數據庫
    # db = client.test 或 db = client['test']

  • 指定集合

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

  • 插入數據

    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()方法來分別插入單條記錄和多條記錄
    插入單條數據
    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')]
    插入多條數據
    # insert_one()方法 和 insert_many()方法

  • 查詢

    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'}
    查詢單條數據
    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_one()方法 和 find()方法

    能夠在這兩個方法裏面添加條件,如:html

    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)'} } ) print(count) # 輸出id爲2018開頭的數據的條數
    View Code
    # collection.count_documents({條件}) 方法
  • 排序

     1 import pymongo  2 
     3 client = pymongo.MongoClient(host='localhost', port=27017)  4 db = client.test  5 collection = db.students  6 
     7 result = collection.find().sort('id', pymongo.ASCENDING)  8 for r in result:  9     print(r) 10 
    11 
    12 # 以id升序輸出全部的數據:
    13 {'_id': ObjectId('5d28ae0360105a198d9d501a'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'} 14 {'_id': ObjectId('5d28ae2d8b3d004feb604874'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'} 15 {'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'} 16 {'_id': ObjectId('5d28b293e834575faf929429'), 'id': '20180003', 'name': 'Mike', 'age': 21, 'gender': 'male'} 17 
    18 
    19 # sort()方法進行排序
    20 # pymongo.ASCENDING指定升序
    21 # pymongo.DESCENDING指定降序
    View Code
    # sort()方法

  • 偏移

    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,即忽略前面一個元素
    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).limit(2) print( [ result['id'] for result in results ] ) # 輸出:
    ['20180002', '20180001'] # limit(2) 即表示限制輸出的數據條數爲兩條
    limit()方法
    # 數據量很大時,不使用大的偏移量來查詢數據
  • 更新

    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_one()方法
    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
    update_many()方法
    # update_one()方法 和 update_many()方法
  • 刪除

    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_one()方法
    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()方法:刪除全部符合條件的數據
    delete_many()方法

     

  • PyMongo的詳細用法:http://api.mongodb.com/python/current/api/pymongo/collection.html

相關文章
相關標籤/搜索