{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pymongo\n", "import datetime\n", "import pprint\n", "from bson.son import SON" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Be sure to run mongod" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Get a client connection, default connection information\n", "client = pymongo.MongoClient()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Get a client connection, host and port\n", "client = pymongo.MongoClient('localhost', 27017)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Another way of saying the same thing\n", "client = pymongo.MongoClient('mongodb://localhost:27017/')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Just in case, we'll drop our test database\n", "client.drop_database('test_database')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Get connection to database\n", "db = client.test_database" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Same thing said differently\n", "db = client['test_database']" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Get a collection in the DB\n", "collection = db.test_collection" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Same thing said differently\n", "collection = db['test_collection']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Why does it not show up? Created only when needed\n", "db.list_collection_names()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Create some data\n", "post = {\"author\": \"Mike\",\n", " \"text\": \"My first blog post!\",\n", " \"tags\": [\"mongodb\", \"python\", \"pymongo\"],\n", " \"date\": datetime.datetime.utcnow()}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ObjectId('5f6a1bfbb520d93bc9144eab')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Insert data\n", "posts = db.posts\n", "post_id = posts.insert_one(post).inserted_id\n", "post_id" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['posts']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# NOW the collection exists\n", "db.list_collection_names()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1bfbb520d93bc9144eab'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2020, 9, 22, 15, 43, 34, 66000),\n", " 'tags': ['mongodb', 'python', 'pymongo'],\n", " 'text': 'My first blog post!'}\n" ] } ], "source": [ "# Get some data\n", "pprint.pprint(posts.find_one())" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1bfbb520d93bc9144eab'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2020, 9, 22, 15, 43, 34, 66000),\n", " 'tags': ['mongodb', 'python', 'pymongo'],\n", " 'text': 'My first blog post!'}\n" ] } ], "source": [ "# Query format\n", "pprint.pprint(posts.find_one({\"author\": \"Mike\"}))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "posts.find_one({\"author\": \"Eliot\"})" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ObjectId('5f6a1bfbb520d93bc9144eab')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "post_id" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1bfbb520d93bc9144eab'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2020, 9, 22, 15, 43, 34, 66000),\n", " 'tags': ['mongodb', 'python', 'pymongo'],\n", " 'text': 'My first blog post!'}\n" ] } ], "source": [ "# Get a specific item by ID\n", "pprint.pprint(posts.find_one({\"_id\": post_id}))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# What if we treat it as a string?\n", "post_id_as_str = str(post_id)\n", "posts.find_one({\"_id\": post_id_as_str})" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'5f6a1bfbb520d93bc9144eab'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "post_id_as_str" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ObjectId('5f6a1dfcb520d93bc9144eac'), ObjectId('5f6a1dfcb520d93bc9144ead')]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Bulk insert\n", "new_posts = [{\"author\": \"Mike\",\n", " \"text\": \"Another post!\",\n", " \"tags\": [\"bulk\", \"insert\"],\n", " \"date\": datetime.datetime(2009, 11, 12, 11, 14)},\n", " {\"author\": \"Eliot\",\n", " \"title\": \"MongoDB is fun\",\n", " \"text\": \"and pretty easy too!\",\n", " \"date\": datetime.datetime(2009, 11, 10, 10, 45)}]\n", "result = posts.insert_many(new_posts)\n", "result.inserted_ids" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1bfbb520d93bc9144eab'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2020, 9, 22, 15, 43, 34, 66000),\n", " 'tags': ['mongodb', 'python', 'pymongo'],\n", " 'text': 'My first blog post!'}\n", "{'_id': ObjectId('5f6a1dfcb520d93bc9144eac'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2009, 11, 12, 11, 14),\n", " 'tags': ['bulk', 'insert'],\n", " 'text': 'Another post!'}\n", "{'_id': ObjectId('5f6a1dfcb520d93bc9144ead'),\n", " 'author': 'Eliot',\n", " 'date': datetime.datetime(2009, 11, 10, 10, 45),\n", " 'text': 'and pretty easy too!',\n", " 'title': 'MongoDB is fun'}\n" ] } ], "source": [ "# Query \n", "for post in posts.find():\n", " pprint.pprint(post)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1bfbb520d93bc9144eab'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2020, 9, 22, 15, 43, 34, 66000),\n", " 'tags': ['mongodb', 'python', 'pymongo'],\n", " 'text': 'My first blog post!'}\n", "{'_id': ObjectId('5f6a1dfcb520d93bc9144eac'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2009, 11, 12, 11, 14),\n", " 'tags': ['bulk', 'insert'],\n", " 'text': 'Another post!'}\n" ] } ], "source": [ "# Find all that match some criterion\n", "for post in posts.find({\"author\": \"Mike\"}):\n", " pprint.pprint(post)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "posts.count_documents({})" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "posts.count_documents({\"author\": \"Mike\"})" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1dfcb520d93bc9144ead'),\n", " 'author': 'Eliot',\n", " 'date': datetime.datetime(2009, 11, 10, 10, 45),\n", " 'text': 'and pretty easy too!',\n", " 'title': 'MongoDB is fun'}\n", "{'_id': ObjectId('5f6a1dfcb520d93bc9144eac'),\n", " 'author': 'Mike',\n", " 'date': datetime.datetime(2009, 11, 12, 11, 14),\n", " 'tags': ['bulk', 'insert'],\n", " 'text': 'Another post!'}\n" ] } ], "source": [ "d = datetime.datetime(2009, 11, 12, 12)\n", "for post in posts.find({\"date\": {\"$lt\": d}}).sort(\"author\"):\n", " pprint.pprint(post)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "myclient = pymongo.MongoClient(\"mongodb://localhost:27017/\")\n", "myclient.drop_database(\"mydatabase\")\n", "mydb = myclient[\"mydatabase\"]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['admin', 'aggregation_example', 'config', 'local', 'test_database']\n" ] } ], "source": [ "print(myclient.list_database_names())" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "mycol = mydb[\"customers\"]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "print(mydb.list_collection_names())" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['admin', 'aggregation_example', 'config', 'local', 'test_database']\n" ] } ], "source": [ "print(myclient.list_database_names())" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "mydict = { \"name\": \"Tim\"}\n", "x = mycol.insert_one(mydict)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5f6a220cb520d93bc9144ebd\n" ] } ], "source": [ "print(x.inserted_id)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['admin', 'aggregation_example', 'config', 'local', 'mydatabase', 'test_database']\n" ] } ], "source": [ "print(myclient.list_database_names())" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ObjectId('5f6a1f44b520d93bc9144eb0'), ObjectId('5f6a1f44b520d93bc9144eb1'), ObjectId('5f6a1f44b520d93bc9144eb2'), ObjectId('5f6a1f44b520d93bc9144eb3'), ObjectId('5f6a1f44b520d93bc9144eb4'), ObjectId('5f6a1f44b520d93bc9144eb5'), ObjectId('5f6a1f44b520d93bc9144eb6'), ObjectId('5f6a1f44b520d93bc9144eb7'), ObjectId('5f6a1f44b520d93bc9144eb8'), ObjectId('5f6a1f44b520d93bc9144eb9'), ObjectId('5f6a1f44b520d93bc9144eba'), ObjectId('5f6a1f44b520d93bc9144ebb')]\n" ] } ], "source": [ "mylist = [\n", " { \"name\": \"Amy\", \"address\": \"Apple st 652\"},\n", " { \"name\": \"Hannah\", \"address\": \"Mountain 21\"},\n", " { \"name\": \"Michael\", \"address\": \"Valley 345\"},\n", " { \"name\": \"Sandy\", \"address\": \"Ocean blvd 2\"},\n", " { \"name\": \"Betty\", \"address\": \"Green Grass 1\"},\n", " { \"name\": \"Richard\", \"address\": \"Sky st 331\"},\n", " { \"name\": \"Susan\", \"address\": \"One way 98\"},\n", " { \"name\": \"Vicky\", \"address\": \"Yellow Garden 2\"},\n", " { \"name\": \"Ben\", \"address\": \"Park Lane 38\"},\n", " { \"name\": \"William\", \"address\": \"Central st 954\"},\n", " { \"name\": \"Chuck\", \"address\": \"Main Road 989\"},\n", " { \"name\": \"Viola\", \"address\": \"Sideway 1633\"}\n", "]\n", "\n", "x = mycol.insert_many(mylist)\n", "\n", "print(x.inserted_ids)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n" ] } ], "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\": \n", " 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(x.inserted_ids)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1f1eb520d93bc9144eaf'), 'name': 'John', 'address': 'Highway 37'}\n" ] } ], "source": [ "x = mycol.find_one()\n", "\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1f1eb520d93bc9144eaf'), 'name': 'John', 'address': 'Highway 37'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb0'), 'name': 'Amy', 'address': 'Apple st 652'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb1'), 'name': 'Hannah', 'address': 'Mountain 21'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb2'), 'name': 'Michael', 'address': 'Valley 345'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb3'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb4'), 'name': 'Betty', 'address': 'Green Grass 1'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb5'), 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb6'), 'name': 'Susan', 'address': 'One way 98'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb7'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb8'), 'name': 'Ben', 'address': 'Park Lane 38'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb9'), 'name': 'William', 'address': 'Central st 954'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eba'), 'name': 'Chuck', 'address': 'Main Road 989'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144ebb'), 'name': 'Viola', 'address': 'Sideway 1633'}\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" ] } ], "source": [ "for x in mycol.find():\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': 1, 'address': 'Highway 37'}\n", "{'_id': 2, 'address': 'Lowstreet 27'}\n", "{'_id': 3, 'address': 'Apple st 652'}\n", "{'_id': 4, 'address': 'Mountain 21'}\n", "{'_id': 5, 'address': 'Valley 345'}\n", "{'_id': 6, 'address': 'Ocean blvd 2'}\n", "{'_id': 7, 'address': 'Green Grass 1'}\n", "{'_id': 8, 'address': 'Sky st 331'}\n", "{'_id': 9, 'address': 'One way 98'}\n", "{'_id': 10, 'address': 'Yellow Garden 2'}\n", "{'_id': 11, 'address': 'Park Lane 38'}\n", "{'_id': 12, 'address': 'Central st 954'}\n", "{'_id': 13, 'address': 'Main Road 989'}\n", "{'_id': 14, 'address': 'Sideway 1633'}\n", "{'_id': ObjectId('5f6a2122b520d93bc9144ebc'), 'address': 2}\n", "{'_id': ObjectId('5f6a220cb520d93bc9144ebd')}\n" ] } ], "source": [ "for x in mycol.find({},{ \"address\": 1 }):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a220cb520d93bc9144ebd'), 'name': 'Tim'}\n" ] } ], "source": [ "myquery = { \"address\": {\"$eq\":None} }\n", "\n", "mydoc = mycol.find(myquery)\n", "\n", "for x in mydoc:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a2122b520d93bc9144ebc'), 'name': 'John', 'address': 2}\n" ] } ], "source": [ "myquery = { \"address\": { \"$gt\": 1 } }\n", "\n", "mydoc = mycol.find(myquery)\n", "\n", "for x in mydoc:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1f44b520d93bc9144eb5'), 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144ebb'), 'name': 'Viola', 'address': 'Sideway 1633'}\n", "{'_id': 8, 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': 14, 'name': 'Viola', 'address': 'Sideway 1633'}\n" ] } ], "source": [ "myquery = { \"address\": { \"$regex\": \"^S\" } }\n", "\n", "mydoc = mycol.find(myquery)\n", "\n", "for x in mydoc:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1f44b520d93bc9144eb0'), 'name': 'Amy', 'address': 'Apple st 652'}\n", "{'_id': 3, 'name': 'Amy', 'address': 'Apple st 652'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb8'), 'name': 'Ben', 'address': 'Park Lane 38'}\n", "{'_id': 11, 'name': 'Ben', 'address': 'Park Lane 38'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb4'), 'name': 'Betty', 'address': 'Green Grass 1'}\n", "{'_id': 7, 'name': 'Betty', 'address': 'Green Grass 1'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eba'), 'name': 'Chuck', 'address': 'Main Road 989'}\n", "{'_id': 13, 'name': 'Chuck', 'address': 'Main Road 989'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb1'), 'name': 'Hannah', 'address': 'Mountain 21'}\n", "{'_id': 4, 'name': 'Hannah', 'address': 'Mountain 21'}\n", "{'_id': ObjectId('5f6a1f1eb520d93bc9144eaf'), 'name': 'John', 'address': 'Highway 37'}\n", "{'_id': 1, 'name': 'John', 'address': 'Highway 37'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb2'), 'name': 'Michael', 'address': 'Valley 345'}\n", "{'_id': 5, 'name': 'Michael', 'address': 'Valley 345'}\n", "{'_id': 2, 'name': 'Peter', 'address': 'Lowstreet 27'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb5'), 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': 8, 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb3'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}\n", "{'_id': 6, 'name': 'Sandy', 'address': 'Ocean blvd 2'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb6'), 'name': 'Susan', 'address': 'One way 98'}\n", "{'_id': 9, 'name': 'Susan', 'address': 'One way 98'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb7'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}\n", "{'_id': 10, 'name': 'Vicky', 'address': 'Yellow Garden 2'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144ebb'), 'name': 'Viola', 'address': 'Sideway 1633'}\n", "{'_id': 14, 'name': 'Viola', 'address': 'Sideway 1633'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb9'), 'name': 'William', 'address': 'Central st 954'}\n", "{'_id': 12, 'name': 'William', 'address': 'Central st 954'}\n" ] } ], "source": [ "mydoc = mycol.find().sort(\"name\")\n", "\n", "for x in mydoc:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1f44b520d93bc9144eb9'), 'name': 'William', 'address': 'Central st 954'}\n", "{'_id': 12, 'name': 'William', 'address': 'Central st 954'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144ebb'), 'name': 'Viola', 'address': 'Sideway 1633'}\n", "{'_id': 14, 'name': 'Viola', 'address': 'Sideway 1633'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb7'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}\n", "{'_id': 10, 'name': 'Vicky', 'address': 'Yellow Garden 2'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb6'), 'name': 'Susan', 'address': 'One way 98'}\n", "{'_id': 9, 'name': 'Susan', 'address': 'One way 98'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb3'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}\n", "{'_id': 6, 'name': 'Sandy', 'address': 'Ocean blvd 2'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb5'), 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': 8, 'name': 'Richard', 'address': 'Sky st 331'}\n", "{'_id': 2, 'name': 'Peter', 'address': 'Lowstreet 27'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb2'), 'name': 'Michael', 'address': 'Valley 345'}\n", "{'_id': 5, 'name': 'Michael', 'address': 'Valley 345'}\n", "{'_id': ObjectId('5f6a1f1eb520d93bc9144eaf'), 'name': 'John', 'address': 'Highway 37'}\n", "{'_id': 1, 'name': 'John', 'address': 'Highway 37'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb1'), 'name': 'Hannah', 'address': 'Mountain 21'}\n", "{'_id': 4, 'name': 'Hannah', 'address': 'Mountain 21'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eba'), 'name': 'Chuck', 'address': 'Main Road 989'}\n", "{'_id': 13, 'name': 'Chuck', 'address': 'Main Road 989'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb4'), 'name': 'Betty', 'address': 'Green Grass 1'}\n", "{'_id': 7, 'name': 'Betty', 'address': 'Green Grass 1'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb8'), 'name': 'Ben', 'address': 'Park Lane 38'}\n", "{'_id': 11, 'name': 'Ben', 'address': 'Park Lane 38'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144eb0'), 'name': 'Amy', 'address': 'Apple st 652'}\n", "{'_id': 3, 'name': 'Amy', 'address': 'Apple st 652'}\n" ] } ], "source": [ "mydoc = mycol.find().sort(\"name\", -1)\n", "\n", "for x in mydoc:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['customers']\n" ] } ], "source": [ "print(mydb.list_collection_names())" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 documents updated.\n" ] } ], "source": [ "myquery = { \"address\": { \"$regex\": \"^S\" } }\n", "newvalues = { \"$set\": { \"name\": \"Minnie\" } }\n", "\n", "x = mycol.update_many(myquery, newvalues)\n", "\n", "print(x.modified_count, \"documents updated.\")" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_id': ObjectId('5f6a1f44b520d93bc9144eb5'), 'name': 'Minnie', 'address': 'Sky st 331'}\n", "{'_id': ObjectId('5f6a1f44b520d93bc9144ebb'), 'name': 'Minnie', 'address': 'Sideway 1633'}\n", "{'_id': 8, 'name': 'Minnie', 'address': 'Sky st 331'}\n", "{'_id': 14, 'name': 'Minnie', 'address': 'Sideway 1633'}\n" ] } ], "source": [ "myquery = { \"address\": { \"$regex\": \"^S\" } }\n", "for x in mycol.find(myquery):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myquery = { \"address\": \"Mountain 21\" }\n", "mycol.delete_one(myquery)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "26 documents deleted.\n" ] } ], "source": [ "x = mycol.delete_many({})\n", "\n", "print(x.deleted_count, \" documents deleted.\")" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "mycol.drop()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "myclient.drop_database(\"aggregation_example\")" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ObjectId('5f636b39b520d9f06a484f4d'),\n", " ObjectId('5f636b39b520d9f06a484f4e'),\n", " ObjectId('5f636b39b520d9f06a484f4f'),\n", " ObjectId('5f636b39b520d9f06a484f50')]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db = pymongo.MongoClient().aggregation_example\n", "result = db.things.insert_many([{\"x\": 1, \"tags\": [\"dog\", \"cat\"]},\n", " {\"x\": 2, \"tags\": [\"cat\"]},\n", " {\"x\": 2, \"tags\": [\"mouse\", \"cat\", \"dog\"]},\n", " {\"x\": 3, \"tags\": []}])\n", "result.inserted_ids" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'_id': ObjectId('5f636b39b520d9f06a484f4d'), 'tags': 'dog', 'x': 1},\n", " {'_id': ObjectId('5f636b39b520d9f06a484f4d'), 'tags': 'cat', 'x': 1},\n", " {'_id': ObjectId('5f636b39b520d9f06a484f4e'), 'tags': 'cat', 'x': 2},\n", " {'_id': ObjectId('5f636b39b520d9f06a484f4f'), 'tags': 'mouse', 'x': 2},\n", " {'_id': ObjectId('5f636b39b520d9f06a484f4f'), 'tags': 'cat', 'x': 2},\n", " {'_id': ObjectId('5f636b39b520d9f06a484f4f'), 'tags': 'dog', 'x': 2}]\n" ] } ], "source": [ "pipeline = [\n", " {\"$unwind\": \"$tags\"},\n", "# {\"$group\": {\"_id\": \"$tags\", \"count\": {\"$sum\": 1}}},\n", "# {\"$sort\": SON([(\"count\", -1), (\"_id\", -1)])}\n", "]\n", "pprint.pprint(list(db.things.aggregate(pipeline)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.5.6" } }, "nbformat": 4, "nbformat_minor": 1 }