1.Pymongo
Pymongo is the Python interface development package for MongoDB and is the recommended way to use Python and MongoDB.
Official documents
2. Installation
>>> post_id ObjectId(...)>>> posts.find_one({"_id": post_id}){u‘date‘: datetime.datetime(...), u‘text‘: u‘My first blog post!‘, u‘_id‘: ObjectId(‘...‘), u‘author‘: u‘Mike‘, u‘tags‘: [u‘mongodb‘, u‘python‘, u‘pymongo‘]}
>>> post_id_as_str = str(post_id)>>> posts.find_one({"_id": post_id_as_str}) # No result >>>
from bson.objectid import ObjectId # The web framework gets post_id from the URL and passes it as a string def get(post_id): # Convert from string to ObjectId: document = client.db.collection.find_one({‘_id‘: ObjectId(post_id)})
4.mongoDB Other operations
1. Super Admin
For more secure access to MongoDB, visitors are required to provide a user name and password, so users need to be created in MongoDB
Using the role-user-database security management method
Common system roles are as follows:
sudo vi /etc/mongod.conf
security: authorization: enabled
sudo service mongod stopsudo service mongod start
mongo -u ‘admin‘ -p ‘123‘ --authenticationDatabase ‘admin‘
2. master-Slave Dual Standby
Advantages of replication
Replication provides redundant backups of data, stores copies of data on multiple servers, improves data availability, and ensures data security
Replication also allows recovery of data from hardware failures and service outages
mongo --host 192.168.10.111 --port 27019
use test1for(i=0;i<10;i++){db.t1.insert({_id:i})}db.t1.find()
3. Backup
5.Mongodb interacting with Python
With the Robo 3T visualizer we can see that 137 data has been fetched and stored in MongoDB
6. Complete the Command line project: Student Information Management (based on Python2.7)
Has succeeded!
You are welcome to follow my blog: https://home.cnblogs.com/u/sm123456/
Welcome to join thousands of people to communicate questions and Answers group: 125240963
MongoDB interacts with Python! This is the right way to play the database correctly!