Created
August 3, 2019 08:56
-
-
Save brydavis/8a1a94ded28aacff53fe0cf769684ff9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"from pymongo import MongoClient\n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# mongo_client?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# MongoClient?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"\n", | |
"# CLIENT\n", | |
"mongo = MongoClient(\"mongodb://localhost:27017\")\n", | |
"# 127.0.0.1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"# CREATE A DATABASE\n", | |
"db = mongo[\"ecommerce_store\"]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# TABLE == COLLECTION\n", | |
"\n", | |
"customers = db[\"customers\"]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5d40f396a64c92be10817e90\n" | |
] | |
} | |
], | |
"source": [ | |
"# CREATE\n", | |
"# READ\n", | |
"# UPDATE\n", | |
"# DELETE\n", | |
"\n", | |
"# CRUD\n", | |
"\n", | |
"\n", | |
"\n", | |
"# DOCUMENT == ROW\n", | |
"\n", | |
"row = {\n", | |
" \"first_name\": \"Sam\", \n", | |
" \"last_name\": \"Adams\",\n", | |
" \"occupation\": \"beverage maker\",\n", | |
" \"city\": \"Boston\"\n", | |
"}\n", | |
"\n", | |
"# CREATE\n", | |
"results = customers.insert_one(row)\n", | |
"\n", | |
"if results.acknowledged: \n", | |
" print(results.inserted_id)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ObjectId('5d40f9a2a64c92be10817e9b'), ObjectId('5d40f9a2a64c92be10817e9c'), ObjectId('5d40f9a2a64c92be10817e9d'), ObjectId('5d40f9a2a64c92be10817e9e'), ObjectId('5d40f9a2a64c92be10817e9f')]\n" | |
] | |
} | |
], | |
"source": [ | |
"# INSERT MULTIPLE ROWS\n", | |
"\n", | |
"\n", | |
"rows = [\n", | |
" { \"first_name\": \"Amy\", \"last_name\": \"Jones\", \"city\": \"Seattle\" },\n", | |
" { \"first_name\": \"Vu\", \"last_name\": \"Huong\", \"city\": \"New York\" },\n", | |
" { \"first_name\": \"Keerthi\", \"last_name\": \"Mittal\", \"city\": \"Charlotte\" },\n", | |
" { \"first_name\": \"Sandy\", \"last_name\": \"Richardson\", \"city\": \"Portland\" },\n", | |
" { \"first_name\": \"Bryan\", \"last_name\": \"Davis\", \"city\": [\"Seattle\",\"Chicago\"] },\n", | |
"]\n", | |
"\n", | |
"results = customers.insert_many(rows)\n", | |
"\n", | |
"if results.acknowledged: \n", | |
" print(results.inserted_ids)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"_id = results.inserted_ids[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'5d40f9a2a64c92be10817e9b'" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"str(_id)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'_id': ObjectId('5d40f396a64c92be10817e90'),\n", | |
" 'first_name': 'Sam',\n", | |
" 'last_name': 'Adams',\n", | |
" 'occupation': 'beverage maker',\n", | |
" 'city': 'Boston'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e91'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e92'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'New York'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e93'),\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal',\n", | |
" 'city': 'Charlotte'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e94'),\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson',\n", | |
" 'city': 'Portland'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e95'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': 'Seattle'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e96'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e97'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'New York'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e98'),\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal',\n", | |
" 'city': 'Charlotte'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e99'),\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson',\n", | |
" 'city': 'Portland'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e9a'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': ['Seattle']},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9b'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9c'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'New York'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9d'),\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal',\n", | |
" 'city': 'Charlotte'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9e'),\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson',\n", | |
" 'city': 'Portland'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9f'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': ['Seattle', 'Chicago']}]" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"# READ ALL DOCUMENTS\n", | |
"list(customers.find())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"where = {\"city\": \"Seattle\"}\n", | |
"\n", | |
"seattlites = customers.find_one(where)\n", | |
"\n", | |
"type(seattlites)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"\n", | |
"# UPDATE\n", | |
"\n", | |
"where = {\n", | |
" \"last_name\": \"Huong\"\n", | |
"}\n", | |
"\n", | |
"update = {\n", | |
" \"$set\": {\"city\": \"San Francisco\"}\n", | |
"}\n", | |
"\n", | |
"\n", | |
"results = customers.update_one(\n", | |
" where,\n", | |
" update\n", | |
")\n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "raw", | |
"metadata": {}, | |
"source": [ | |
" resul" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"results.matched_count" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'_id': ObjectId('5d40f4e2a64c92be10817e92'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'San Francisco'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e97'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'San Francisco'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9c'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'San Francisco'}]" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"list(customers.find({\"last_name\":\"Huong\"}))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<pymongo.results.UpdateResult at 0x107c8c320>" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from random import randint, random, seed\n", | |
"from datetime import datetime\n", | |
"\n", | |
"seed(datetime.now())\n", | |
"\n", | |
"where = {\n", | |
" \"city\": \"San Francisco\"\n", | |
"}\n", | |
"\n", | |
"update = {\n", | |
" \"$set\": {\"age\": 35}\n", | |
"}\n", | |
"\n", | |
"\n", | |
"customers.update_many(\n", | |
" where,\n", | |
" update\n", | |
")\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'_id': ObjectId('5d40f396a64c92be10817e90'),\n", | |
" 'first_name': 'Sam',\n", | |
" 'last_name': 'Adams',\n", | |
" 'occupation': 'beverage maker',\n", | |
" 'city': 'Boston'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e91'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e92'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'San Francisco',\n", | |
" 'age': 35},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e93'),\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal',\n", | |
" 'city': 'Charlotte'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e94'),\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson',\n", | |
" 'city': 'Portland'},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e95'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e96'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e97'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'San Francisco',\n", | |
" 'age': 35},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e98'),\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal',\n", | |
" 'city': 'Charlotte'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e99'),\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson',\n", | |
" 'city': 'Portland'},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e9a'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': ['Seattle'],\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9b'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9c'),\n", | |
" 'first_name': 'Vu',\n", | |
" 'last_name': 'Huong',\n", | |
" 'city': 'San Francisco',\n", | |
" 'age': 35},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9d'),\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal',\n", | |
" 'city': 'Charlotte'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9e'),\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson',\n", | |
" 'city': 'Portland'},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9f'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': ['Seattle', 'Chicago'],\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30}]" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"list(customers.find({}))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'_id': ObjectId('5d40f4e2a64c92be10817e91'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f4e2a64c92be10817e95'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e96'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f8b6a64c92be10817e9a'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': ['Seattle'],\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9b'),\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'city': 'Seattle',\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30},\n", | |
" {'_id': ObjectId('5d40f9a2a64c92be10817e9f'),\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'city': ['Seattle', 'Chicago'],\n", | |
" 'occupation': 'developer',\n", | |
" 'age': 30}]" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"list(customers.find({\n", | |
" \"age\": {\"$lt\": 32},\n", | |
" \"occupation\": {\"$eq\": \"developer\"}\n", | |
"}))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<pymongo.results.DeleteResult at 0x108285d70>" | |
] | |
}, | |
"execution_count": 68, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"where = {\n", | |
" \"city\": \"Seattle\",\n", | |
" \"last_name\": \"Davis\"\n", | |
"}\n", | |
"\n", | |
"\n", | |
"\n", | |
"customers.delete_one(\n", | |
" where\n", | |
")\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'_id': ObjectId('5d40f396a64c92be10817e90'),\n", | |
" 'city': 'Boston',\n", | |
" 'first_name': 'Sam',\n", | |
" 'last_name': 'Adams',\n", | |
" 'occupation': 'beverage maker'}\n", | |
"{'_id': ObjectId('5d40f4e2a64c92be10817e91'),\n", | |
" 'age': 30,\n", | |
" 'city': 'Seattle',\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'occupation': 'developer'}\n", | |
"{'_id': ObjectId('5d40f4e2a64c92be10817e93'),\n", | |
" 'city': 'Charlotte',\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal'}\n", | |
"{'_id': ObjectId('5d40f4e2a64c92be10817e94'),\n", | |
" 'city': 'Portland',\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson'}\n", | |
"{'_id': ObjectId('5d40f8b6a64c92be10817e96'),\n", | |
" 'age': 30,\n", | |
" 'city': 'Seattle',\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'occupation': 'developer'}\n", | |
"{'_id': ObjectId('5d40f8b6a64c92be10817e98'),\n", | |
" 'city': 'Charlotte',\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal'}\n", | |
"{'_id': ObjectId('5d40f8b6a64c92be10817e99'),\n", | |
" 'city': 'Portland',\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson'}\n", | |
"{'_id': ObjectId('5d40f8b6a64c92be10817e9a'),\n", | |
" 'age': 30,\n", | |
" 'city': ['Seattle'],\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'occupation': 'developer'}\n", | |
"{'_id': ObjectId('5d40f9a2a64c92be10817e9b'),\n", | |
" 'age': 30,\n", | |
" 'city': 'Seattle',\n", | |
" 'first_name': 'Amy',\n", | |
" 'last_name': 'Jones',\n", | |
" 'occupation': 'developer'}\n", | |
"{'_id': ObjectId('5d40f9a2a64c92be10817e9d'),\n", | |
" 'city': 'Charlotte',\n", | |
" 'first_name': 'Keerthi',\n", | |
" 'last_name': 'Mittal'}\n", | |
"{'_id': ObjectId('5d40f9a2a64c92be10817e9e'),\n", | |
" 'city': 'Portland',\n", | |
" 'first_name': 'Sandy',\n", | |
" 'last_name': 'Richardson'}\n", | |
"{'_id': ObjectId('5d40f9a2a64c92be10817e9f'),\n", | |
" 'age': 30,\n", | |
" 'city': ['Seattle', 'Chicago'],\n", | |
" 'first_name': 'Bryan',\n", | |
" 'last_name': 'Davis',\n", | |
" 'occupation': 'developer'}\n" | |
] | |
} | |
], | |
"source": [ | |
"from pprint import pprint\n", | |
"\n", | |
"for customer in customers.find():\n", | |
" pprint(customer)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'_id': ObjectId('5d40f396a64c92be10817e90'),\n", | |
" 'city': 'Boston',\n", | |
" 'first_name': 'Sam',\n", | |
" 'last_name': 'Adams'}\n" | |
] | |
} | |
], | |
"source": [ | |
"# update({}, {'$unset': {'parent.toremove':1}}, multi=True)\n", | |
"\n", | |
"customers.update_many(\n", | |
" {\"city\":\"Boston\", \"last_name\":\"Adams\"},\n", | |
" {\"$unset\": {\"occupation\" : \"\"}},\n", | |
")\n", | |
"\n", | |
"\n", | |
"for customer in customers.find({\"city\":\"Boston\"}):\n", | |
" pprint(customer)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"\n", | |
"customers.drop()\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"db[\"orders\"].drop()\n", | |
"db[\"inventory\"].drop()\n", | |
"\n", | |
"\n", | |
"\n", | |
"orders = db[\"orders\"]\n", | |
"inventory = db[\"inventory\"]\n", | |
"\n", | |
"db[\"orders\"].insert_many([\n", | |
" { \"_id\" : 1, \"item\" : \"almonds\", \"price\" : 12, \"quantity\" : 2 },\n", | |
" { \"_id\" : 2, \"item\" : \"pecans\", \"price\" : 20, \"quantity\" : 1 },\n", | |
"])\n", | |
"\n", | |
"db.inventory.insert_many([\n", | |
" { \"_id\" : 1, \"sku\" : \"almonds\", \"description\": \"product 1\", \"instock\" : 120 },\n", | |
" { \"_id\" : 2, \"sku\" : \"bread\", \"description\": \"product 2\", \"instock\" : 80 },\n", | |
" { \"_id\" : 3, \"sku\" : \"cashews\", \"description\": \"product 3\", \"instock\" : 60 },\n", | |
" { \"_id\" : 4, \"sku\" : \"pecans\", \"description\": \"product 4\", \"instock\" : 70 },\n", | |
" { \"_id\" : 5, \"sku\": None, \"description\": \"Incomplete\" },\n", | |
"])\n", | |
"\n", | |
"results = orders.aggregate([\n", | |
" {\n", | |
" \"$lookup\":\n", | |
" {\n", | |
" \"from\": \"inventory\",\n", | |
" \"localField\": \"item\",\n", | |
" \"foreignField\": \"sku\",\n", | |
" \"as\": \"inventory_docs\"\n", | |
" }\n", | |
" }\n", | |
"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'_id': 1,\n", | |
" 'inventory_docs': [{'_id': 1,\n", | |
" 'description': 'product 1',\n", | |
" 'instock': 120,\n", | |
" 'sku': 'almonds'}],\n", | |
" 'item': 'almonds',\n", | |
" 'price': 12,\n", | |
" 'quantity': 2}\n", | |
"{'_id': 2,\n", | |
" 'inventory_docs': [{'_id': 4,\n", | |
" 'description': 'product 4',\n", | |
" 'instock': 70,\n", | |
" 'sku': 'pecans'}],\n", | |
" 'item': 'pecans',\n", | |
" 'price': 20,\n", | |
" 'quantity': 1}\n" | |
] | |
} | |
], | |
"source": [ | |
"from pprint import pprint\n", | |
"\n", | |
"for row in results:\n", | |
" pprint(row)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment