reference resources Python operates MongoDB. Just read this article
1. Install MongoDB
windows download address of mongodb
MongoDB provides precompiled binary packages.
(1) Download msi file. After downloading, double-click the file and install it according to the operating instructions.
During installation, you can set your installation directory by clicking the "custom" button.
D:\MongoDB\Server\5.0\.
Configure the environment variable D:\MongoDB\Server.0\bin.
(2) The next step is to install "install mongoDB compass" without checking it (of course, you can choose to install it, which may take a longer installation time). MongoDB Compass is a graphical interface management tool. We can download and install it on the official website later.
(3) Create directory
MongoDB stores the data directory in the db directory. However, this data directory will not be created actively. We need to create it after installation. Please note that the data directory should be placed in the root directory (e.g. C: \ or D: \).
D:\data\db.
(4) Run the MongoDB server from the command line
CMD>mongod --dbpath D:\data\db
(5) Connect MongoDB
CMD>mongo
2. Configure MongoDB service
3 python operation mongodb
Please ensure that MongoDB is installed and its service is started, and Python's PyMongo library is installed.
3.1 connection information
import pymongo # (1) Connect mongodb #client = pymongo.MongoClient(host='localhost',port=27017) client = pymongo.MongoClient('mongodb://localhost:27017/') # (2) Specify database #Multiple databases can be established in MongoDB #db = client.test # Mode 1 db = client['test'] # (3) Specify collection # Each database contains many collection s, similar to tables in a relational database. #collection = db.students # Mode 1 collection = db['students'] print(collection)
Insert is officially recommended_ One() and insert_ The many () method to insert a single record and multiple records, respectively.
3.2 insert one
import pymongo client = pymongo.MongoClient(host='localhost',port=27017) # connect db = client.test # Specify database collection = db.students # Specify collection # Insert a student = { 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male' } result = collection.insert_one(student) print(result) print(result.inserted_id)
The output is as follows
<pymongo.results.InsertOneResult object at 0x000001EE321E7348>
6194bd1c4688ef8efc735c66
3.3 insert multiple
import pymongo client = pymongo.MongoClient(host='localhost',port=27017) # connect db = client.test # Specify database collection = db.students # Specify collection # Insert multiple student1 = { 'id': '20170102', 'name': 'lucy', 'age': 20, 'gender': 'male' } student2 = { 'id': '20170203', 'name': 'Mike', 'age': 21, 'gender': 'male' } result = collection.insert_many([student1, student2]) print(result) print(result.inserted_ids)
The output is as follows
<pymongo.results.InsertManyResult object at 0x000001F5161B0448>
[ObjectId('6194bd84c61333642af39309'), ObjectId('6194bd84c61333642af3930a')]
3.4 query one
After inserting data, we can use find_one() or find() method, where find_one() gets a single result, and find() returns a generator object.
import pymongo client = pymongo.MongoClient(host='localhost',port=27017) # connect db = client.test # Specify database collection = db.students # Specify collection result = collection.find_one({'name': 'Mike'}) print(type(result)) print(result)
The output is as follows
<class 'dict'>
{'_id': ObjectId('6194bd84c61333642af3930a'), 'id': '20170203', 'name': 'Mike', 'age': 21, 'gender': 'male'}
As you can see, it's too much_ id attribute, which is automatically added by MongoDB during insertion.
3.4 query multiple items
import pymongo client = pymongo.MongoClient(host='localhost',port=27017) # connect db = client.test # Specify database collection = db.students # Specify collection results = collection.find({'age': 20}) print(type(results)) print(results) for result in results: print(result)
The output is as follows
<class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x000001937FBC6860>
{'_id': ObjectId('6194bd1c4688ef8efc735c66'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('6194bd84c61333642af39309'), 'id': '20170102', 'name': 'lucy', 'age': 20, 'gender': 'male'}
The returned result is the Cursor type, which is equivalent to a generator. We need to traverse to get all the results, and each result is a dictionary type.