|
Python3操作MongoDB数据库
- 0. 写在前面
- 1. 安装开源驱动库pymongo
- 2. 参考
<hr/>0. 写在前面
- Linux:Ubuntu Kylin 16.04
- MongoDB:MongoDB3.2.7
- Python:Anaconda With Python3.7
1. 安装开源驱动库pymongo
pymongo驱动程序可以直接连接MongoDB数据库 zhangsan@node01:~$ conda create -n py39 python = 3.9
zhangsan@node01:~$ conda activate py39
(py39) zhangsan@node01:~$ python3 -m pip install pymongo
Collecting pymongo
Downloading pymongo-4.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (479 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 479.2/479.2 kB 37.7 kB/s eta 0:00:00
Installing collected packages: pymongo
Successfully installed pymongo-4.2.0
(py39) zhangsan@node01:~$ vim pyinsert.py
from pymongo import MongoClient
from random import randint
import datetime
client = MongoClient(&#39;localhost&#39;,27017)
db = client.taobao
order = db.order_info
status = [&#39;A&#39;,&#39;B&#39;,&#39;C&#39;]
cust_id = [&#39;A123&#39;,&#39;B123&#39;,&#39;C123&#39;]
price = [500,200,250,300]
sku = [&#39;mmm&#39;,&#39;nnn&#39;]
for i in range(1,100):
items = []
item_count =randint(2,6)
for n in range(item_count):
items.append({&#34;sku&#34;:sku[randint(0,1)],&#34;qty&#34;:randint(1,10),&#34;price&#34;:randint(0,5)})
new = { &#34;status&#34;:status[randint(0,2)], &#34;cust_id&#34;:cust_id[randint(0,2)], &#34;price&#34;:price[randint(0,3)], &#34;ord_date&#34;:datetime.datetime.utcnow(), &#34;items&#34;:items }
print(new)
order.insert_one(new)
print(order.estimated_document_count())
(py39) zhangsan@node01:/usr/local/mongodb-3.2.7$ mongod --config /mongodb/single/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3493
child process started successfully, parent exiting
(py39) zhangsan@node01:/usr/local/mongodb-3.2.7$ ps -ef | grep mongo
zhangsan 3493 1 3 00:56 ? 00:00:01 mongod --config /mongodb/single/mongod.conf
zhangsan 3511 3214 0 00:57 pts/0 00:00:00 grep --color=auto mongo
(py39) zhangsan@node01:~$ python3 pyinsert.py
pymongo.errors.ConfigurationError: Server at localhost:27017 reports wire version 4, but this version of PyMongo requires at least 6 (MongoDB 3.6).很明显,此处需要降低驱动pymongo的版本
(py39) zhangsan@node01:~$ pip uninstall -y pymongo
Found existing installation: pymongo 4.2.0
Uninstalling pymongo-4.2.0:
Successfully uninstalled pymongo-4.2.0
(py39) zhangsan@node01:~$ pip3 list
Package Version
------------ ------------
certifi 2022.6.15
pip 22.1.2
setuptools 63.4.1
(py39) zhangsan@node01:~$ pip3 install pymongo==3.6
Collecting pymongo==3.6
Downloading pymongo-3.6.0.tar.gz (583 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 583.3/583.3 kB 52.1 kB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pymongo
Building wheel for pymongo (setup.py) ... done
Created wheel for pymongo: filename=pymongo-3.6.0-cp39-cp39-linux_x86_64.whl size=272327 sha256=72a9dacdef7afa26cb169debe429964d1b5ab119ee39e7ec4c3eb5c59c5433c0
Stored in directory: /home/zhangsan/.cache/pip/wheels/6c/97/2e/74c0fd06fe0df24ded743a8af0707d1b5d618a0e8d321ba85c
Successfully built pymongo
Installing collected packages: pymongo
Successfully installed pymongo-3.6.0
(py39) zhangsan@node01:~$ pip3 list | grep pymongo
pymongo 3.6.0
- 继续报错:AttributeError: module &#39;platform&#39; has no attribute &#39;linux_distribution&#39;
(py39) zhangsan@node01:~$ python3 pyinsert.py
Traceback (most recent call last):
File &#34;/home/zhangsan/pyinsert.py&#34;, line 1, in <module>
from pymongo import MongoClient
File &#34;/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/__init__.py&#34;, line 87, in <module>
from pymongo.mongo_client import MongoClient
File &#34;/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/mongo_client.py&#34;, line 52, in <module>
from pymongo.client_options import ClientOptions
File &#34;/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/client_options.py&#34;, line 23, in <module>
from pymongo.pool import PoolOptions
File &#34;/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/pool.py&#34;, line 124, in <module>
platform.linux_distribution() if part])),
AttributeError: module &#39;platform&#39; has no attribute &#39;linux_distribution&#39;关于这个error,其实是Python自身的原因
Python从3.8版本开始,已经从平台模块中删除了“linux_distribution()”函数 前文使用的是Python3.9,解决这个问题有两种方法:
- 修改Python3.9源码
- 降低Python版本到3.7
- 新建一个Python3.7虚拟环境
zhangsan@node01:~$ conda create -n pymongo python = 3.7
zhangsan@node01:~$ conda activate pymongo
(pymongo) zhangsan@node01:~$ python3 -m pip install pymongo=3.6
Collecting pymongo
Downloading pymongo-3.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (479 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 479.2/479.2 kB 37.7 kB/s eta 0:00:00
Installing collected packages: pymongo
Successfully installed pymongo-3.6.0
(pymongo) zhangsan@node01:~$ python3 pyinsert.py上述代码中使用了estimated_document_count()函数,该函数是MongoDB4.0.3的新功能,此处会出现错误,改为count()函数即可
2. 参考
https://stackoverflow.com/questions/63636697/error-module-platform-has-no-attribute-linux-distribution |
|