[Pythonの爬虫類道路day12]:クローラデータベースのMongoDBの基本的な動作に基づいて

今日では、データベースの基本的な操作を学ぶのMongoDB。
:として記録爬虫類のデータベースの簡単な側面の予備的な理解は、以下の

MongoDBのをしてmysqlの比較
ここに画像を挿入説明
三つの要素:
データベース、文書のコレクション
ここに画像を挿入説明
1.db(現在のデータベースを)
2.show DBS
3.use zhihu
4.db.dropDatabase()
5.db名前.insert(値)のコレクション
6.db.コレクション名.find()ボールドスタイル
ここに画像を挿入説明
管理者モードでは、CMD
:次のコマンドをクリアする

のmongod --config C:\フォルダ\ alwaysuse \ skilllearn \ MONGOALL \ mongod.cfgを- -install
NETスタートMongoDBの
NET STOP MongoDBの
DB
表示DBSの
使用zhihu
db.qa.insert({ "SD": "SDSA"})
を表示DBS
db.qa.find()
db.qa.dropDatabase()#削除する
Pythonコード動作MongoDBのデータベース

import pymongo
#获取连接pymongo的对象
client=pymongo.MongoClient("127.0.0.1",port=27017)
#获取数据库(如果没有zhihu,也没有关系,会创建)
db=client.zhihu
#获取数据库中的集合(也就是mysql的表)
collection=db.qa
#插入数据
 collection.insert_one({"username":"aaaa"})
# #插入多条数据
collection.insert_many([
     {"username":"222","age":"18"},
    {"username":"3f22","age":"28"}
 ])
#查找所有数据find()
 cursor=collection.find()
 for s in cursor:
     print(s)
#查找一条数据
result=collection.find_one({"age":"28"})
print(result)
#更新数据(一条,多条)
collection.update_one({"username":"222"},{"$set":{"username":"ccc"}})
collection.update_many({"username":"aaaa"},{"$set":{"username":"cccb"}})
#删除数据
collection.delete_one({"username":"cccb"})
collection.delete_many({"username":"cccb"})
公開された12元の記事 ウォンの賞賛3 ビュー2205

おすすめ

転載: blog.csdn.net/dinnersize/article/details/104522152