Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect and access MongoDB in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to connect to MongoDB in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. From Mongo Shell to application access interface

Mongo Shell is an official database access interface program provided by MongoDB, which is similar to Oracle's sqlplus and Mysql's mysql program. It should be said that Mongo shell is the best for MongoDB connection, access, and functional operation. All MongoDB-related management, development, and data access functions can be implemented through Mongo Shell.

The other is the application access interface, which can also be understood as the programming language interface. As with all other databases, MongoDB is supported by a dedicated access driver Driver, or Module, for each development language. However, due to the differences in syntax, semantics and program organization among different languages, there are some differences between using the program access interface and the standard Mongo Shell. This is why multiple programming language versions are available for each operation in the official MongoDB documentation.

Currently, MongoDB provides language drivers, including Mongo Shell (native support packages are also required), Python, Java, Node.js, PHP, C #, Perl, Ruby, and Scala. This article mainly introduces the way to visit Python, write it down and leave it for friends who need it to check.

2. Python driver installation

Python is a popular programming language at present, especially in artificial intelligence AI and big data analysis and processing, the market space is relatively large. MongoDB is a popular NOSQL database solution, and there are many scenarios that combine the two.

For Python, the organization process is carried out in the form of Module. To develop locally, you need to add local support for MongoDB based on the Python standard library. At present, the more commonly used Mongodb package is pymongo.

If you are in the Windows environment, if you have installed the Python development environment JDK, you only need to call the esay_install program to download it automatically.

C:\ Users\ admin > easy_install pymongo

Searching for pymongo

Reading https://pypi.python.org/simple/pymongo/

Best match: pymongo 3.4.0

Downloading https://pypi.python.org/packages/e7/7c/6c6fa7f0c416e227445979403f14c

4fcfc5960f7c220e8ad0370197fe87a/pymongo-3.4.0-py3.5-win-amd64.egg#md5=0fa1f3d995

42f032fc8940d8d53d7559

Processing pymongo-3.4.0-py3.5-win-amd64.egg

Creating c:\ program files\ python35\ lib\ site-packages\ pymongo-3.4.0-py3.5-win-amd

64.egg

Extracting pymongo-3.4.0-py3.5-win-amd64.egg to c:\ program files\ python35\ lib\ si

Te-packages

Adding pymongo 3.4.0 to easy-install.pth file

Installed c:\ program files\ python35\ lib\ site-packages\ pymongo-3.4.0-py3.5-win-am

D64.egg

Processing dependencies for pymongo

Finished processing dependencies for pymongo

It may take a long time to download in China, and there are often cases of Timeout. It can be solved by testing a few more times, or downloading the installation package directly from the Python official website. The downloaded pymongo package is automatically placed in the standard Python library. Note: at this time, even if you use the Eclipse plug-in for development, you will automatically sense the latest library changes, effective immediately.

3. Simple development example

Let's take a simple example. The current target database, test, includes the collection collection, pyinsert.

> show collections

Blog

Pyinsert

Xl

> db.pyinsert.find () .count ()

0

Here is the inserted python record script

From pymongo import MongoClient-Import package module

From bson.objectid import ObjectId

Connect = MongoClient ('172.16.19.143)

Db = connect.test-corresponding database

OperList = [{"name": "Ttest", "age": 10}, {"name": "Mark", "age": 12}

{"name": "Lucy", "age": 10}, {"name": "Tom", "age": 32}]

Res = db.pyinsert.insert_many (operList)-bulk insert

Print (res)

Note: there are some differences in the implementation of MongoDB grammar in each language, which should be referred to the official explanation. In the above example, join statements, organizing Document Array, and bulk inserts are introduced. After insertion, the results are as follows:

> db.pyinsert.find ()

{"_ id": ObjectId ("5972d32dc3e2cc1d108511b7"), "age": 10, "name": "Ttest"}

{"_ id": ObjectId ("5972d32dc3e2cc1d108511b8"), "age": 12, "name": "Mark"}

{"_ id": ObjectId ("5972d32dc3e2cc1d108511b9"), "age": 10, "name": "Lucy"}

{"_ id": ObjectId ("5972d32dc3e2cc1d108511ba"), "age": 32, "name": "Tom"}

Retrieve the data script:

From pymongo import MongoClient

From bson.objectid import ObjectId

Connect = MongoClient ('172.16.19.143)

Db = connect.test

For data in db.pyinsert.find ():

Print (data)

Operation result:

{'_ id': ObjectId ('5972d32dc3e2cc1d108511b7'),' name': 'Ttest',' age': 10}

{'_ id': ObjectId ('5972d32dc3e2cc1d108511b8'),' name': 'Mark',' age': 12}

{'_ id': ObjectId ('5972d32dc3e2cc1d108511b9'),' name': 'Lucy',' age': 10}

{'_ id': ObjectId ('5972d32dc3e2cc1d108511ba'),' name': 'Tom',' age': 32}

The above is how to connect and access MongoDB in the Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report