自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python也能操作MongoDB數(shù)據(jù)庫

開發(fā) 后端 其他數(shù)據(jù)庫 MongoDB
作為非關(guān)系數(shù)據(jù)庫的代表--Mongo,可以說是讓人又愛又恨,讓人愛的是它的便捷性,讓人恨的是它的配置,實在是坑多。那么今天我們就來深入剖析它吧。

大家好,我是Python進階者。

前言

作為非關(guān)系數(shù)據(jù)庫的代表--Mongo,可以說是讓人又愛又恨,讓人愛的是它的便捷性,讓人恨的是它的配置,實在是坑多。那么今天我們就來深入剖析它吧。

一、下載并導入Python 連接Mongo的模塊

  1. pip install pymongo 
  2. from pymongo import MongoClient 

二、連接Mongo數(shù)據(jù)庫

1.普通登錄,又稱游客登陸,安全等級低

  1. MongoClient('mongodb://localhost:27017/'

2.用戶密碼登陸,安全等級高

  1. MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'

這里連接到了用戶名為hwzjj,密碼為123456的用戶。

三、執(zhí)行插入操作

為了安全,我們使用用戶名和密碼登陸,然后創(chuàng)建一個集合,不知道大家對Mongo創(chuàng)建集合還有沒有印象,反正小編還有,廢話不多說,先創(chuàng)建兩個集合。

  1. db.createCollection(name='student',option={capped:true,autoIndexId:true,size:100,max:1000}) 
  2. db.createCollection(name='teacher',option={capped:true,autoIndexId:true,size:200,max:2000}) 

這樣就創(chuàng)建了一student和teacher的集合了。然后我們再來顯示一下所有的集合名:

  1. show collections; 

然后我們往集合里插入數(shù)據(jù),在Mongo中是這樣插入的:

可以看到我們成功插入了兩條數(shù)據(jù),接下來我們利用Python來插入數(shù)據(jù)。

1.直接使用創(chuàng)建好的集合插入數(shù)據(jù)

  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw') 連接數(shù)據(jù)庫 
  3. db=client['hw']        選擇數(shù)據(jù)庫hw 
  4. coll=db['student']     選擇集合 
  5. res={'id':'0003','name':'任性','age':43} 
  6. first=coll.insert_one(res)  將數(shù)據(jù)插入到集合中 
  7. print(first.inserted_id)   打印插入數(shù)據(jù)的id(每個插入數(shù)據(jù)都會有) 

2.自己創(chuàng)建集合插入數(shù)據(jù)

  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'
  3. db=client['hw'
  4. db.create_collection('teacher')  創(chuàng)建集合 
  5. res={'id':'0001','name':'boy','age':36} 
  6. last=db.student.insert_one(res)  插入數(shù)據(jù) 
  7. print(last.inserted_id) 打印id 

3.插入多條數(shù)據(jù)

  1. import random 
  2. from pymongo import MongoClient 
  3. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'
  4. db=client['hw'
  5. coll=db['student'
  6. def get(): 
  7.     for y in range(100000): 
  8.         data={'id':y,'name':'user--'+str(y),'age':random.choice(range(100))} 
  9.         yield data 
  10. for y in get(): 
  11.     coll.insert(y) 

同樣是插入十萬個數(shù)據(jù), 不過數(shù)據(jù)卻是比Mysql慢一點,可自行測試。

注:執(zhí)行插入操作時,Insert最多可插入四條同樣的記錄。

四、執(zhí)行更改操作

仍舊是先要獲取集合,然后對集合中的內(nèi)容進行修改。

1.更新匹配到的第一條數(shù)據(jù)

  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'
  3. db=client['hw'
  4. coll=db['student'
  5. coll.update_one({'name':'user--10'},{'$set':{'name':'用戶已注銷'}}) 更新匹配到的第一條數(shù)據(jù) 

2.更新匹配到的所有數(shù)據(jù)

我們創(chuàng)建四個一樣的數(shù)據(jù),將程序執(zhí)行四次即可:

  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'
  3. db=client['hw'
  4. coll=db['student'
  5. coll.insert({'id':'111','name':'hw','age':43}) 

可以看到生成了四個同樣的記錄,當然了,只能生成最多4條記錄。然后我們?nèi)繉⑺鼈償?shù)據(jù)修改。

  1. coll.update({'name':'hw'},{'$set':{'name':'用戶已注冊'}}) 

五、執(zhí)行刪除操作

1.刪除所有符合條件的數(shù)據(jù)

  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'
  3. db=client['hw'
  4. coll=db['student'
  5. coll.insert({'id':'111','name':'hw','age':43}) 插入數(shù)據(jù) 
  6. coll.remove({'name':'hw'}) 刪除所有name 為hw的數(shù)據(jù),注意不要以id為條件來刪除,會報錯 
  7. coll.delete_many({'name':'hw'}) 跟上者功能一樣 

2.刪除所有符合條件的第一條數(shù)據(jù)

  1. from pymongo import MongoClient 
  2. client=MongoClient('mongodb://hwzjj:123456@localhost:27017/hw'
  3. db=client['hw'
  4. coll=db['student'
  5. coll.insert({'id':'111','name':'hw','age':43}) 
  6. coll.delete_one({'name':'hw'}) 刪除符合條件的第一條數(shù)據(jù) 

六、執(zhí)行查詢操作

1.查詢符合條件的第一條數(shù)據(jù)

2.查詢符合條件的所有數(shù)據(jù)

3.查找后刪除

4.查找后替換

5.查找后更新

6.統(tǒng)計符合條件的記錄數(shù)量

  1. coll.find().count() # 記錄符合條件的數(shù)量 

7.符合條件的數(shù)據(jù)的排序

  1. coll.find().sort('name', pymongo.ASCENDING) # 升序排序 DESCENDING 降序排序 

8.符合條件數(shù)量中跳過

  1. https://mp.weixin.qq.com/s/34t_u-JxL3HFXvEdtgFQEg#:~:text=coll.find().sort(%27name%27%2C%20pymongo.ASCENDING).skip(1)%20%23%20%E8%B7%B3%E8%BF%87%E4%B8%80%E4%B8%AA%E8%AE%B0%E5%BD%95 

9.限制符合條件輸出數(shù)量

  1. coll.find().sort('name', pymongo.ASCENDING).limit(2) # 輸出兩個符合條件的記錄 

10.通過Id來查找

每個插入的數(shù)據(jù)都會生成一個id,貌似被加密了,前面我們已經(jīng)和它打過交道了,下面來看下它的使用。

  1. from bson.objectid import ObjectId 
  2. find_one({'_id': ObjectId(id_name)}) 

七、索引操作

1.創(chuàng)建索引

可以看到有兩個索引,一個是Mongo自動創(chuàng)建的在id上的索引,另一個是剛剛創(chuàng)建在name上的索引。

2.獲取索引

  1. for y in coll.list_indexes(): # 獲取所有索引 
  2.   print(y) 

3.刪除索引

可以看到剛剛的索引name已經(jīng)被刪除了,而且只有一條數(shù)據(jù)了,那么有人就問了,為何不把_id一起刪除,很抱歉,這個是刪不了的。

八、總結(jié)

通過本章對Pymongo的學習,相信你已經(jīng)可以勝任日常一些開發(fā)了,Pymongo中還有很多值得學習的地方,值得你去推敲,在這里就不一一列舉了,希望本文能帶大家零基礎(chǔ)毫無壓力入門Pymongo。

 

責任編輯:姜華 來源: Python爬蟲與數(shù)據(jù)挖掘
相關(guān)推薦

2021-08-02 09:01:29

PythonMySQL 數(shù)據(jù)庫

2015-03-13 15:30:26

編程數(shù)據(jù)庫創(chuàng)建表單

2019-05-31 08:23:00

Oracle數(shù)據(jù)庫云渡劫

2024-05-08 08:37:44

2021-06-03 09:30:30

Python操作注冊表regedit

2010-03-04 13:47:13

Python操作Acc

2024-11-28 08:07:14

2010-03-04 15:31:44

Python SQLI

2010-09-27 14:54:38

SQL數(shù)據(jù)庫

2010-08-31 16:53:54

DHCP數(shù)據(jù)庫

2020-07-06 14:20:43

MongoDB數(shù)據(jù)庫安全

2019-08-20 14:02:07

MongoDB數(shù)據(jù)庫恢復數(shù)據(jù)

2020-11-16 08:56:02

Python

2022-03-10 09:08:43

數(shù)據(jù)庫Mongodb數(shù)據(jù)庫轉(zhuǎn)

2023-04-27 09:36:43

2017-11-29 13:11:37

PythonOracle中文查詢報錯

2024-01-31 08:23:54

2011-08-02 17:06:29

Oracle遠程數(shù)據(jù)庫創(chuàng)建DB Link

2011-07-05 10:16:16

Qt 數(shù)據(jù)庫 SQLite

2019-10-21 13:52:14

MySQL數(shù)據(jù)庫命令
點贊
收藏

51CTO技術(shù)棧公眾號