MongoDB interacts with Python! This is the right way to play the database correctly!

Source: Internet
Author: User
Tags datetime

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

    • Search by Objectid.

>>> 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‘]}
    • Do not convert the type of Objectid to string

>>> post_id_as_str = str(post_id)>>> posts.find_one({"_id": post_id_as_str}) # No result >>>
    • If post_id is a string

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:

    • Enable secure authentication

      Modifying a configuration file

sudo vi /etc/mongod.conf
    • Enable authentication

      Note: You must add a space between keys and values, or the parsing will error

security: authorization: enabled
    • Restart Service

sudo service mongod stopsudo service mongod start
    • Terminal connection

mongo -u ‘admin‘ -p ‘123‘ --authenticationDatabase ‘admin‘
    • General user Management

      Log in with Super Administrator and then go to user management action

      View users of the current database

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

    • STEP8: New open window, connecting the second MONGO service

mongo --host 192.168.10.111 --port 27019
    • STEP9: Inserting data into the primary server

use test1for(i=0;i<10;i++){db.t1.insert({_id:i})}db.t1.find()

3. Backup

    • Grammar

5.Mongodb interacting with Python
    • Before we learned the crawler, we now store the crawled data in MongoDB.

    • Turn on MONGO, run code

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)
    • Code manipulation

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.