[python爬虫之路day12]:基于爬虫的mongodb数据库的基本操作

今天学习了mongodb数据库的基本操作。
初步了解数据库的爬虫方面的简单操作,记录如下:

mongodb和mysql比较
在这里插入图片描述
三元素:
数据库,集合,文档
在这里插入图片描述
1.db (当前数据库)
2.show dbs
3.use zhihu
4.db.dropDatabase()
5.db.集合名.insert(value)
6.db.集合名.find() 加粗样式
在这里插入图片描述
使用管理员模式运行cmd
明确以下命令:

mongod --config C:\folders\alwaysuse\skilllearn\MONGOALL\mongod.cfg --install
net start mongodb
net stop mongodb
db
show dbs
use zhihu
db.qa.insert({“sd”:“sdsa”})
show 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"})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章