1 import pymongo #pip install pymongo安裝python操作mongodb的模塊 2 myclient=pymongo.MongoClient(host='127.0.0.1',port=27017) #指定主機和端口號創建客戶端 3 4 dblist=myclient.list_database_names() #查看MongoDB中存在的數據庫 5 print(dblist) 6 7 mydb=myclient['dbtest']#數據庫使用:有則直接使用,沒有的話數據庫只有在內容插入后才會自動創建! 8 print(mydb) 9 10 collist = mydb. list_collection_names()#查看當前數據庫中的表 11 print(collist) 12 13 14 mycol=mydb['t1']#表(集合)使用:有則直接使用,沒有的話集合只有在內容插入后才會創建! 15 print(mycol) 16 17 #MongoDB中數據表的操作: 18 19 # 1.添加數據 20 ''' 21 添加數據: 22 (1)insert()----單條和多條插入:MongoDB3.2版本自之前 23 db.collection.insert({}) 24 db.collection.insert([{},{}...]) 25 (2)3.2之后官方推薦: 26 insert_one()----單條插入 27 db.collection.insert_one({}) 28 insert_many()----多條插入 29 db.collection.insert_many([{},{}...]) 30 ''' 31 # 32 # res1=mycol.insert([{'name':'a','age':20,'gender':1}]) 33 # print(res1) 34 # res2=mycol.insert([{'name':'b','age':21,'gender':0},{'name':'c'}]) 35 # print(res2) 36 37 38 mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" } 39 mylist = [ 40 { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" }, 41 { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" }, 42 { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" }, 43 { "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" }, 44 { "name": "Github", "alexa": "109", "url": "https://www.github.com" } 45 ] 46 # res3=mycol.insert_one(mydict) 47 # print(res3) 48 # res4=mycol.insert_many(mylist) 49 # print(res4) 50 51 52 #2.查詢數據 53 ''' 54 查詢數據: 55 (1)find()----查詢符合條件的所有數據 56 db.collection.find(查詢條件) 57 (2)find_one()----查詢符合條件的第一條數據 58 db.collection.find_one(查詢條件) 59 ''' 60 61 62 all=mycol.find() 63 # all=mycol.find({'age':21}) 64 print(all) 65 for doc in all: 66 print(doc) 67 68 # one=mycol.find_one() 69 # one=mycol.find_one({'age':21}) 70 # print(one) 71 72 73 #3.修改數據(不存在即創建) 74 ''' 75 (1)update()---只能修改查詢出的第一條數據 76 db.coolection.update({查詢條件},{$修改器:{修改值}}) 77 修改器:認定當前修改的類型 $set設置類型修改器 強制修改某字段的值 78 (2)3.2之后官方推薦: 79 update_one()----修改查詢結果的第一條數據 80 db.colleciton.update_one({查詢條件},{$修改器:{修改值}}) 81 update_many()----修改查詢結果所有數據 82 db.colleciton.update_many({查詢條件},{$修改器:{修改值}}) 83 ''' 84 # res5=mycol.update({'name':'c'},{'$set':{'age':23}}) 85 # print(res5) 86 87 # res6=mycol.update_one({'name':'d'},{'$set':{'age':30,'gender':1}}) 88 # print(res6) 89 # res7=mycol.update_many({'name':'d'},{'$set':{'age':22}}) 90 # print(res7) 91 92 93 #4.刪除數據 94 ''' 95 (1)remove()----不在推薦使用 96 db.collection.remove(查詢條件)----刪除符合查詢結果的所有數據 97 (2)3.2之后官方推薦: 98 delete_one()----刪除查詢結果的第一條數據 99 db.colleciton.delete_one(查詢條件) 100 delete_many()----刪除查詢結果的所有數據 101 db.colleciton.delete_many(查詢條件) 102 ''' 103 # res8=mycol.remove({'age':22}) 104 # print(res8) 105 106 # res9=mycol.delete_one({'name':'c'}) 107 # print(res9) 108 109 # res10=mycol.delete_many({'name':'a'}) 110 # print(res10) 111 112 113 #5.數據排序+跳躍+范圍 114 ''' 115 對查詢結果進行排序、跳躍取值、范圍截取 116 117 (1)sort(filed,pymongo.ASCENDING/pymongo.DESCENDING)----對查詢結果升序/降序排序 118 db.collection.find({}).sort() 119 (2)skip(num)----對查詢結果進行跳躍取值 120 db.collection.find({}).skip() 121 (3)limit(num)----對查詢結果進行范圍截取 122 db.collection.find({}).limit() 123 (4)優先級:sort>skip>limit,與使用時的順序無關 124 db.collection.find({}).sort().skip().limit() 125 db.collection.find({}).limit().sort().skip() 126 db.collection.find({}).skip().sort().limit() 127 ''' 128 print('----------------------------') 129 alldata1=mycol.find({}).sort('age',pymongo.ASCENDING)#升序 130 for i in alldata1: 131 print(i) 132 print('----------------------------') 133 alldata2=mycol.find({}).skip(8) 134 for i in alldata2: 135 print(i) 136 print('----------------------------') 137 alldata3=mycol.find({}).limit(2) 138 for i in alldata3: 139 print(i) 140 141 #6.查詢關鍵字 142 ''' 143 (1)$and----並列查詢 144 db.collection.find({'$and':[{},{}...]}) 145 (2)$or----或條件查詢 146 db.collection.find({'$or':[{},{}...]}) 147 (3)$in----范圍查詢 148 db.collection.find({field:{'$in':['',''...]}}) 149 (4)$all----子集查詢 150 db.collection.find({field:{'$all':['',''...]}}) 151 ''' 152 153 print('+++++++++++++++++++++++++++++++') 154 res11=mycol.find({"name":'c'}) 155 for i in res11: 156 print(i) 157 158 print('+++++++++++++++++++++++++++++++') 159 res12=mycol.find({'$and':[{'name':'c'},{'age':20}]}) 160 for i in res12: 161 print(i) 162 163 print('+++++++++++++++++++++++++++++++') 164 res13=mycol.find({'$or':[{'name':'c'},{'name':'b'}]}) 165 for i in res13: 166 print(i) 167 168 print('+++++++++++++++++++++++++++++++') 169 res14=mycol.find({'name':{'$in':['c','b']}}) 170 for i in res14: 171 print(i) 172 173 174 175 print('+++++++++++++++++++++++++++++++') 176 # res15=mycol.find({'hobby':{'$all':['run','eat','swim']}}) 177 res15=mycol.find({'hobby':{'$all':['run','eat']}}) 178 for i in res15: 179 print(i) 180 181 #7.查詢條件操作符(一般用於數字比較) 182 ''' 183 (1)$lt----小於 184 db.collection.find({field:{'$lt':value}}) 185 (2)$gt----大於 186 db.collection.find({field:{'$gt':value}}) 187 (3)$eq----等於 188 db.collection.find({field:{'$eq':value}}) 189 (4)$lte----小於等於 190 db.collection.find({field:{'$lte':value}}) 191 (5)$gte----大於等於 192 db.collection.find({field:{'$gte':value}}) 193 (6)$ne----不等於 194 db.collection.find({field:{'$ne':value}}) 195 ''' 196 197 # mycol.insert_many([{'name':'d','age':18},{'name':'e','age':22},{'name':'f','age':32},]) 198 print("**************************************") 199 res16=mycol.find({'age':{'$lt':21}}) 200 for i in res16: 201 print(i) 202 203 print("**************************************") 204 res17=mycol.find({'age':{'$gt':21}}) 205 for i in res17: 206 print(i) 207 208 print("**************************************") 209 res18=mycol.find({'age':{'$eq':21}}) 210 for i in res18: 211 print(i) 212 print("**************************************") 213 res19=mycol.find({'age':{'$lte':21}}) 214 for i in res19: 215 print(i) 216 217 print("**************************************") 218 res20=mycol.find({'age':{'$gte':21}}) 219 for i in res20: 220 print(i) 221 222 223 print("**************************************") 224 res21=mycol.find({'age':{'$ne':21}}) 225 for i in res21: 226 print(i) 227 228 229 #8.$修改器 + $ 字符特殊用法 230 ''' 231 (1)$set----修改某個字段的值 232 (2)$unset---刪除字段 233 (3)$inc----引用增加(先引用 后增加) 234 (4)針對數組操作: 235 ①$push----在Array的最后一個位置中增加一個數據 236 ②$pushAll----在Array的最后一個位置中增加多個數據 237 ③$pull ----刪除Array中的指定單個元素 238 ④$pullAll ----刪除Array中的指定多個元素 239 ⑤$pop----刪除Array中的第一個或者最后一個元素 正數是倒序刪除 負數是正序刪除 240 ⑥$----存儲當前(Array)符合條件的元素下標索引 ,只能存儲最外層的 索引位置 241 ''' 242 # mycol.update_one({'name':'c','age':20},{'$set':{'hobby':['swim,dump','sing']}}) 243 # mycol.update_one({'$and':[{'name':'c'},{'age':20}]},{"$unset":{'hobby':[1,2]}}) 244 # mycol.update_many({'name':{'$in':['d','e','f']}},{'$inc':{'age':2}}) 245 # mycol.update_one({'name':'b'},{'$push':{'hobby':['swim','sing']}}) 246 247 # mycol.update({'name':'c'},{'$pushAll':{'hobby':['sing','scrapy']}})#實際測試無法使用,報錯:Unknown modifier: $pushAll 248 # mycol.update_many({'name':'b'},{'$pullAll':{'hobby':['swim','play']}})#實際測試每次只刪除一個元素 249 250 # mycol.update_one({'hobby':'run'},{'$pull':{'hobby':'eat'}}) 251 # mycol.update_many({'hobby':'run'},{'$pop':{'hobby':1}}) 252 # mycol.update_many({'hobby':'run'},{'$pop':{'hobby':-1}}) 253 254 # mycol.update_many({'name':'c','age':{'$ne':20}},{'$set':{'hobby':['swim','sing']}}) 255 # mycol.update_many({'hobby':'run'},{'$push':{'hobby':'swim'}})