Author: Unreal good

Source: Hang Seng LIGHT Cloud Community

An overview of the

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

In this paper, Python will be used to connect to MongoDB, and the operation practice of adding, deleting and modifying data will be carried out. Readers need to have a certain understanding of Python or MongoDB first, so that they can get started quickly.

Environment to prepare

System: Windows 10 X64

Python:

  • Version: 3.7
  • Development tool: Pycharm

MongoDB:

  • Version: 4. X
  • Visualization tool: MongoDB Compass

steps

Start the Mongo Service

Start the MongoDB service locally and connect to the database service using MongoDB Compass:

Installing the Tool Package

Install pyMongo via Pycharm, a python driver that enables python programs to use the MongoDB database:

Write check connection

Create a.py file, write the MongoDB connection program, check whether the connection is successful by querying:

MongoClient = pymongo import MongoClient from pymongo Access to the client via a URL = MongoClient (' mongo: / / localhost: 27017 / ') # test client connection, Can query whether or not the document data query properly for the I in the client. Newdb. Lightmap. Find ({}) : print (I)Copy the code

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

{" _id ": ObjectId (' 61 bc3f6d9e58737faea3c5cc '), 'name' : 'palace', 'city' : 'Beijing', 'country' : 'China' and 'GPS' : {" lat" : 116.403, 'LNG: 39.924}} {" _id ": ObjectId (' 61 bc3f6d9e58737faea3c5cd'), 'name' : 'the wall', 'city' : 'Beijing', 'the country: 'China' and 'GPS' : {' lat: 106.384, 'LNG: 39.031}} {" _id ": ObjectId (' 61 bc3f6d9e58737faea3c5ce'), 'name' : 'White House', 'city' : 'Washington', 'country', 'us',' GPS ': {' lat: 116.652,' LNG: 40.121}} {" _id ": ObjectId (' 61 bc3f6d9e58737faea3c5cf '), 'name' : 'London eye', 'city' : 'London', 'country' : 'the UK', 'GPS' : {' lat: 116.348, 'LNG: 34.43}}Copy the code

Write programs to operate MongoDB

Database operations

Select * from new_db; select * from new_db; New_db_one = client.new_db_one # db_names = client.list_database_names() print(db_names) # Drop existing database client.drop_database('new_db_one')Copy the code

Operation of set

Client.new_db_one.collection_name. Insert_one ({'light': 'hs'}) # Client.new_db_one.collection_names (); client.new_db_one.collection_names ();Copy the code

Document operation

New_db_one.collection_name insert_one({'light': 'hs'}) for i in client.new_db_one.collection_name.find({}): New_db_one. collection_name. Replace_one ({'light': 'hs'}, {'hs2': 'light2'}) for i in client.new_db_one.collection_name.find({}): New_db_one.collection_name. Find ({'hs2': 'light2'}) for i in client.new_db_one.collection_name.find({}): New_db_one. collection_name. Delete_one ({'hs2': 'light2'}) for i in client.new_db_one.collection_name.find({}): print(i)Copy the code

conclusion

This article through the use of Python to MongoDB database, collection, document operation practice, convenient readers quickly start to practice it.


Want to learn more from the tech gurus? Where are problems encountered during development discussed? How to access the massive resources of fintech?

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