MongoDB安裝流程:
1.在該網站上下載MongoDB軟件包:
-url:http://dl.mongodb.org/dl/win32/x86_64
-版本(3.4/3.6安裝有問題):win32/mongodb-win32-x86_64-v3.4-latest-signed.msi
2.安裝時自定義安裝路徑(路徑不要有中文字符)
3.在安裝路徑下有一個bin文件,將此路徑寫入環境變量path
4.在控制檯cmd中輸入mongo -version,不報錯代表路徑配置成功
5.在bin文件夾下建立data-db文件夾,在cmd輸入:mongod -dbpath D:\MongoDB\data\db
-在瀏覽器中輸入:localhost:27017/出現:It looks like you are trying to access MongoDB over HTTP on the native driver port.
代表此步驟成功
6.在D:\MongoDB\data\db建立文件夾logs\mongo.log
7.以管理員身份進入cmd-進入D:\MongoDB\bin下-輸入:
mongod -bind_ip 0.0.0.0 -logpath D:\MongoDB\data\logs\mongo.log -logappend -dbpath D:\MongoDB\data\db -port 27017 -serviceName "MongoDB" -serviceDisplayName "MongoDB" -install
8.在計算機-右鍵點擊'管理'-'服務和應用程序'中查看
9.下載mongodb可視化界面:roboMongodbpython
啓動mongoDB:
D:\database\MongoDB\bin> mongod --dbpath D:\database\MongoDB\data\db[代碼啓動]
個人電腦(管理)-服務和應用程序-服務(雙擊)-開啓MongoDB[手動啓動]mongodb
開啓MongoDB服務:
-進入MongoDB安裝的bin目錄下,調出CMD;
-輸入mongod --dbpath D:\database\MongoDB\data\db[建立的db文件夾路徑]
成功開啓的標誌:在瀏覽器中輸入localhost:27017(刷新)出現如下一組文字
[It looks like you are trying to access MongoDB over HTTP on the native driver port.]數據庫
python環境下的MongoDB存儲
1.鏈接MongoDB:
import pymongo
client=pymongo.MongoClient(host='localhost',port=27017)
2.指定數據庫:
db=client['db_name']
3.指定集合:
collection=db['collection_namme']
4.插入數據:insert_one(插入一條數據)/insert_many(插入多條數據)
data={'id':'1001','name':'qinlan','age':20}
collection.insert_one(data)[插入數據]
result=collection.insert_one(data)[pymongo.results.InsertOneResult object at 0x000001F461BD5A48]
print(result.inserted_id)[每插入一條數據,數據庫會分配一個id給該記錄]瀏覽器
a1={'id':'001','name':'liming','age':25}
a2={'id':'002','name':'mayun'}
result=collection.insert_many([a1,a2])[多條記錄以列表形式存放]
print(inserted_ids)
5.查詢記錄:find_one(查詢一條記錄)/find(返回一個生成器對象)
result=collection.find_one({'name':'liming'})
print(type(result),result)[字典類型,記錄結果]app
result=collection.find()
for item in result:
print(item)
補充:
collection.find({'item_name':{'$lt':num}})[item_name屬性值小於num]
collection.find({'item_name':{'$gt':num}})[item_name屬性值大於num]
collection.find({'item_name':{'$gte':num}})[item_name屬性值大於等於num]
collection.find({'item_name':{'$lte':num}})[item_name屬性值小於等於num]
collection.find({'item_name':{'$in':[min,max]}})[item_name屬性值在範圍內]網站
collection.find({'item_name':{'$regex':'pattern'}})[進行正則匹配查詢]
collection.find({'item_name':{'$exists':True}})[查詢屬性item_name存在的記錄]
6.計數(查詢記錄結果):
collection.find().count()/collection.find({}).count()url
7.限制記錄數:
collection.find({}).limit(num)對象
8.更新數據:
condition={'name':'liming'}
item=collection.find_one(condition)[查詢記錄]
item['age']=50[修改屬性值]
collection.update(condition,item)[實現更新]ip
9.刪除數據:
result=collection.delete_one({'attr':value})[刪除一條記錄]
print(result.deleted_count)rem
result=collection.delete_many({'attr':{...}})[刪除多條記錄]
print(result.deleted_count)
result=collection.remove({'attr':value})print(result.deleted_count)