安裝MongoDB環(huán)境:
1.官網(wǎng)下載:https://www.mongodb.com/download-center#community
2.MongoDB可視化工具compass下載https://www.mongodb.com/download-center#compass
筆記
import pymongo
# 獲取連接Mongodb的對象
client = pymongo.MongoClient('127.0.0.1',port=27017)
# 獲取數(shù)據(jù)庫(如果沒有當(dāng)前數(shù)據(jù)庫也沒關(guān)系)
db = client.zhihu
# 獲取數(shù)據(jù)庫的集合(MySQL中的表類似)
collection = db.qa
# 寫入數(shù)據(jù)
# collection.insert({'name':'123'})
# 插入數(shù)據(jù)
# collection.insert_many([
# {
# "username":"bbb",
# "age":18
# },{
# "username":"ccc",
# "age":19
# }
# ])
# 查找數(shù)據(jù)(返回游標(biāo))
# cursor = collection.find()
# for x in cursor:
# print(x)
# 獲取集合中一條數(shù)據(jù)(可以指定條件)
# result = collection.find_one({'name':'123'})
# print(result)
# 更新一條數(shù)據(jù)
# collection.update_one({'username':'ccc'},{'$set':{'username':'aaa'}})
# 更新多條數(shù)據(jù)(第一個參數(shù)是更新那條數(shù)據(jù)沃但,第二個參數(shù)是更新后的數(shù)據(jù))
# collection.update_many({'username':'aaa'},{'$set':{'username':'123'}})
# 刪除一條數(shù)據(jù)
# collection.delete_one({'name':'123'})
# 刪除多條數(shù)據(jù)(第一個參數(shù)是刪除那條數(shù)據(jù))
# collection.delete_many({'username':'123'})
上一篇:數(shù)據(jù)存儲之使用mysql數(shù)據(jù)庫存儲數(shù)據(jù)
下一篇:爬蟲進(jìn)階之多線程爬蟲