Monday, September 5, 2022

Creating a dummy database and collection in MongoDB Cloud and reading the dummy document using PyMongo

Login to MongoDB Atlas using your Google credentials. And perform the following actions as shown in screenshots below:

1:
2:
3:
4:

Next, we check for "pymongo"

(base) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ python Python 3.9.12 (main, Apr 5 2022, 06:56:58) [GCC 7.5.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pymongo Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'pymongo' >>> exit() ENV.YML FILE: name: mongodb channels: - conda-forge dependencies: - pip - pymongo

Environment Setup

(base) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ conda env create -f env.yml Collecting package metadata (repodata.json): done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.12.0 latest version: 4.14.0 Please update conda by running $ conda update -n base -c defaults conda Downloading and Extracting Packages openssl-3.0.5 | 2.8 MB | ### | 100% python-3.10.6 | 29.0 MB | ### | 100% setuptools-65.3.0 | 782 KB | ### | 100% pymongo-4.2.0 | 1.3 MB | ### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate mongodb # # To deactivate an active environment, use # # $ conda deactivate - - - - - - - - - - (base) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ conda activate mongodb (mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ conda install ipykernel -c conda-forge Collecting package metadata (current_repodata.json): done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.12.0 latest version: 4.14.0 Please update conda by running $ conda update -n base -c defaults conda ## Package Plan ## environment location: /home/ashish/anaconda3/envs/mongodb added / updated specs: - ipykernel The following packages will be downloaded: package | build ---------------------------|----------------- executing-1.0.0 | pyhd8ed1ab_0 19 KB conda-forge ipykernel-6.15.2 | pyh210e3f2_0 96 KB conda-forge ipython-8.4.0 | pyh41d4057_1 552 KB conda-forge jupyter_client-7.3.5 | pyhd8ed1ab_0 91 KB conda-forge psutil-5.9.2 | py310h5764c6d_0 350 KB conda-forge stack_data-0.5.0 | pyhd8ed1ab_0 24 KB conda-forge ------------------------------------------------------------ Total: 1.1 MB The following NEW packages will be INSTALLED: asttokens conda-forge/noarch::asttokens-2.0.8-pyhd8ed1ab_0 backcall conda-forge/noarch::backcall-0.2.0-pyh9f0ad1d_0 backports conda-forge/noarch::backports-1.0-py_2 backports.functoo~ conda-forge/noarch::backports.functools_lru_cache-1.6.4-pyhd8ed1ab_0 debugpy conda-forge/linux-64::debugpy-1.6.3-py310hd8f1fbe_0 decorator conda-forge/noarch::decorator-5.1.1-pyhd8ed1ab_0 entrypoints conda-forge/noarch::entrypoints-0.4-pyhd8ed1ab_0 executing conda-forge/noarch::executing-1.0.0-pyhd8ed1ab_0 ipykernel conda-forge/noarch::ipykernel-6.15.2-pyh210e3f2_0 ipython conda-forge/noarch::ipython-8.4.0-pyh41d4057_1 jedi conda-forge/noarch::jedi-0.18.1-pyhd8ed1ab_2 jupyter_client conda-forge/noarch::jupyter_client-7.3.5-pyhd8ed1ab_0 jupyter_core conda-forge/linux-64::jupyter_core-4.11.1-py310hff52083_0 libsodium conda-forge/linux-64::libsodium-1.0.18-h36c2ea0_1 matplotlib-inline conda-forge/noarch::matplotlib-inline-0.1.6-pyhd8ed1ab_0 nest-asyncio conda-forge/noarch::nest-asyncio-1.5.5-pyhd8ed1ab_0 packaging conda-forge/noarch::packaging-21.3-pyhd8ed1ab_0 parso conda-forge/noarch::parso-0.8.3-pyhd8ed1ab_0 pexpect conda-forge/noarch::pexpect-4.8.0-pyh9f0ad1d_2 pickleshare conda-forge/noarch::pickleshare-0.7.5-py_1003 prompt-toolkit conda-forge/noarch::prompt-toolkit-3.0.30-pyha770c72_0 psutil conda-forge/linux-64::psutil-5.9.2-py310h5764c6d_0 ptyprocess conda-forge/noarch::ptyprocess-0.7.0-pyhd3deb0d_0 pure_eval conda-forge/noarch::pure_eval-0.2.2-pyhd8ed1ab_0 pygments conda-forge/noarch::pygments-2.13.0-pyhd8ed1ab_0 pyparsing conda-forge/noarch::pyparsing-3.0.9-pyhd8ed1ab_0 python-dateutil conda-forge/noarch::python-dateutil-2.8.2-pyhd8ed1ab_0 pyzmq conda-forge/linux-64::pyzmq-23.2.1-py310h330234f_0 six conda-forge/noarch::six-1.16.0-pyh6c4a22f_0 stack_data conda-forge/noarch::stack_data-0.5.0-pyhd8ed1ab_0 tornado conda-forge/linux-64::tornado-6.2-py310h5764c6d_0 traitlets conda-forge/noarch::traitlets-5.3.0-pyhd8ed1ab_0 wcwidth conda-forge/noarch::wcwidth-0.2.5-pyh9f0ad1d_2 zeromq conda-forge/linux-64::zeromq-4.3.4-h9c3ff4c_1 Proceed ([y]/n)? y Downloading and Extracting Packages stack_data-0.5.0 | 24 KB | ### | 100% jupyter_client-7.3.5 | 91 KB | ### | 100% executing-1.0.0 | 19 KB | ### | 100% ipython-8.4.0 | 552 KB | ### | 100% ipykernel-6.15.2 | 96 KB | ### | 100% psutil-5.9.2 | 350 KB | ### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done - - - - - - - - - - (mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ conda install jupyterlab -c conda-forge Collecting package metadata (current_repodata.json): done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.12.0 latest version: 4.14.0 Please update conda by running $ conda update -n base -c defaults conda ## Package Plan ## environment location: /home/ashish/anaconda3/envs/mongodb added / updated specs: - jupyterlab The following packages will be downloaded: package | build ---------------------------|----------------- anyio-3.6.1 | pyhd8ed1ab_1 83 KB conda-forge babel-2.10.3 | pyhd8ed1ab_0 6.7 MB conda-forge cryptography-37.0.1 | py310h9ce1e76_0 1.5 MB json5-0.9.5 | pyh9f0ad1d_0 20 KB conda-forge jsonschema-4.15.0 | pyhd8ed1ab_0 64 KB conda-forge jupyter_server-1.18.1 | pyhd8ed1ab_0 232 KB conda-forge jupyterlab-3.4.5 | pyhd8ed1ab_0 5.8 MB conda-forge jupyterlab_server-2.15.1 | pyhd8ed1ab_0 49 KB conda-forge nbclassic-0.4.3 | pyhd8ed1ab_0 7.5 MB conda-forge notebook-shim-0.1.0 | pyhd8ed1ab_0 15 KB conda-forge pysocks-1.7.1 | pyha2e5f31_6 19 KB conda-forge requests-2.28.1 | pyhd8ed1ab_1 53 KB conda-forge sniffio-1.3.0 | pyhd8ed1ab_0 14 KB conda-forge websocket-client-1.4.1 | pyhd8ed1ab_0 42 KB conda-forge ------------------------------------------------------------ Total: 22.2 MB The following NEW packages will be INSTALLED: anyio conda-forge/noarch::anyio-3.6.1-pyhd8ed1ab_1 argon2-cffi conda-forge/noarch::argon2-cffi-21.3.0-pyhd8ed1ab_0 argon2-cffi-bindi~ conda-forge/linux-64::argon2-cffi-bindings-21.2.0-py310h5764c6d_2 attrs conda-forge/noarch::attrs-22.1.0-pyh71513ae_1 babel conda-forge/noarch::babel-2.10.3-pyhd8ed1ab_0 beautifulsoup4 conda-forge/noarch::beautifulsoup4-4.11.1-pyha770c72_0 bleach conda-forge/noarch::bleach-5.0.1-pyhd8ed1ab_0 brotlipy conda-forge/linux-64::brotlipy-0.7.0-py310h5764c6d_1004 certifi conda-forge/noarch::certifi-2022.6.15-pyhd8ed1ab_1 cffi conda-forge/linux-64::cffi-1.15.1-py310h255011f_0 charset-normalizer conda-forge/noarch::charset-normalizer-2.1.1-pyhd8ed1ab_0 cryptography pkgs/main/linux-64::cryptography-37.0.1-py310h9ce1e76_0 defusedxml conda-forge/noarch::defusedxml-0.7.1-pyhd8ed1ab_0 flit-core conda-forge/noarch::flit-core-3.7.1-pyhd8ed1ab_0 icu conda-forge/linux-64::icu-70.1-h27087fc_0 idna conda-forge/noarch::idna-3.3-pyhd8ed1ab_0 importlib-metadata conda-forge/linux-64::importlib-metadata-4.11.4-py310hff52083_0 importlib_metadata conda-forge/noarch::importlib_metadata-4.11.4-hd8ed1ab_0 importlib_resourc~ conda-forge/noarch::importlib_resources-5.9.0-pyhd8ed1ab_0 ipython_genutils conda-forge/noarch::ipython_genutils-0.2.0-py_1 jinja2 conda-forge/noarch::jinja2-3.1.2-pyhd8ed1ab_1 json5 conda-forge/noarch::json5-0.9.5-pyh9f0ad1d_0 jsonschema conda-forge/noarch::jsonschema-4.15.0-pyhd8ed1ab_0 jupyter_server conda-forge/noarch::jupyter_server-1.18.1-pyhd8ed1ab_0 jupyterlab conda-forge/noarch::jupyterlab-3.4.5-pyhd8ed1ab_0 jupyterlab_pygmen~ conda-forge/noarch::jupyterlab_pygments-0.2.2-pyhd8ed1ab_0 jupyterlab_server conda-forge/noarch::jupyterlab_server-2.15.1-pyhd8ed1ab_0 libiconv conda-forge/linux-64::libiconv-1.16-h516909a_0 libxml2 conda-forge/linux-64::libxml2-2.9.14-h22db469_4 libxslt conda-forge/linux-64::libxslt-1.1.35-h8affb1d_0 lxml conda-forge/linux-64::lxml-4.9.1-py310h5764c6d_0 markupsafe conda-forge/linux-64::markupsafe-2.1.1-py310h5764c6d_1 mistune conda-forge/noarch::mistune-2.0.4-pyhd8ed1ab_0 nbclassic conda-forge/noarch::nbclassic-0.4.3-pyhd8ed1ab_0 nbclient conda-forge/noarch::nbclient-0.6.7-pyhd8ed1ab_0 nbconvert conda-forge/noarch::nbconvert-7.0.0-pyhd8ed1ab_0 nbconvert-core conda-forge/noarch::nbconvert-core-7.0.0-pyhd8ed1ab_0 nbconvert-pandoc conda-forge/noarch::nbconvert-pandoc-7.0.0-pyhd8ed1ab_0 nbformat conda-forge/noarch::nbformat-5.4.0-pyhd8ed1ab_0 notebook conda-forge/noarch::notebook-6.4.12-pyha770c72_0 notebook-shim conda-forge/noarch::notebook-shim-0.1.0-pyhd8ed1ab_0 pandoc conda-forge/linux-64::pandoc-2.19.2-ha770c72_0 pandocfilters conda-forge/noarch::pandocfilters-1.5.0-pyhd8ed1ab_0 pkgutil-resolve-n~ conda-forge/noarch::pkgutil-resolve-name-1.3.10-pyhd8ed1ab_0 prometheus_client conda-forge/noarch::prometheus_client-0.14.1-pyhd8ed1ab_0 pycparser conda-forge/noarch::pycparser-2.21-pyhd8ed1ab_0 pyopenssl conda-forge/noarch::pyopenssl-22.0.0-pyhd8ed1ab_0 pyrsistent conda-forge/linux-64::pyrsistent-0.18.1-py310h5764c6d_1 pysocks conda-forge/noarch::pysocks-1.7.1-pyha2e5f31_6 python-fastjsonsc~ conda-forge/noarch::python-fastjsonschema-2.16.1-pyhd8ed1ab_0 pytz conda-forge/noarch::pytz-2022.2.1-pyhd8ed1ab_0 requests conda-forge/noarch::requests-2.28.1-pyhd8ed1ab_1 send2trash conda-forge/noarch::send2trash-1.8.0-pyhd8ed1ab_0 sniffio conda-forge/noarch::sniffio-1.3.0-pyhd8ed1ab_0 soupsieve conda-forge/noarch::soupsieve-2.3.2.post1-pyhd8ed1ab_0 terminado conda-forge/linux-64::terminado-0.15.0-py310hff52083_0 tinycss2 conda-forge/noarch::tinycss2-1.1.1-pyhd8ed1ab_0 typing_extensions conda-forge/noarch::typing_extensions-4.3.0-pyha770c72_0 urllib3 conda-forge/noarch::urllib3-1.26.11-pyhd8ed1ab_0 webencodings conda-forge/noarch::webencodings-0.5.1-py_1 websocket-client conda-forge/noarch::websocket-client-1.4.1-pyhd8ed1ab_0 zipp conda-forge/noarch::zipp-3.8.1-pyhd8ed1ab_0 Proceed ([y]/n)? y Downloading and Extracting Packages websocket-client-1.4 | 42 KB | ### | 100% nbclassic-0.4.3 | 7.5 MB | ### | 100% anyio-3.6.1 | 83 KB | ### | 100% sniffio-1.3.0 | 14 KB | ### | 100% pysocks-1.7.1 | 19 KB | ### | 100% babel-2.10.3 | 6.7 MB | ### | 100% notebook-shim-0.1.0 | 15 KB | ### | 100% jupyterlab_server-2. | 49 KB | ### | 100% json5-0.9.5 | 20 KB | ### | 100% jupyterlab-3.4.5 | 5.8 MB | ### | 100% jsonschema-4.15.0 | 64 KB | ### | 100% jupyter_server-1.18. | 232 KB | ### | 100% requests-2.28.1 | 53 KB | ### | 100% cryptography-37.0.1 | 1.5 MB | ### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done (mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$

Installing kernel for Jupyter Lab

(mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ python -m ipykernel install --user --name mongodb Installed kernelspec mongodb in /home/ashish/.local/share/jupyter/kernels/mongodb (mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$

Testing Python Environment

import pymongo client = pymongo.MongoClient("mongodb+srv://$USERNAME:$PASSWORD@abc.xyz.mongodb.net/?retryWrites=true&w=majority") db = client.test --------------------------------------------------------------------------- ConfigurationError Traceback (most recent call last) Input In [1], in <cell line: 2>() 1 import pymongo ----> 2 client = pymongo.MongoClient("mongodb+srv://$USERNAME:$PASSWORD@abc.xyz.mongodb.net/?retryWrites=true&w=majority") 3 db = client.test File ~/anaconda3/envs/mongodb/lib/python3.10/site-packages/pymongo/mongo_client.py:726, in MongoClient.__init__(self, host, port, document_class, tz_aware, connect, type_registry, **kwargs) 722 if timeout is not None: 723 timeout = common.validate_timeout_or_none_or_zero( 724 keyword_opts.cased_key("connecttimeoutms"), timeout 725 ) --> 726 res = uri_parser.parse_uri( 727 entity, 728 port, 729 validate=True, 730 warn=True, 731 normalize=False, 732 connect_timeout=timeout, 733 srv_service_name=srv_service_name, 734 srv_max_hosts=srv_max_hosts, 735 ) 736 seeds.update(res["nodelist"]) 737 username = res["username"] or username File ~/anaconda3/envs/mongodb/lib/python3.10/site-packages/pymongo/uri_parser.py:469, in parse_uri(uri, default_port, validate, warn, normalize, connect_timeout, srv_service_name, srv_max_hosts) 467 if not _HAVE_DNSPYTHON: 468 python_path = sys.executable or "python" --> 469 raise ConfigurationError( 470 'The "dnspython" module must be ' 471 "installed to use mongodb+srv:// URIs. " 472 "To fix this error install pymongo with the srv extra:\n " 473 '%s -m pip install "pymongo[srv]"' % (python_path) 474 ) 475 is_srv = True 476 scheme_free = uri[SRV_SCHEME_LEN:] ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs. To fix this error install pymongo with the srv extra: /home/ashish/anaconda3/envs/mongodb/bin/python -m pip install "pymongo[srv]"

Fix:

(base) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ conda activate mongodb (mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$ python -m pip install "pymongo[srv]" Requirement already satisfied: pymongo[srv] in /home/ashish/anaconda3/envs/mongodb/lib/python3.10/site-packages (4.2.0) Collecting dnspython<3.0.0,>=1.16.0 Downloading dnspython-2.2.1-py3-none-any.whl (269 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 269.1/269.1 kB 123.6 kB/s eta 0:00:00 Installing collected packages: dnspython Successfully installed dnspython-2.2.1 (mongodb) ashish@ashish-Lenovo-ideapad-130-15IKB:~/Desktop$

ISSUE WITH SPECIAL CHARACTERS IN PASSWORD

import pymongo client = pymongo.MongoClient("mongodb+srv://$USERNAME:$PASSWORD@abc.xyz.mongodb.net/?retryWrites=true&w=majority") db = client.test --------------------------------------------------------------------------- InvalidURI Traceback (most recent call last) Input In [1], in <cell line: 2>() 1 import pymongo ----> 2 client = pymongo.MongoClient("mongodb+srv://$USERNAME:$PASSWORD@abc.xyz.mongodb.net/?retryWrites=true&w=majority") 3 db = client.test File ~/anaconda3/envs/mongodb/lib/python3.10/site-packages/pymongo/mongo_client.py:726, in MongoClient.__init__(self, host, port, document_class, tz_aware, connect, type_registry, **kwargs) 722 if timeout is not None: 723 timeout = common.validate_timeout_or_none_or_zero( 724 keyword_opts.cased_key("connecttimeoutms"), timeout 725 ) --> 726 res = uri_parser.parse_uri( 727 entity, 728 port, 729 validate=True, 730 warn=True, 731 normalize=False, 732 connect_timeout=timeout, 733 srv_service_name=srv_service_name, 734 srv_max_hosts=srv_max_hosts, 735 ) 736 seeds.update(res["nodelist"]) 737 username = res["username"] or username File ~/anaconda3/envs/mongodb/lib/python3.10/site-packages/pymongo/uri_parser.py:516, in parse_uri(uri, default_port, validate, warn, normalize, connect_timeout, srv_service_name, srv_max_hosts) 514 if "@" in host_part: 515 userinfo, _, hosts = host_part.rpartition("@") --> 516 user, passwd = parse_userinfo(userinfo) 517 else: 518 hosts = host_part File ~/anaconda3/envs/mongodb/lib/python3.10/site-packages/pymongo/uri_parser.py:71, in parse_userinfo(userinfo) 60 """Validates the format of user information in a MongoDB URI. 61 Reserved characters that are gen-delimiters (":", "/", "?", "#", "[", 62 "]", "@") as per RFC 3986 must be escaped. (...) 68 - `userinfo`: A string of the form <username>:<password> 69 """ 70 if "@" in userinfo or userinfo.count(":") > 1 or _unquoted_percent(userinfo): ---> 71 raise InvalidURI( 72 "Username and password must be escaped according to " 73 "RFC 3986, use urllib.parse.quote_plus" 74 ) 76 user, _, passwd = userinfo.partition(":") 77 # No password is expected with GSSAPI authentication. InvalidURI: Username and password must be escaped according to RFC 3986, use urllib.parse.quote_plus

All Issues Resolved. See Successful Code Run Below:

import pymongo import urllib uri = "mongodb+srv://$USERNAME:" + urllib.parse.quote_plus("$PASSWORD") + "@abc.xyz.mongodb.net/?retryWrites=true&w=majority" client = pymongo.MongoClient(uri) print(client) MongoClient(host=['ac-af4wahu-shard-00-02.xyz.mongodb.net:27017', 'ac-af4wahu-shard-00-01.xyz.mongodb.net:27017', 'ac-af4wahu-shard-00-00.xyz.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, retrywrites=True, w='majority', authsource='admin', replicaset='atlas-3xaqbc-shard-0', tls=True) db = client['db1'] collection = db['ccn1'] print(collection.count_documents({})) # Output: 1 print(collection.find({})) <pymongo.cursor.Cursor at 0x7fd632c38850> import pprint for doc in collection.find(): pprint.pprint(doc) {'_id': ObjectId('6315c3a74afb509774a88467'), 'address': 'Delhi', 'name': 'Ashish Jain'}
Tags: Database,Cloud

No comments:

Post a Comment