Python操作Mongodb

來源:互聯網
上載者:User

標籤:Python   資料庫   爬蟲   程式員   

一 匯入 pymongo

from pymongo import MongoClient

二 串連伺服器 連接埠號碼 27017

串連MongoDB
串連MongoDB我們需要使用PyMongo庫裡面的MongoClient,一般來說傳入MongoDB的IP及連接埠即可,第一個參數為地址host,第二個參數為連接埠port,連接埠如果不傳預設是27017。
conn = MongoClient("localhost")
MongoClient(host=‘127.0.0.1‘,port=27017)

三 串連資料庫

db = conn.資料庫名稱
串連集合
collection = db[collection_name]
or
collection = db.collection_name
查看全部聚集名稱
db.collection_names()

四 插入資料(1) 插入一條資料

db.user.insert({"name":"夏利剛","age":18,"hobby":"學習"})

(2) 插入多條資料

db.user.insert([{"name":"夏利剛","age":18,"hobby":"學習"},{"name":"xxxoo","age":48,"hobby":"學習"}]

(3) 在3.x以上 建議 使用

insert_one 插入一條資料
insert_many() 插入多條資料

(4) 返回 id 使用insert_one()

data.inserted_id
data.inserted_ids

五 查詢資料(1) 查詢所有
db.user.find()#帶條件的查詢# data = db.user.find({"name":"周日"})# print(data) #返回result類似一個迭代器  可以使用 next方法 一個一個 的取出來# print(next(data))   #取出一條資料
(2) 查詢一條
db.user.find_one()
(3) 帶條件查詢
db.user.find({"name":"張三"})
(4) 查詢 id
from bson.objectid import ObjectId*#用於ID查詢data = db.user.find({"_id":ObjectId("59a2d304b961661b209f8da1")})
(5) 模糊查詢
(1){"name":{‘$regex‘:"張"}}(2)import re {‘xxx‘:re.compile(‘xxx‘)}
六 sort limit count skip(1) sort 排序

? 年齡 大於10

data = db.user.find({"age":{"$gt":10}}).sort("age",-1) #年齡 升序 查詢 pymongo.ASCENDING --升序data = db.user.find({"age":{"$gt":10}}).sort("age",1) #年齡 降序 查詢 pymongo.DESCENDING --降序
(2) limit 取值

? 取三條資料

db.user.find().limit(3)data = db.user.find({"age":{"$gt":10}}).sort("age",-1).limit(3)
(3) count 統計資料條數
db.user.find().count()
(4) skip 從第幾條資料開始取
db.user.find().skip(2)
七 update 修改

? update()方法其實也是官方不推薦使用的方法,在這裡也分了update_one()方法和update_many()方法,用法更加嚴格,

(1) update()
db.user.update({"name":"張三"},{"$set":{"age":25}})db.user.update({"name":"張三"},{"$inc":{"age":25}})
(2) update_one() 第一條合格資料進行更新
? db.user.update_one({"name":"張三"},{"$set":{"age":99}})
(3) update_many() 將所有合格資料都更新
db.user.update_many({"name":"張三"},{"$set":{"age":91}})
(4) 其返回結果是UpdateResult類型,然後調用matched_count和modified_count屬性分別可以獲得匹配的資料條數和影響的資料條數。
print(result.matched_count, result.modified_count)沒
八 remove 刪除

刪除操作比較簡單,直接調用remove()方法指定刪除的條件即可,合格所有資料均會被刪除,

(1) 刪除 張三
collection.remove({"name":"lilei"})
(2) 全部刪除
collection.remove()
(3) 依然存在兩個新的推薦方法,delete_one()和delete_many()方法,樣本如下:
delete_one()即刪除第一條合格資料collection.delete_one({“name”:“ Kevin”})delete_many()即刪除所有合格資料,返回結果是DeleteResult類型collection.delete_many({“age”: {$lt:25}})
(4) 可以調用deleted_count屬性擷取刪除的資料條數。
result.deleted_count
九 關閉串連
conn.close()

Python操作Mongodb

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.