python_crawler 11 MongoDB database

table of Contents

1. Install mongodb on windows

Two, mongodb concept introduction

Three, mongodb three elements

Four, mongodb manipulation commands

Five, python operation mongodb

1. Install mongodb on windows

1. Download the mongodb installation package from the server, here is the zip installation package, download link:

2. After downloading, create a data folder under the folder, and then enter the bin folder under the unzipped folder, the directory structure is as follows

3. Execute the command mongod -dbpath "D:\mongodb\data" in the bin folder to   start the mongodb service, the results are as follows

4. Open another command line window, execute mongo in the bin directory to connect to the mongodb service. Execute show dbs; you can see the existing db.MongoDB will create admin, config, and local libraries by default.

5. Create a new mongodb.conf and modify the contents of the configuration file mongodb.conf as follows:

# 端口号,默认为27017port=27017 # 数据库存放路径dbpath=D:\mongodb\data # 日志存放路径logpath=D:\mongodb\mongodb.log # 是否追加日志logappend=true # 是否启用日志,默认启用journal=true # 是否过滤无用日志信息quiet=true

6. Register MongoDB as a Windows service

①Close all the previous cmd windows, open the new administrator cmd, enter the bin directory of MongoDB, and enter

mongod -config "D:\mongodb\mongodb.conf" --install --serviceName "MongoDB"

②At this time, the MongoDB service can be generated in the service list of win + r, services.msc, which can be set to self-start after booting, and then there is no need to manually start MongoDB every time

If you want to start and stop the mongodb service, you can execute the following commands as an administrator :

Start: net start MongoDB

Stop: net stop MongoDB

7. Configure the bin directory of MongoDB under the path environment variable, and then you can enter mongo login anywhere

Two, mongodb concept introduction

SQL terms/conceptsMongoDB terms/conceptsexplain
tablecollectionDatabase table/collection
rowdocumentData record line/document
columnfieldData field/domain
joinsjoinsTable connection, mongodb does not support
primary keyprimary keyPrimary key, MongoDB automatically sets the _id field as the primary key

Three, mongodb three elements

Three elements: database, collection, document

1. Document: A row in a relational database. A document is an object composed of key-value pairs, which is an extended form of json.

{"_id": ObjectId("60bb738d1b680d63598f15a1"), "username": "aaa"}

2. Collection: It is a table in a relational database. Multiple documents can be stored, and the structure can be variable. The following data can be stored in the same collection:

{"username": "aaa"}
{"age": 17}
{"username": "aaa", "age": 17}

Four, mongodb manipulation commands

db : View the current database

show dbs : View all databases

use database name : switch database. If the database does not exist, create the database (you must insert data after creation to be considered successful)

db.collection name.insert(value) : add data to the specified collection

db.collection name.find() : Find data from the specified collection.

Five, python operation mongodb

Install the dependent modules first: pip3 install pymongo

import pymongo # 获取连接 mongodb 对象,默认端口:27017client = pymongo.MongoClient("", port=27017) # 获取数据库(如果没有test这个数据库也没关系)db = client.test # 获取数据库中的集合(也就是 mysql 中的表)collection =  # 1、insert_one 加入一条文档数据到集合中collection.insert_one({"username":"aaa"}) # 2、insert_many: 加入多条文档数据到集合中collection.insert_many([{"username":"a2"},{"username":"a1"}])  # 3、find_one: 查找一条文档对象res = collection.find_one()print(res)  # {'_id': ObjectId('60bb6e2151ee3420356af4f0'), 'username': 'aaa'} # 4、find : 查找所有文档对象cursor = collection.find()print(res)  # <pymongo.cursor.Cursor object at 0x000001F7FEE62BA8>for d in cursor:    print(d)  # 5、update_one: 更新一条文档对象collection.update_one({"username":"a1"}, {"$set":{"username":"a1", "age":18}}) # 6、update_many: 更新多条文档对象collection.update_many({"username":"a1"}, {"$set":{"username":"a1", "age":18}})  # 7、delete_one: 删除一条文档对象collection.delete_one({"username":"a1", "age":18}) # 8、delete_many: 删除多条文档对象collection.delete_many({"username":"aaa"})

table of Contents