{ "cells": [ { "cell_type": "code", "execution_count": 9, "id": "22005864-a267-4819-b083-935a85452b39", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pymongo in /opt/conda/lib/python3.10/site-packages (4.2.0)\r\n" ] } ], "source": [ "!pip install pymongo" ] }, { "cell_type": "code", "execution_count": 10, "id": "d1690099-dbf9-4d75-946c-c556c13ec34d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "630855c7014e9a17ccd6bd03\n", "['customers']\n" ] } ], "source": [ "import pymongo\n", "\n", "myclient = pymongo.MongoClient(\"mongodb://donaldrich:ibanez69@mongo:27017\")\n", "# DB\n", "\n", "mydb = myclient[\"mydatabase\"]\n", "\n", "# Collectino\n", "\n", "mycol = mydb[\"customers\"]\n", "\n", "mydict = { \"name\": \"Peter\", \"address\": \"Lowstreet 27\" }\n", "\n", "x = mycol.insert_one(mydict)\n", "\n", "print(x.inserted_id)\n", "\n", "print(mydb.list_collection_names())" ] }, { "cell_type": "markdown", "id": "d97181c9-08bb-47c0-a8a3-50f8ea878a15", "metadata": {}, "source": [ "# hjhj" ] }, { "cell_type": "code", "execution_count": 5, "id": "54535d13-e279-44bc-bd9d-52d14cb550fb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "# DB\n", "\n", "mydb = myclient[\"mydatabase\"]\n", "\n", "# Collectino\n", "\n", "mycol = mydb[\"customers\"]\n", "\n", "print(mydb.list_collection_names())" ] }, { "cell_type": "markdown", "id": "7b7a6e52-4de9-4f6d-91b4-b3716baba323", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7ed477f1-9044-498c-9d3e-5021df5bdc93", "metadata": {}, "outputs": [], "source": [ "mydict = { \"name\": \"Peter\", \"address\": \"Lowstreet 27\" }\n", "\n", "x = mycol.insert_one(mydict)\n", "\n", "print(x.inserted_id)" ] }, { "cell_type": "markdown", "id": "efb52832-dc7d-4e4c-b70c-d29b923f02db", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "430085cf-e0b4-4a12-b382-90f9351e5490", "metadata": {}, "outputs": [], "source": [ "mylist = [\n", " { \"_id\": 1, \"name\": \"John\", \"address\": \"Highway 37\"},\n", " { \"_id\": 2, \"name\": \"Peter\", \"address\": \"Lowstreet 27\"},\n", " { \"_id\": 3, \"name\": \"Amy\", \"address\": \"Apple st 652\"},\n", " { \"_id\": 4, \"name\": \"Hannah\", \"address\": \"Mountain 21\"},\n", " { \"_id\": 5, \"name\": \"Michael\", \"address\": \"Valley 345\"},\n", " { \"_id\": 6, \"name\": \"Sandy\", \"address\": \"Ocean blvd 2\"},\n", " { \"_id\": 7, \"name\": \"Betty\", \"address\": \"Green Grass 1\"},\n", " { \"_id\": 8, \"name\": \"Richard\", \"address\": \"Sky st 331\"},\n", " { \"_id\": 9, \"name\": \"Susan\", \"address\": \"One way 98\"},\n", " { \"_id\": 10, \"name\": \"Vicky\", \"address\": \"Yellow Garden 2\"},\n", " { \"_id\": 11, \"name\": \"Ben\", \"address\": \"Park Lane 38\"},\n", " { \"_id\": 12, \"name\": \"William\", \"address\": \"Central st 954\"},\n", " { \"_id\": 13, \"name\": \"Chuck\", \"address\": \"Main Road 989\"},\n", " { \"_id\": 14, \"name\": \"Viola\", \"address\": \"Sideway 1633\"}\n", "]\n", "\n", "x = mycol.insert_many(mylist)\n", "\n", "#print list of the _id values of the inserted documents:\n", "print(x.inserted_ids)c" ] }, { "cell_type": "code", "execution_count": null, "id": "9a107bb3-0a12-4000-98b0-ac72b0b07731", "metadata": {}, "outputs": [], "source": [ "myquery = { \"address\": \"Park Lane 38\" }\n", "\n", "mydoc = mycol.find(myquery)\n", "\n", "for x in mydoc:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "191814c5-990e-4c30-9c5f-363fc479e734", "metadata": {}, "outputs": [], "source": [ "import pymongo\n", "\n", "myclient = pymongo.MongoClient(\"mongodb://localhost:27017/\")\n", "mydb = myclient[\"mydatabase\"]\n", "mycol = mydb[\"customers\"]\n", "\n", "myquery = { \"address\": \"Mountain 21\" }\n", "\n", "mycol.delete_one(myquery)" ] }, { "cell_type": "code", "execution_count": null, "id": "024256e0-4c13-4820-812b-c9cb7d7ff53b", "metadata": {}, "outputs": [], "source": [ "mycol.drop()" ] }, { "cell_type": "code", "execution_count": null, "id": "62487b99-8edc-4179-a33b-28a0536afd1e", "metadata": {}, "outputs": [], "source": [ "import pymongo\n", "\n", "myclient = pymongo.MongoClient(\"mongodb://localhost:27017/\")\n", "mydb = myclient[\"mydatabase\"]\n", "mycol = mydb[\"customers\"]\n", "\n", "for x in mycol.find({},{ \"_id\": 0, \"name\": 1, \"address\": 1 }):\n", " print(x)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.5" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }