[Python reptile road day12]: based on the basic operation of the crawler database mongodb

Today mongodb learning the basic operation of the database.
A preliminary understanding of the database simple aspects of reptiles, recorded as follows:

MongoDB and compare mysql
Here Insert Picture Description
three elements:
a database, a collection of documents
Here Insert Picture Description
1.db (current database)
2.show dbs
3.use zhihu
4.db.dropDatabase ()
5.db A collection of name .insert (value)
6.db. collection name .find () bold style
Here Insert Picture Description
with administrator mode cmd
clear the following command:

the 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 () # remove
python code operation mongodb database

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"})
Published 12 original articles · won praise 3 · Views 2205

Guess you like

Origin blog.csdn.net/dinnersize/article/details/104522152