1. 程式人生 > >學習日誌(七) Python+MongoDB

學習日誌(七) Python+MongoDB

Python+MongoDB

1. 插入資料

insert_one(doc)#插入一條
insert_many(doc,ordered=True/False)#插入多條
#True 代表順序插入,若有一條插入失敗,則批量插入失敗
#False 代表不啟用順序插入,插入失敗的資料不硬性其他資料的插入

連線MongoDB伺服器

import pymongo
from pymongo import MongoClient#匯入連線函式
conn = MongoClient('localhost')#連線
db = conn.myschool#連線myschool這個庫
employees = db.stuinfo#建立第一個集合stuinfo
# employees.remove(None)#清空 studentsInfo = db.stuinfo

插入一條記錄

zhangsan = {'name':'zhangsan',
            'age':30,
            'sex':'boy',
            'contact':{
                'email1':'[email protected]',
                'email2':'[email protected]'
                        }
            }
employees.insert_one(zhangsan)

插入多條記錄

lishi = {'name':'lishi','habit':{'habit1':'eat','habit2':'sleep'}}
wangwu = {'name':'wangwu','age':20,'sex':'boy'}
result = [lishi,wangwu]
employees.insert_many(result,True)
print('插入成功!')

2. 查詢資料

cursor = studentsInfo.find({})
for student in cursor:
    print(student)
conn.close()#關閉連線
print('按條件查詢') cursor = studentsInfo.find({'name':'zhangsan'}) for student in cursor: print(student) print('按條件查詢(查詢條件加操作符)') cursor = studentsInfo.find({'name':{'$in':['zhangsan','lishi']}}) for student in cursor: print(student) print('按條件查詢(age大於25歲)') cursor = studentsInfo.find({'age':{'$gt':25}}) for student in cursor: print(student) print('條件查詢(or 方式查詢)') cursor = studentsInfo.find({'$or':[{'name':{'$in':['zhangsan','wangwu']}},{'age':{'$gt':30}}]}) for student in cursor: print(student) print('條件查詢(and 方式查詢)') cursor = studentsInfo.find({'$and':[{'name':{'$in':['zhangsan','lishi']}},{'age':{'$gt':25}}]}) for student in cursor: print(student) print('子條件查詢(巢狀模式查詢)') cursor = studentsInfo.find({'contact.email1':'[email protected]'}) for student in cursor: print(student)

3. 更新資料

print('$set:更新操作')
studentsInfo.update_one({'name':'wangwu'},{'$set':{'sex':'girl'}})
cursor = studentsInfo.find({})
for student in cursor:
    print(student)

print('$ine:如果記錄中沒有這個欄位,會增加此欄位')
studentsInfo.update_many({},{'$inc':{'age':2}})
cursor = studentsInfo.find({})
for student in cursor:
    print(student)

# $min($max) 的意思是和當前值比較,取最小(最大)值來進行更新
studentsInfo.update_many({'name':{'$in':['zhangsan','lishi']}},{'$max':{'age':20}})
cursor = studentsInfo.find({})
for student in cursor:
    print(student)

4. 刪除資料

studentsInfo.delete_one({'age':20})#刪除單條
studentsInfo.delete_many({'name':'lishi'})#刪除包含的所有

5. 查詢同時更新

cursor = studentsInfo.find_one_and_update({'name':'wangwu'},{'$set':{'city':'sahnghai'},'$inc':{'age':2}})
print(cursor)

練習

import pymongo
from pymongo import MongoClient
conn = MongoClient('localhost')
db = conn.employee
employee_info = db.employeel
employee_salary = db.employeel

dingsong={'name':'dingsong','salary':238000}
zhangsan={'name':'zhangsan','salary':1500}
lisi={'name':'lisi','salary':300}
wangwu={'name':'wangwu','salary':2500}
result = [dingsong,zhangsan,lisi,wangwu]
employee_info.insert_many(result,True)

cursor = employee_salary.find({'salary':{'$gt':2000}})
for employees in cursor:
    print(employees)

employee_salary.update_many({},{'$inc':{'salary':3000}})
cursor = employee_salary.find({})
for employees in cursor:
    print(employees)

employee_salary.delete_many({'name':'dingsong'})
cursor = employee_salary.find({})
for employees in cursor:
    print(employees)