This is the 25th day of my participation in Gwen Challenge

If ❤️ my article is helpful, welcome to comment, follow, like. This is the biggest encouragement for me to continue my technical creation. More previous posts on my personal blog

Python operation mongo

In the last article, we covered in detail how to install and use the MongoDB database. The MongoDB database service is ready. How to use visualization software Robo 3T

But in the actual crawler process, data capture and data storage are completed automatically by the program. So we need to write the program code to let the crawler to store and manage the data by itself. In this article we will learn how to store and manage data in Python

Python first needs to install the MongoDB driver Pymongo extension package.

Install the Pymongo extension pack

Enter thePypi Expansion Pack websitehttps://pypi.org/Search,pymongo, look at the introduction according to their needs to choose. I’m going to pick the first term here

Copy the PIP installation command inProject virtual environment VENvThe installation

Open the PyCharm development tool. Click on the belowTerminalCommand line tools. In this case, check whether it is in the PY_VENv virtual environmentD:\Dev\env\py_venv\Scripts\activate ). Pasting installation commandspip install pymongoenterEnterExecute the installation command.If the installation speed is too slow, refer toVirtualenv Sets up the project running environmentChapter, using domestic PIP source acceleration

appearSuccessfully installed pymongo - 3.10.1Message indicates successful installation

Use Pymongo to operate databases

Pymongo operates on a database, which is basically divided into three parts:

  • Connect to the database and specify the collection
  • Add, delete, change and check the collection
  • Aggregate, sort, summarize queries…

Without further ado, we create the pymongoutil.py file under the D: Dev workplace py project. And then we start writing our code