[MongoDB study notes] teach you how to configure Python to operate MongoDB

Time:2022-8-5

Author: Phantom

source:Hang Seng LIGHT Cloud Community

Overview

In crawler-related projects, sometimes it is necessary to crawl data of various data structures. For more convenient storage, we usually use MongoDB for storage.

This article will use Python to connect to MongoDB and practice the operation of adding, deleting, and modifying data. Readers need to have a certain understanding of Python or MongoDB to get started faster.

Environmental preparation

System: windows 10 x64

Python:

  • Version: 3.7
  • Development tool: Pycharm

MongoDB:

  • Version: 4.x
  • Visualizer: MongoDB Compass

Steps

Start Mongo Service

First start the MongoDB service locally and connect to the database service using MongoDB Compass:

[MongoDB study notes] teach you how to configure Python to operate MongoDB

installation kit

Then install the tool library pymongo on the development tool Pycharm (pymongo is a driver that enables python programs to use the MongoDB database, written in python):

[MongoDB study notes] teach you how to configure Python to operate MongoDB

write check connection

create a new one.pyfile, write a MongoDB connection program, and check whether the connection is successful by querying:

# Import MongoClient in the pymongo library
from pymongo import MongoClient

# Connect to the MongoDB database and access it by URL
client = MongoClient('mongodb://localhost:27017/')

# Detect client connection, you can query whether the document data can be queried normally
for i in client.newdb.lightmap.find({}):
    print(i)

If no error is reported and the output is successfully printed, the connection is successful:

{'_id': ObjectId('61bc3f6d9e58737faea3c5cc'), 'name': 'Forbidden City', 'city': 'Beijing', 'country': 'China', 'gps': {'lat': 116.403, 'lng' : 39.924}}
{'_id': ObjectId('61bc3f6d9e58737faea3c5cd'), 'name': 'Great Wall', 'city': 'Beijing', 'country': 'China', 'gps': {'lat': 106.384, 'lng' : 39.031}}
{'_id': ObjectId('61bc3f6d9e58737faea3c5ce'), 'name': 'White House', 'city': 'Washington', 'country': 'United States', 'gps': {'lat': 116.652, 'lng' : 40.121}}
{'_id': ObjectId('61bc3f6d9e58737faea3c5cf'), 'name': 'London Eye', 'city': 'London', 'country': 'United Kingdom', 'gps': {'lat': 116.348, ' lng': 34.43}}

Write programs to operate MongoDB

Operation of the database

# Add or get a database, if the database (new_db) does not exist, the system will automatically create the database (new_db)
new_db_one = client.new_db_one

# Query database list
db_names = client.list_database_names()
print(db_names)

# delete existing database
client.drop_database('new_db_one')

set operations

# The addition operation of the collection, if the collection_name collection does not exist, it will be created, and if it exists, the data will be inserted directly
client.new_db_one.collection_name.insert_one({'light': 'hs'})

# Set query operation, query the list of set names corresponding to the database
client.new_db_one.list_collection_names()

# Collection delete operation
client.new_db_one.collection_name.drop()

Operations on documents

# New document operation, add a document to the collection collection_name
client.new_db_one.collection_name.insert_one({'light': 'hs'})
for i in client.new_db_one.collection_name.find({}):
    print(i)

# New document operation, add a document to the collection collection_name
client.new_db_one.collection_name.replace_one({'light': 'hs'}, {'hs2': 'light2'})
for i in client.new_db_one.collection_name.find({}):
    print(i)

# New document operation, add a document to the collection collection_name
client.new_db_one.collection_name.find({'hs2': 'light2'})
for i in client.new_db_one.collection_name.find({}):
    print(i)

# Document deletion operation, the first matching data is deleted
client.new_db_one.collection_name.delete_one({'hs2': 'light2'})
for i in client.new_db_one.collection_name.find({}):
    print(i)

Summarize

This article uses python to operate MongoDB's databases, collections, and documents, which is convenient for readers to get started quickly, so let's practice quickly.


Want to learn more from the tech giants? Where are the problems encountered in development discussed? How to obtain the massive resources of financial technology?

Hang Seng LIGHT Cloud Community, a professional community platform for financial technology built by Hang Seng Electronics, sharing practical technical dry goods, resource data, financial technology industry trends, and embracing all financial developers.

Scan the QR code of the applet below to join us!

[MongoDB study notes] teach you how to configure Python to operate MongoDB